|
| 1 | +provider "aws" { |
| 2 | + region = local.region |
| 3 | +} |
| 4 | + |
| 5 | +provider "kubernetes" { |
| 6 | + host = module.eks_blueprints.eks_cluster_endpoint |
| 7 | + cluster_ca_certificate = base64decode(module.eks_blueprints.eks_cluster_certificate_authority_data) |
| 8 | + token = data.aws_eks_cluster_auth.this.token |
| 9 | +} |
| 10 | + |
| 11 | +provider "helm" { |
| 12 | + kubernetes { |
| 13 | + host = module.eks_blueprints.eks_cluster_endpoint |
| 14 | + cluster_ca_certificate = base64decode(module.eks_blueprints.eks_cluster_certificate_authority_data) |
| 15 | + token = data.aws_eks_cluster_auth.this.token |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +provider "kubectl" { |
| 20 | + apply_retry_count = 10 |
| 21 | + host = module.eks_blueprints.eks_cluster_endpoint |
| 22 | + cluster_ca_certificate = base64decode(module.eks_blueprints.eks_cluster_certificate_authority_data) |
| 23 | + load_config_file = false |
| 24 | + token = data.aws_eks_cluster_auth.this.token |
| 25 | +} |
| 26 | + |
| 27 | +data "aws_eks_cluster_auth" "this" { |
| 28 | + name = module.eks_blueprints.eks_cluster_id |
| 29 | +} |
| 30 | + |
| 31 | +data "aws_availability_zones" "available" {} |
| 32 | + |
| 33 | +locals { |
| 34 | + name = basename(path.cwd) |
| 35 | + region = "us-west-2" |
| 36 | + |
| 37 | + vpc_cidr = "10.0.0.0/16" |
| 38 | + azs = slice(data.aws_availability_zones.available.names, 0, 3) |
| 39 | + |
| 40 | + |
| 41 | + tags = { |
| 42 | + Blueprint = local.name |
| 43 | + GithubRepo = "github.com/aws-ia/terraform-aws-eks-blueprints" |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#--------------------------------------------------------------- |
| 48 | +# EKS Blueprints |
| 49 | +#--------------------------------------------------------------- |
| 50 | + |
| 51 | +module "eks_blueprints" { |
| 52 | + source = "../.." |
| 53 | + |
| 54 | + cluster_name = local.name |
| 55 | + cluster_version = "1.23" |
| 56 | + |
| 57 | + vpc_id = module.vpc.vpc_id |
| 58 | + private_subnet_ids = module.vpc.private_subnets |
| 59 | + |
| 60 | + managed_node_groups = { |
| 61 | + this = { |
| 62 | + node_group_name = local.name |
| 63 | + instance_types = ["m5.large"] |
| 64 | + subnet_ids = module.vpc.private_subnets |
| 65 | + |
| 66 | + min_size = 1 |
| 67 | + max_size = 2 |
| 68 | + desired_size = 1 |
| 69 | + |
| 70 | + update_config = [{ |
| 71 | + max_unavailable_percentage = 30 |
| 72 | + }] |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + tags = local.tags |
| 77 | +} |
| 78 | + |
| 79 | +module "eks_blueprints_kubernetes_addons" { |
| 80 | + source = "../../modules/kubernetes-addons" |
| 81 | + |
| 82 | + eks_cluster_id = module.eks_blueprints.eks_cluster_id |
| 83 | + eks_cluster_endpoint = module.eks_blueprints.eks_cluster_endpoint |
| 84 | + eks_oidc_provider = module.eks_blueprints.oidc_provider |
| 85 | + eks_cluster_version = module.eks_blueprints.eks_cluster_version |
| 86 | + eks_cluster_domain = var.eks_cluster_domain |
| 87 | + |
| 88 | + enable_amazon_eks_vpc_cni = true |
| 89 | + enable_amazon_eks_coredns = true |
| 90 | + enable_amazon_eks_kube_proxy = true |
| 91 | + |
| 92 | + aws_privateca_acmca_arn = aws_acmpca_certificate_authority.example.arn |
| 93 | + enable_appmesh_controller = true |
| 94 | + enable_cert_manager = true |
| 95 | + enable_aws_privateca_issuer = true |
| 96 | + |
| 97 | + tags = local.tags |
| 98 | +} |
| 99 | + |
| 100 | +#--------------------------------------------------------------- |
| 101 | +# Certificate Resources |
| 102 | +#--------------------------------------------------------------- |
| 103 | + |
| 104 | +resource "aws_acmpca_certificate_authority" "example" { |
| 105 | + type = "ROOT" |
| 106 | + |
| 107 | + certificate_authority_configuration { |
| 108 | + key_algorithm = "RSA_4096" |
| 109 | + signing_algorithm = "SHA512WITHRSA" |
| 110 | + |
| 111 | + subject { |
| 112 | + common_name = "example.com" |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +resource "aws_acmpca_certificate" "example" { |
| 118 | + certificate_authority_arn = aws_acmpca_certificate_authority.example.arn |
| 119 | + certificate_signing_request = aws_acmpca_certificate_authority.example.certificate_signing_request |
| 120 | + signing_algorithm = "SHA512WITHRSA" |
| 121 | + |
| 122 | + template_arn = "arn:aws:acm-pca:::template/RootCACertificate/V1" |
| 123 | + |
| 124 | + validity { |
| 125 | + type = "YEARS" |
| 126 | + value = 10 |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +resource "aws_acmpca_certificate_authority_certificate" "example" { |
| 131 | + certificate_authority_arn = aws_acmpca_certificate_authority.example.arn |
| 132 | + |
| 133 | + certificate = aws_acmpca_certificate.example.certificate |
| 134 | + certificate_chain = aws_acmpca_certificate.example.certificate_chain |
| 135 | +} |
| 136 | + |
| 137 | +# This resource creates a CRD of AWSPCAClusterIssuer Kind, which then represents the ACM PCA in K8 |
| 138 | +resource "kubectl_manifest" "cluster_pca_issuer" { |
| 139 | + yaml_body = yamlencode({ |
| 140 | + apiVersion = "awspca.cert-manager.io/v1beta1" |
| 141 | + kind = "AWSPCAClusterIssuer" |
| 142 | + |
| 143 | + metadata = { |
| 144 | + name = module.eks_blueprints.eks_cluster_id |
| 145 | + } |
| 146 | + |
| 147 | + spec = { |
| 148 | + arn = aws_acmpca_certificate_authority.example.arn |
| 149 | + region : local.region |
| 150 | + } |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +# This resource creates a CRD of Certificate Kind, which then represents certificate issued from ACM PCA, |
| 155 | +# mounted as K8 secret |
| 156 | +resource "kubectl_manifest" "example_pca_certificate" { |
| 157 | + yaml_body = yamlencode({ |
| 158 | + apiVersion = "cert-manager.io/v1" |
| 159 | + kind = "Certificate" |
| 160 | + |
| 161 | + metadata = { |
| 162 | + name = var.certificate_name |
| 163 | + namespace = "default" |
| 164 | + } |
| 165 | + |
| 166 | + spec = { |
| 167 | + commonName = var.certificate_dns |
| 168 | + duration = "2160h0m0s" |
| 169 | + issuerRef = { |
| 170 | + group = "awspca.cert-manager.io" |
| 171 | + kind = "AWSPCAClusterIssuer" |
| 172 | + name : module.eks_blueprints.eks_cluster_id |
| 173 | + } |
| 174 | + renewBefore = "360h0m0s" |
| 175 | + # This is the name with which the K8 Secret will be available |
| 176 | + secretName = "${var.certificate_name}-clusterissuer" |
| 177 | + usages = [ |
| 178 | + "server auth", |
| 179 | + "client auth" |
| 180 | + ] |
| 181 | + privateKey = { |
| 182 | + algorithm : "RSA" |
| 183 | + size : 2048 |
| 184 | + } |
| 185 | + } |
| 186 | + }) |
| 187 | + |
| 188 | + depends_on = [ |
| 189 | + kubectl_manifest.cluster_pca_issuer, |
| 190 | + ] |
| 191 | +} |
| 192 | + |
| 193 | +#--------------------------------------------------------------- |
| 194 | +# Supporting Resources |
| 195 | +#--------------------------------------------------------------- |
| 196 | + |
| 197 | +module "vpc" { |
| 198 | + source = "terraform-aws-modules/vpc/aws" |
| 199 | + version = "~> 3.0" |
| 200 | + |
| 201 | + name = local.name |
| 202 | + cidr = local.vpc_cidr |
| 203 | + |
| 204 | + azs = local.azs |
| 205 | + public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)] |
| 206 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)] |
| 207 | + |
| 208 | + enable_nat_gateway = true |
| 209 | + single_nat_gateway = true |
| 210 | + enable_dns_hostnames = true |
| 211 | + |
| 212 | + # Manage so we can name |
| 213 | + manage_default_network_acl = true |
| 214 | + default_network_acl_tags = { Name = "${local.name}-default" } |
| 215 | + manage_default_route_table = true |
| 216 | + default_route_table_tags = { Name = "${local.name}-default" } |
| 217 | + manage_default_security_group = true |
| 218 | + default_security_group_tags = { Name = "${local.name}-default" } |
| 219 | + |
| 220 | + public_subnet_tags = { |
| 221 | + "kubernetes.io/cluster/${local.name}" = "shared" |
| 222 | + "kubernetes.io/role/elb" = 1 |
| 223 | + } |
| 224 | + |
| 225 | + private_subnet_tags = { |
| 226 | + "kubernetes.io/cluster/${local.name}" = "shared" |
| 227 | + "kubernetes.io/role/internal-elb" = 1 |
| 228 | + } |
| 229 | + |
| 230 | + tags = local.tags |
| 231 | +} |
0 commit comments