Create multi-az VPC in AWS w/ Terraform and _not_ specifying individual zones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
provider "aws" { | |
region = "us-west-2" | |
profile = "md-sandbox" | |
} | |
resource "aws_s3_bucket" "dont_create_me_in_same_script_obvi" { | |
bucket = "terraform-provisioning-cache" | |
acl = "private" | |
} | |
data "aws_availability_zones" "available" { | |
state = "available" | |
} | |
data "aws_s3_bucket_objects" "aws_availability_zones_cache_exists" { | |
bucket = aws_s3_bucket.dont_create_me_in_same_script_obvi.id | |
prefix = local.vpc_az_cache_key | |
} | |
locals { | |
vpc_az_cache_key = "orgId/pkgId/vpc-availability-zones.json" | |
cache_keys = data.aws_s3_bucket_objects.aws_availability_zones_cache_exists.keys | |
cache_exists = length(local.cache_keys) > 0 | |
zones_json = jsonencode(data.aws_availability_zones.available.names) | |
availability_cache = local.cache_exists ? data.aws_s3_bucket_object.aws_availability_zones_cache_reader[0].body : local.zones_json | |
} | |
# The object keys should be a unique value for the terraform run (org/pkg/cache-key-name) | |
data "aws_s3_bucket_object" "aws_availability_zones_cache_reader" { | |
count = local.cache_exists ? 1 : 0 | |
bucket = aws_s3_bucket.dont_create_me_in_same_script_obvi.id | |
key = local.vpc_az_cache_key | |
} | |
resource "aws_s3_bucket_object" "aws_availability_zones_cache_reader" { | |
tags = {} | |
metadata = {} | |
content_type = "application/json" | |
bucket = aws_s3_bucket.dont_create_me_in_same_script_obvi.id | |
key = local.vpc_az_cache_key | |
content = local.availability_cache | |
etag = md5(local.availability_cache) | |
} | |
output "resource-availability_cache" { | |
value = aws_s3_bucket_object.aws_availability_zones_cache_reader.content | |
} | |
output "local-availability_cache" { | |
value = local.availability_cache | |
} | |
output "did_cache_exist" { | |
value = local.cache_exists | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment