From e3655ef895c7528fe98a00aca061e619388bcb5c Mon Sep 17 00:00:00 2001 From: cybershady Date: Wed, 14 Aug 2024 20:23:47 -0600 Subject: [PATCH 1/6] start eks module and fix tf fmt checker --- .github/workflows/tf-fmt-check.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tf-fmt-check.yml b/.github/workflows/tf-fmt-check.yml index 4cb931c..167068d 100644 --- a/.github/workflows/tf-fmt-check.yml +++ b/.github/workflows/tf-fmt-check.yml @@ -1,4 +1,4 @@ -name: tfactions +name: tf-fmt-check on: push: branches: @@ -6,7 +6,7 @@ on: pull_request: jobs: tfactions: - name: tfactions + name: tf-fmt-check runs-on: ubuntu-latest steps: - name: Checkout repository @@ -18,4 +18,4 @@ jobs: - name: Terraform fmt id: fmt - run: terraform fmt -check \ No newline at end of file + run: terraform fmt -check From 3ebcd62eaa81a49220abb6ede1c3738b66591f2c Mon Sep 17 00:00:00 2001 From: cybershady Date: Thu, 15 Aug 2024 19:08:41 -0600 Subject: [PATCH 2/6] adding initial eks work --- main.tf | 65 +++++++++++++++++++++++++++++++ modules/eks/main.tf | 17 ++++++++ modules/eks/outputs.tf | 0 modules/eks/variables.tf | 84 ++++++++++++++++++++++++++++++++++++++++ terraform.tfvars | 3 +- variables.tf | 6 ++- 6 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 modules/eks/main.tf create mode 100644 modules/eks/outputs.tf create mode 100644 modules/eks/variables.tf diff --git a/main.tf b/main.tf index 8976331..08af0c7 100644 --- a/main.tf +++ b/main.tf @@ -3,4 +3,69 @@ module "network" { name = var.name create_ngw = false vpc_cidr = "10.0.0.0/16" +} + +module "eks" { + source = "terraform-aws-modules/eks/aws" + version = "~> 20.0" + + cluster_name = "${var.name}-cluster" + cluster_version = "1.30" + + cluster_endpoint_public_access = true + + cluster_addons = { + coredns = {} + eks-pod-identity-agent = {} + kube-proxy = {} + vpc-cni = {} + } + + vpc_id = module.network.vpc + subnet_ids = module.network.private_subnet_ids + control_plane_subnet_ids = module.network.private_subnet_ids + + # EKS Managed Node Group(s) + eks_managed_node_group_defaults = { + instance_types = ["m6i.large", "m5.large", "m5n.large", "m5zn.large"] + } + + eks_managed_node_groups = { + example = { + # Starting on 1.30, AL2023 is the default AMI type for EKS managed node groups + ami_type = "AL2023_x86_64_STANDARD" + instance_types = ["m5.xlarge"] + + min_size = 1 + max_size = 10 + desired_size = 1 + } + } + + # Cluster access entry + # To add the current caller identity as an administrator + enable_cluster_creator_admin_permissions = true + + access_entries = { + # One access entry with a policy associated + example = { + kubernetes_groups = [] + principal_arn = "arn:aws:iam::123456789012:role/something" + + policy_associations = { + example = { + policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" + access_scope = { + namespaces = ["default"] + type = "namespace" + } + } + } + } + } + + tags = { + Environment = "dev" + Terraform = "true" + } } \ No newline at end of file diff --git a/modules/eks/main.tf b/modules/eks/main.tf new file mode 100644 index 0000000..30dfdc5 --- /dev/null +++ b/modules/eks/main.tf @@ -0,0 +1,17 @@ +# resource "aws_eks_cluster" "this" { +# count = var.create_eks ? 1 : 0 + +# name = var.cluster_name +# role_arn = local.cluster_role +# version = var.cluster_version +# enabled_cluster_log_types = var.cluster_enabled_log_types +# bootstrap_self_managed_addons = var.bootstrap_self_managed_addons + +# vpc_config { +# security_group_ids = compact(distinct(concat(var.cluster_additional_security_group_ids, [local.cluster_security_group_id]))) +# subnet_ids = coalescelist(var.control_plane_subnet_ids, var.subnet_ids) +# endpoint_private_access = var.cluster_endpoint_private_access +# endpoint_public_access = var.cluster_endpoint_public_access +# public_access_cidrs = var.cluster_endpoint_public_access_cidrs +# } +# } \ No newline at end of file diff --git a/modules/eks/outputs.tf b/modules/eks/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/modules/eks/variables.tf b/modules/eks/variables.tf new file mode 100644 index 0000000..7e95e26 --- /dev/null +++ b/modules/eks/variables.tf @@ -0,0 +1,84 @@ +# variable "create_eks" { +# type = bool +# default = false +# } + +# variable "cluster_name" { +# description = "Name of the EKS cluster" +# type = string +# default = "" +# } + +# variable "cluster_version" { +# description = "Kubernetes `.` version to use for the EKS cluster (i.e.: `1.27`)" +# type = string +# default = null +# } + +# variable "authentication_mode" { +# description = "The authentication mode for the cluster. Valid values are `CONFIG_MAP`, `API` or `API_AND_CONFIG_MAP`" +# type = string +# default = "API_AND_CONFIG_MAP" +# } + +# variable "control_plane_subnet_ids" { +# description = "A list of subnet IDs where the EKS cluster control plane (ENIs) will be provisioned. Used for expanding the pool of subnets used by nodes/node groups without replacing the EKS control plane" +# type = list(string) +# default = [] +# } + +# variable "subnet_ids" { +# description = "A list of subnet IDs where the nodes/node groups will be provisioned. If `control_plane_subnet_ids` is not provided, the EKS cluster control plane (ENIs) will be provisioned in these subnets" +# type = list(string) +# default = [] +# } + +# variable "cluster_endpoint_private_access" { +# description = "Indicates whether or not the Amazon EKS private API server endpoint is enabled" +# type = bool +# default = true +# } + +# variable "cluster_endpoint_public_access" { +# description = "Indicates whether or not the Amazon EKS public API server endpoint is enabled" +# type = bool +# default = false +# } + +# variable "cluster_endpoint_public_access_cidrs" { +# description = "List of CIDR blocks which can access the Amazon EKS public API server endpoint" +# type = list(string) +# default = ["0.0.0.0/0"] +# } + +# variable "cluster_encryption_config" { +# description = "Configuration block with encryption configuration for the cluster. To disable secret encryption, set this value to `{}`" +# type = any +# default = { +# resources = ["secrets"] +# } +# } + +# variable "attach_cluster_encryption_policy" { +# description = "Indicates whether or not to attach an additional policy for the cluster IAM role to utilize the encryption key provided" +# type = bool +# default = true +# } + +# variable "cluster_tags" { +# description = "A map of additional tags to add to the cluster" +# type = map(string) +# default = {} +# } + +# variable "create_cluster_primary_security_group_tags" { +# description = "Indicates whether or not to tag the cluster's primary security group. This security group is created by the EKS service, not the module, and therefore tagging is handled after cluster creation" +# type = bool +# default = true +# } + +# variable "enable_cluster_creator_admin_permissions" { +# description = "Indicates whether or not to add the cluster creator (the identity used by Terraform) as an administrator via access entry" +# type = bool +# default = true +# } \ No newline at end of file diff --git a/terraform.tfvars b/terraform.tfvars index c44606e..75e3e3e 100644 --- a/terraform.tfvars +++ b/terraform.tfvars @@ -1,2 +1,3 @@ name = "playground" -region = "us-west-2" \ No newline at end of file +region = "us-west-2" +create_eks = true diff --git a/variables.tf b/variables.tf index f8d95d5..b0e5352 100644 --- a/variables.tf +++ b/variables.tf @@ -10,4 +10,8 @@ variable "region" { variable "aws_account" { description = "Account number to create aws resources in. This variable should be defined in the terraform cloud workspace settings" -} \ No newline at end of file +} + +variable "create_eks" { + default = false +} From 7b9fcca6f16ba1b49506a292e83706a0f4b3b44f Mon Sep 17 00:00:00 2001 From: cybershady Date: Thu, 15 Aug 2024 20:50:53 -0600 Subject: [PATCH 3/6] try and add own eks module --- main.tf | 108 +++++++++++++++++++++------------------ modules/eks/eks_roles.tf | 29 +++++++++++ modules/eks/main.tf | 31 +++++------ modules/eks/outputs.tf | 7 +++ modules/eks/variables.tf | 48 ++++++++--------- terraform.tfvars | 4 +- variables.tf | 2 +- 7 files changed, 137 insertions(+), 92 deletions(-) create mode 100644 modules/eks/eks_roles.tf diff --git a/main.tf b/main.tf index 08af0c7..d8ee269 100644 --- a/main.tf +++ b/main.tf @@ -4,68 +4,76 @@ module "network" { create_ngw = false vpc_cidr = "10.0.0.0/16" } - module "eks" { - source = "terraform-aws-modules/eks/aws" + source = "./modules/eks" version = "~> 20.0" cluster_name = "${var.name}-cluster" cluster_version = "1.30" - cluster_endpoint_public_access = true - - cluster_addons = { - coredns = {} - eks-pod-identity-agent = {} - kube-proxy = {} - vpc-cni = {} - } - - vpc_id = module.network.vpc subnet_ids = module.network.private_subnet_ids control_plane_subnet_ids = module.network.private_subnet_ids +} + + +# module "eks" { +# source = "terraform-aws-modules/eks/aws" +# version = "~> 20.0" + +# cluster_name = "${var.name}-cluster" +# cluster_version = "1.30" + +# cluster_endpoint_public_access = true + +# cluster_addons = { +# coredns = {} +# eks-pod-identity-agent = {} +# kube-proxy = {} +# vpc-cni = {} +# } - # EKS Managed Node Group(s) - eks_managed_node_group_defaults = { - instance_types = ["m6i.large", "m5.large", "m5n.large", "m5zn.large"] - } +# vpc_id = module.network.vpc +# subnet_ids = module.network.private_subnet_ids +# control_plane_subnet_ids = module.network.private_subnet_ids - eks_managed_node_groups = { - example = { - # Starting on 1.30, AL2023 is the default AMI type for EKS managed node groups - ami_type = "AL2023_x86_64_STANDARD" - instance_types = ["m5.xlarge"] +# # EKS Managed Node Group(s) +# eks_managed_node_group_defaults = { +# instance_types = ["m6i.large", "m5.large", "m5n.large", "m5zn.large"] +# } - min_size = 1 - max_size = 10 - desired_size = 1 - } - } +# eks_managed_node_groups = { +# example = { +# # Starting on 1.30, AL2023 is the default AMI type for EKS managed node groups +# ami_type = "AL2023_x86_64_STANDARD" +# instance_types = ["m5.xlarge"] - # Cluster access entry - # To add the current caller identity as an administrator - enable_cluster_creator_admin_permissions = true +# min_size = 1 +# max_size = 10 +# desired_size = 1 +# } +# } - access_entries = { - # One access entry with a policy associated - example = { - kubernetes_groups = [] - principal_arn = "arn:aws:iam::123456789012:role/something" +# # Cluster access entry +# # To add the current caller identity as an administrator +# enable_cluster_creator_admin_permissions = true - policy_associations = { - example = { - policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" - access_scope = { - namespaces = ["default"] - type = "namespace" - } - } - } - } - } +# access_entries = { +# # One access entry with a policy associated +# example = { +# policy_associations = { +# example = { +# policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" +# access_scope = { +# namespaces = ["default"] +# type = "namespace" +# } +# } +# } +# } +# } - tags = { - Environment = "dev" - Terraform = "true" - } -} \ No newline at end of file +# tags = { +# Environment = "dev" +# Terraform = "true" +# } +# } \ No newline at end of file diff --git a/modules/eks/eks_roles.tf b/modules/eks/eks_roles.tf new file mode 100644 index 0000000..88cc386 --- /dev/null +++ b/modules/eks/eks_roles.tf @@ -0,0 +1,29 @@ +data "aws_iam_policy_document" "assume_role" { + statement { + effect = "Allow" + + principals { + type = "Service" + identifiers = ["eks.amazonaws.com"] + } + + actions = ["sts:AssumeRole"] + } +} + +resource "aws_iam_role" "example" { + name = "eks-cluster-example" + assume_role_policy = data.aws_iam_policy_document.assume_role.json +} + +resource "aws_iam_role_policy_attachment" "example-AmazonEKSClusterPolicy" { + policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy" + role = aws_iam_role.example.name +} + +# Optionally, enable Security Groups for Pods +# Reference: https://docs.aws.amazon.com/eks/latest/userguide/security-groups-for-pods.html +resource "aws_iam_role_policy_attachment" "example-AmazonEKSVPCResourceController" { + policy_arn = "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController" + role = aws_iam_role.example.name +} \ No newline at end of file diff --git a/modules/eks/main.tf b/modules/eks/main.tf index 30dfdc5..8ace1b4 100644 --- a/modules/eks/main.tf +++ b/modules/eks/main.tf @@ -1,17 +1,18 @@ -# resource "aws_eks_cluster" "this" { -# count = var.create_eks ? 1 : 0 +resource "aws_eks_cluster" "this" { + name = var.cluster_name + role_arn = aws_iam_role.example.arn + count = var.create_eks ? 1 : 0 + version = var.cluster_version -# name = var.cluster_name -# role_arn = local.cluster_role -# version = var.cluster_version -# enabled_cluster_log_types = var.cluster_enabled_log_types -# bootstrap_self_managed_addons = var.bootstrap_self_managed_addons + vpc_config { + subnet_ids = coalescelist(var.control_plane_subnet_ids, var.subnet_ids) + } + + # Ensure that IAM Role permissions are created before and deleted after EKS Cluster handling. + # Otherwise, EKS will not be able to properly delete EKS managed EC2 infrastructure such as Security Groups. + depends_on = [ + aws_iam_role_policy_attachment.example-AmazonEKSClusterPolicy, + aws_iam_role_policy_attachment.example-AmazonEKSVPCResourceController, + ] +} -# vpc_config { -# security_group_ids = compact(distinct(concat(var.cluster_additional_security_group_ids, [local.cluster_security_group_id]))) -# subnet_ids = coalescelist(var.control_plane_subnet_ids, var.subnet_ids) -# endpoint_private_access = var.cluster_endpoint_private_access -# endpoint_public_access = var.cluster_endpoint_public_access -# public_access_cidrs = var.cluster_endpoint_public_access_cidrs -# } -# } \ No newline at end of file diff --git a/modules/eks/outputs.tf b/modules/eks/outputs.tf index e69de29..ddbdcb1 100644 --- a/modules/eks/outputs.tf +++ b/modules/eks/outputs.tf @@ -0,0 +1,7 @@ +output "endpoint" { + value = aws_eks_cluster.example.endpoint +} + +output "kubeconfig-certificate-authority-data" { + value = aws_eks_cluster.example.certificate_authority[0].data +} \ No newline at end of file diff --git a/modules/eks/variables.tf b/modules/eks/variables.tf index 7e95e26..dcea10e 100644 --- a/modules/eks/variables.tf +++ b/modules/eks/variables.tf @@ -1,19 +1,19 @@ -# variable "create_eks" { -# type = bool -# default = false -# } +variable "create_eks" { + type = bool + default = false +} -# variable "cluster_name" { -# description = "Name of the EKS cluster" -# type = string -# default = "" -# } +variable "cluster_name" { + description = "Name of the EKS cluster" + type = string + default = "" +} -# variable "cluster_version" { -# description = "Kubernetes `.` version to use for the EKS cluster (i.e.: `1.27`)" -# type = string -# default = null -# } +variable "cluster_version" { + description = "Kubernetes `.` version to use for the EKS cluster (i.e.: `1.27`)" + type = string + default = null +} # variable "authentication_mode" { # description = "The authentication mode for the cluster. Valid values are `CONFIG_MAP`, `API` or `API_AND_CONFIG_MAP`" @@ -21,17 +21,17 @@ # default = "API_AND_CONFIG_MAP" # } -# variable "control_plane_subnet_ids" { -# description = "A list of subnet IDs where the EKS cluster control plane (ENIs) will be provisioned. Used for expanding the pool of subnets used by nodes/node groups without replacing the EKS control plane" -# type = list(string) -# default = [] -# } +variable "control_plane_subnet_ids" { + description = "A list of subnet IDs where the EKS cluster control plane (ENIs) will be provisioned. Used for expanding the pool of subnets used by nodes/node groups without replacing the EKS control plane" + type = list(string) + default = [] +} -# variable "subnet_ids" { -# description = "A list of subnet IDs where the nodes/node groups will be provisioned. If `control_plane_subnet_ids` is not provided, the EKS cluster control plane (ENIs) will be provisioned in these subnets" -# type = list(string) -# default = [] -# } +variable "subnet_ids" { + description = "A list of subnet IDs where the nodes/node groups will be provisioned. If `control_plane_subnet_ids` is not provided, the EKS cluster control plane (ENIs) will be provisioned in these subnets" + type = list(string) + default = [] +} # variable "cluster_endpoint_private_access" { # description = "Indicates whether or not the Amazon EKS private API server endpoint is enabled" diff --git a/terraform.tfvars b/terraform.tfvars index 75e3e3e..5015d3c 100644 --- a/terraform.tfvars +++ b/terraform.tfvars @@ -1,3 +1,3 @@ -name = "playground" -region = "us-west-2" +name = "playground" +region = "us-west-2" create_eks = true diff --git a/variables.tf b/variables.tf index b0e5352..a80f67a 100644 --- a/variables.tf +++ b/variables.tf @@ -13,5 +13,5 @@ variable "aws_account" { } variable "create_eks" { - default = false + default = false } From 3cb51bff7ebef0305f9a61fd00ed56d4e29e6c33 Mon Sep 17 00:00:00 2001 From: cybershady Date: Thu, 15 Aug 2024 20:54:07 -0600 Subject: [PATCH 4/6] remove version, add count bool --- main.tf | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.tf b/main.tf index d8ee269..3d0b6a4 100644 --- a/main.tf +++ b/main.tf @@ -6,8 +6,7 @@ module "network" { } module "eks" { source = "./modules/eks" - version = "~> 20.0" - + create_eks = false cluster_name = "${var.name}-cluster" cluster_version = "1.30" From 99b05c3324cef7ca905b847e1e5fab966e03bd92 Mon Sep 17 00:00:00 2001 From: cybershady Date: Thu, 15 Aug 2024 20:56:09 -0600 Subject: [PATCH 5/6] fix outputs --- main.tf | 4 ++-- modules/eks/outputs.tf | 4 ++-- outputs.tf | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/main.tf b/main.tf index 3d0b6a4..dd6134e 100644 --- a/main.tf +++ b/main.tf @@ -5,8 +5,8 @@ module "network" { vpc_cidr = "10.0.0.0/16" } module "eks" { - source = "./modules/eks" - create_eks = false + source = "./modules/eks" + create_eks = false cluster_name = "${var.name}-cluster" cluster_version = "1.30" diff --git a/modules/eks/outputs.tf b/modules/eks/outputs.tf index ddbdcb1..37cf257 100644 --- a/modules/eks/outputs.tf +++ b/modules/eks/outputs.tf @@ -1,7 +1,7 @@ output "endpoint" { - value = aws_eks_cluster.example.endpoint + value = aws_eks_cluster.this.endpoint } output "kubeconfig-certificate-authority-data" { - value = aws_eks_cluster.example.certificate_authority[0].data + value = aws_eks_cluster.this.certificate_authority[0].data } \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index 121a399..6209819 100644 --- a/outputs.tf +++ b/outputs.tf @@ -5,4 +5,8 @@ output "public_subnets" { output "private_subnets" { value = module.network.private_subnet_cidr_block -} \ No newline at end of file +} + +output "eks_endpoint" { + value = module.eks.endpoint +} From 57452d19ec66ba51b18ac2c17cd34fa92e15c34f Mon Sep 17 00:00:00 2001 From: cybershady Date: Thu, 15 Aug 2024 20:57:19 -0600 Subject: [PATCH 6/6] temp disable outputs --- modules/eks/outputs.tf | 12 ++++++------ outputs.tf | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/eks/outputs.tf b/modules/eks/outputs.tf index 37cf257..3953f2e 100644 --- a/modules/eks/outputs.tf +++ b/modules/eks/outputs.tf @@ -1,7 +1,7 @@ -output "endpoint" { - value = aws_eks_cluster.this.endpoint -} +# output "endpoint" { +# value = aws_eks_cluster.this.endpoint +# } -output "kubeconfig-certificate-authority-data" { - value = aws_eks_cluster.this.certificate_authority[0].data -} \ No newline at end of file +# output "kubeconfig-certificate-authority-data" { +# value = aws_eks_cluster.this.certificate_authority[0].data +# } \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index 6209819..021899a 100644 --- a/outputs.tf +++ b/outputs.tf @@ -7,6 +7,6 @@ output "private_subnets" { value = module.network.private_subnet_cidr_block } -output "eks_endpoint" { - value = module.eks.endpoint -} +# output "eks_endpoint" { +# value = module.eks.endpoint +# }