diff --git a/acd_pune23/terraform-eks/main.tf b/acd_pune23/terraform-eks/main.tf new file mode 100644 index 0000000..2531885 --- /dev/null +++ b/acd_pune23/terraform-eks/main.tf @@ -0,0 +1,118 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +provider "aws" { + region = var.region +} + +# Filter out local zones, which are not currently supported +# with managed node groups +data "aws_availability_zones" "available" { + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + +locals { + cluster_name = "education-eks-${random_string.suffix.result}" +} + +resource "random_string" "suffix" { + length = 8 + special = false +} + +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "5.0.0" + + name = "education-vpc" + + cidr = "10.0.0.0/16" + azs = slice(data.aws_availability_zones.available.names, 0, 3) + + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] + public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"] + + enable_nat_gateway = true + single_nat_gateway = true + 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 + } +} + +module "eks" { + source = "terraform-aws-modules/eks/aws" + version = "19.15.3" + + cluster_name = local.cluster_name + cluster_version = "1.27" + + vpc_id = module.vpc.vpc_id + subnet_ids = module.vpc.private_subnets + cluster_endpoint_public_access = true + + eks_managed_node_group_defaults = { + ami_type = "AL2_x86_64" + + } + + eks_managed_node_groups = { + one = { + name = "node-group-1" + + instance_types = ["t3.small"] + + min_size = 3 + max_size = 4 + desired_size = 3 + } + + two = { + name = "node-group-2" + + instance_types = ["t3.small"] + + min_size = 3 + max_size = 4 + desired_size = 3 + } + } +} + + +# https://aws.amazon.com/blogs/containers/amazon-ebs-csi-driver-is-now-generally-available-in-amazon-eks-add-ons/ +data "aws_iam_policy" "ebs_csi_policy" { + arn = "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy" +} + +module "irsa-ebs-csi" { + source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc" + version = "4.7.0" + + create_role = true + role_name = "AmazonEKSTFEBSCSIRole-${module.eks.cluster_name}" + provider_url = module.eks.oidc_provider + role_policy_arns = [data.aws_iam_policy.ebs_csi_policy.arn] + oidc_fully_qualified_subjects = ["system:serviceaccount:kube-system:ebs-csi-controller-sa"] +} + +resource "aws_eks_addon" "ebs-csi" { + cluster_name = module.eks.cluster_name + addon_name = "aws-ebs-csi-driver" + addon_version = "v1.20.0-eksbuild.1" + service_account_role_arn = module.irsa-ebs-csi.iam_role_arn + tags = { + "eks_addon" = "ebs-csi" + "terraform" = "true" + } +} \ No newline at end of file diff --git a/acd_pune23/terraform-eks/outputs.tf b/acd_pune23/terraform-eks/outputs.tf new file mode 100644 index 0000000..2fe4a26 --- /dev/null +++ b/acd_pune23/terraform-eks/outputs.tf @@ -0,0 +1,22 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +output "cluster_endpoint" { + description = "Endpoint for EKS control plane" + value = module.eks.cluster_endpoint +} + +output "cluster_security_group_id" { + description = "Security group ids attached to the cluster control plane" + value = module.eks.cluster_security_group_id +} + +output "region" { + description = "AWS region" + value = var.region +} + +output "cluster_name" { + description = "Kubernetes Cluster Name" + value = module.eks.cluster_name +} \ No newline at end of file diff --git a/acd_pune23/terraform-eks/terraform.tf b/acd_pune23/terraform-eks/terraform.tf new file mode 100644 index 0000000..0b34172 --- /dev/null +++ b/acd_pune23/terraform-eks/terraform.tf @@ -0,0 +1,36 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +terraform { + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.7.0" + } + + random = { + source = "hashicorp/random" + version = "~> 3.5.1" + } + + tls = { + source = "hashicorp/tls" + version = "~> 4.0.4" + } + + cloudinit = { + source = "hashicorp/cloudinit" + version = "~> 2.3.2" + } + } + + backend "s3" { + bucket = "tfstateacdpune" + key = "tfstate" + region = "us-west-2" + +} + + required_version = "~> 1.3" +} \ No newline at end of file diff --git a/acd_pune23/terraform-eks/terraform.tfvars b/acd_pune23/terraform-eks/terraform.tfvars new file mode 100644 index 0000000..e69de29 diff --git a/acd_pune23/terraform-eks/variables.tf b/acd_pune23/terraform-eks/variables.tf new file mode 100644 index 0000000..9eb2ce6 --- /dev/null +++ b/acd_pune23/terraform-eks/variables.tf @@ -0,0 +1,8 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +variable "region" { + description = "AWS region" + type = string + default = "us-west-2" +} \ No newline at end of file