|
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +data "aws_availability_zones" "available" {} |
| 6 | + |
| 7 | +locals { |
| 8 | + # Use existing (via data source) or create new zone (will fail validation, if zone is not reachable) |
| 9 | + use_existing_route53_zone = var.use_existing_route53_zone |
| 10 | + |
| 11 | + domain = var.domain |
| 12 | + |
| 13 | + # Removing trailing dot from domain - just to be sure :) |
| 14 | + domain_name = trimsuffix(local.domain, ".") |
| 15 | + |
| 16 | + zone_id = try(data.aws_route53_zone.this[0].zone_id, aws_route53_zone.this[0].zone_id) |
| 17 | + |
| 18 | + region = "eu-west-1" |
| 19 | + name = "ex-${basename(path.cwd)}" |
| 20 | + vpc_cidr = "10.0.0.0/16" |
| 21 | + azs = slice(data.aws_availability_zones.available.names, 0, 3) |
| 22 | + |
| 23 | + tags = { |
| 24 | + Name = local.name |
| 25 | + Example = local.name |
| 26 | + Repository = "https://github.com/terraform-aws-modules/terraform-aws-acm" |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +########################################################## |
| 31 | +# Example Private CA |
| 32 | +########################################################## |
| 33 | + |
| 34 | +data "aws_route53_zone" "this" { |
| 35 | + count = local.use_existing_route53_zone ? 1 : 0 |
| 36 | + |
| 37 | + name = local.domain_name |
| 38 | + private_zone = true |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_route53_zone" "this" { |
| 42 | + count = !local.use_existing_route53_zone ? 1 : 0 |
| 43 | + |
| 44 | + name = local.domain_name |
| 45 | +} |
| 46 | + |
| 47 | +module "acm" { |
| 48 | + source = "../../" |
| 49 | + |
| 50 | + providers = { |
| 51 | + aws.acm = aws, |
| 52 | + aws.dns = aws |
| 53 | + } |
| 54 | + |
| 55 | + domain_name = local.domain_name |
| 56 | + zone_id = local.zone_id |
| 57 | + |
| 58 | + subject_alternative_names = [ |
| 59 | + "*.alerts.${local.domain_name}", |
| 60 | + "new.sub.${local.domain_name}", |
| 61 | + "*.${local.domain_name}", |
| 62 | + "alerts.${local.domain_name}", |
| 63 | + ] |
| 64 | + |
| 65 | + private_authority_arn = aws_acmpca_certificate_authority.root_ca.arn |
| 66 | + |
| 67 | + tags = { |
| 68 | + Name = local.domain_name |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +################################################################################ |
| 73 | +# Supporting resources |
| 74 | +################################################################################ |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +module "vpc" { |
| 79 | + source = "terraform-aws-modules/vpc/aws" |
| 80 | + version = "~> 5.0" |
| 81 | + |
| 82 | + name = local.name |
| 83 | + cidr = local.vpc_cidr |
| 84 | + |
| 85 | + azs = local.azs |
| 86 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)] |
| 87 | + public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 48)] |
| 88 | + |
| 89 | + tags = local.tags |
| 90 | +} |
| 91 | + |
| 92 | +resource "aws_acmpca_certificate_authority" "root_ca" { |
| 93 | + type = "ROOT" |
| 94 | + |
| 95 | + certificate_authority_configuration { |
| 96 | + key_algorithm = "RSA_4096" |
| 97 | + signing_algorithm = "SHA512WITHRSA" |
| 98 | + subject { |
| 99 | + common_name = "example.com" |
| 100 | + organization = "org" |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + permanent_deletion_time_in_days = 7 |
| 105 | + enabled = true |
| 106 | +} |
0 commit comments