Skip to content

Commit

Permalink
check if vpc already created
Browse files Browse the repository at this point in the history
  • Loading branch information
leiicamundi committed Jan 17, 2025
1 parent 9f70b4a commit c553308
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/eks-cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module "eks_cluster" {
| [time_sleep.eks_cluster_warmup](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_vpc.existing](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
| [external_external.elastic_ip_quota](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
| [external_external.elastic_ips_count](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
## Inputs
Expand Down
13 changes: 11 additions & 2 deletions modules/eks-cluster/vpc.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ data "external" "elastic_ips_count" {
program = ["bash", "./get_elastic_ips_count.sh", var.region]
}

# Data source to check if the VPC exists
data "aws_vpc" "existing" {
filter {
name = "tag:Name"
values = [local.vpc_name]
}
}

check "elastic_ip_quota_check" {

# Only check the condition when no existing vpc is there
assert {
condition = tonumber(data.external.elastic_ip_quota.result.quota) >= length(local.azs)
condition = length(data.aws_vpc.existing.ids) > 0 || tonumber(data.external.elastic_ip_quota.result.quota) >= length(local.azs)
error_message = "The Elastic IP quota is insufficient to cover all local availability zones (need: ${length(local.azs)}, have: ${tonumber(data.external.elastic_ip_quota.result.quota)})."
}

assert {
condition = (tonumber(data.external.elastic_ip_quota.result.quota) - tonumber(data.external.elastic_ips_count.result.elastic_ips_count)) >= length(local.azs)
condition = length(data.aws_vpc.existing.ids) > 0 || (tonumber(data.external.elastic_ip_quota.result.quota) - tonumber(data.external.elastic_ips_count.result.elastic_ips_count)) >= length(local.azs)
error_message = "Not enough available Elastic IPs to cover all local availability zones (need: ${length(local.azs)}, have: ${(tonumber(data.external.elastic_ip_quota.result.quota) - tonumber(data.external.elastic_ips_count.result.elastic_ips_count))})."
}
}
Expand Down

0 comments on commit c553308

Please sign in to comment.