/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ terraform { required_version = ">= 1.0.1" required_providers { aws = { source = "hashicorp/aws" version = ">= 3.66.0" } kubernetes = { source = "hashicorp/kubernetes" version = ">= 2.6.1" } helm = { source = "hashicorp/helm" version = ">= 2.4.1" } } } provider "aws" { region = data.aws_region.current.id alias = "default" } terraform { backend "local" { path = "local_tf_state/terraform-main.tfstate" } } data "aws_region" "current" {} data "aws_availability_zones" "available" {} locals { tenant = "aws001" # AWS account name or unique id for tenant environment = "preprod" # Environment area eg., preprod or prod zone = "dev" # Environment with in one sub_tenant or business unit kubernetes_version = "1.21" vpc_cidr = "10.0.0.0/16" vpc_name = join("-", [local.tenant, local.environment, local.zone, "vpc"]) cluster_name = join("-", [local.tenant, local.environment, local.zone, "eks"]) terraform_version = "Terraform v1.0.1" } module "aws_vpc" { source = "terraform-aws-modules/vpc/aws" version = "v3.2.0" name = local.vpc_name cidr = local.vpc_cidr azs = data.aws_availability_zones.available.names private_subnets = [for k, v in slice(data.aws_availability_zones.available.names, 0, 3) : cidrsubnet(local.vpc_cidr, 8, k + 10)] enable_dns_hostnames = true public_subnet_tags = { "kubernetes.io/cluster/${local.cluster_name}" = "shared" "kubernetes.io/role/elb" = "1" } private_subnet_tags = { "kubernetes.io/cluster/${local.cluster_name}" = "shared" "kubernetes.io/role/internal-elb" = "1" } manage_default_security_group = true default_security_group_name = "${local.vpc_name}-endpoint-secgrp" default_security_group_ingress = [ { protocol = -1 from_port = 0 to_port = 0 cidr_blocks = local.vpc_cidr }] default_security_group_egress = [ { from_port = 0 to_port = 0 protocol = -1 cidr_blocks = "0.0.0.0/0" }] } module "vpc_endpoint_gateway" { source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints" version = "v3.2.0" create = true vpc_id = module.aws_vpc.vpc_id endpoints = { s3 = { service = "s3" service_type = "Gateway" route_table_ids = flatten([ module.aws_vpc.intra_route_table_ids, module.aws_vpc.private_route_table_ids]) tags = { Name = "s3-vpc-Gateway" } }, } } data "aws_security_group" "default" { name = "default" vpc_id = module.aws_vpc.vpc_id } module "vpc_endpoints" { source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints" version = "v3.2.0" create = true vpc_id = module.aws_vpc.vpc_id security_group_ids = [ data.aws_security_group.default.id] subnet_ids = module.aws_vpc.private_subnets endpoints = { ssm = { service = "ssm" private_dns_enabled = true }, logs = { service = "logs" private_dns_enabled = true }, autoscaling = { service = "autoscaling" private_dns_enabled = true }, sts = { service = "sts" private_dns_enabled = true }, elasticloadbalancing = { service = "elasticloadbalancing" private_dns_enabled = true }, ec2 = { service = "ec2" private_dns_enabled = true }, ec2messages = { service = "ec2messages" private_dns_enabled = true }, ecr_api = { service = "ecr.api" private_dns_enabled = true }, ecr_dkr = { service = "ecr.dkr" private_dns_enabled = true }, kms = { service = "kms" private_dns_enabled = true } } tags = { Project = "EKS" Endpoint = "true" } } #--------------------------------------------------------------- # Example to consume aws-eks-accelerator-for-terraform module #--------------------------------------------------------------- module "aws-eks-accelerator-for-terraform" { source = "github.com/aws-samples/aws-eks-accelerator-for-terraform" tenant = local.tenant environment = local.environment zone = local.zone terraform_version = local.terraform_version # EKS Cluster VPC and Subnet mandatory config vpc_id = module.aws_vpc.vpc_id private_subnet_ids = module.aws_vpc.private_subnets # EKS CONTROL PLANE VARIABLES create_eks = true kubernetes_version = local.kubernetes_version # EKS MANAGED NODE GROUPS managed_node_groups = { mg_4 = { node_group_name = "managed-ondemand" instance_types = ["m4.large"] subnet_ids = module.aws_vpc.private_subnets } } }