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 diff --git a/main.tf b/main.tf index 8976331..dd6134e 100644 --- a/main.tf +++ b/main.tf @@ -3,4 +3,76 @@ module "network" { name = var.name create_ngw = false vpc_cidr = "10.0.0.0/16" -} \ No newline at end of file +} +module "eks" { + source = "./modules/eks" + create_eks = false + cluster_name = "${var.name}-cluster" + cluster_version = "1.30" + + 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 = {} +# } + +# 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 = { +# 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/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 new file mode 100644 index 0000000..8ace1b4 --- /dev/null +++ b/modules/eks/main.tf @@ -0,0 +1,18 @@ +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 + + 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, + ] +} + diff --git a/modules/eks/outputs.tf b/modules/eks/outputs.tf new file mode 100644 index 0000000..3953f2e --- /dev/null +++ b/modules/eks/outputs.tf @@ -0,0 +1,7 @@ +# 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 diff --git a/modules/eks/variables.tf b/modules/eks/variables.tf new file mode 100644 index 0000000..dcea10e --- /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/outputs.tf b/outputs.tf index 121a399..021899a 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 +# } diff --git a/terraform.tfvars b/terraform.tfvars index c44606e..5015d3c 100644 --- a/terraform.tfvars +++ b/terraform.tfvars @@ -1,2 +1,3 @@ -name = "playground" -region = "us-west-2" \ No newline at end of file +name = "playground" +region = "us-west-2" +create_eks = true diff --git a/variables.tf b/variables.tf index f8d95d5..a80f67a 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 +}