mirror of
https://github.com/terraform-aws-modules/terraform-aws-eks.git
synced 2025-09-09 19:32:58 +08:00
db9bb0b482
In order to ensure proper ordering when running terraform destroy. This will block Terraform from removing up security group rules before the cluster has finished its clean up chores.
160 lines
5.4 KiB
Terraform
160 lines
5.4 KiB
Terraform
resource "aws_cloudwatch_log_group" "this" {
|
|
count = length(var.cluster_enabled_log_types) > 0 && var.create_eks ? 1 : 0
|
|
name = "/aws/eks/${var.cluster_name}/cluster"
|
|
retention_in_days = var.cluster_log_retention_in_days
|
|
kms_key_id = var.cluster_log_kms_key_id
|
|
tags = var.tags
|
|
}
|
|
|
|
resource "aws_eks_cluster" "this" {
|
|
count = var.create_eks ? 1 : 0
|
|
name = var.cluster_name
|
|
enabled_cluster_log_types = var.cluster_enabled_log_types
|
|
role_arn = local.cluster_iam_role_arn
|
|
version = var.cluster_version
|
|
tags = var.tags
|
|
|
|
vpc_config {
|
|
security_group_ids = compact([local.cluster_security_group_id])
|
|
subnet_ids = var.subnets
|
|
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
|
|
}
|
|
|
|
timeouts {
|
|
create = var.cluster_create_timeout
|
|
delete = var.cluster_delete_timeout
|
|
}
|
|
|
|
dynamic encryption_config {
|
|
for_each = toset(var.cluster_encryption_config)
|
|
|
|
content {
|
|
provider {
|
|
key_arn = encryption_config.value["provider_key_arn"]
|
|
}
|
|
resources = encryption_config.value["resources"]
|
|
}
|
|
}
|
|
|
|
depends_on = [
|
|
aws_security_group_rule.cluster_egress_internet,
|
|
aws_security_group_rule.cluster_https_worker_ingress,
|
|
aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy,
|
|
aws_iam_role_policy_attachment.cluster_AmazonEKSServicePolicy,
|
|
aws_cloudwatch_log_group.this
|
|
]
|
|
}
|
|
|
|
resource "aws_security_group_rule" "cluster_private_access" {
|
|
count = var.create_eks && var.cluster_endpoint_private_access && var.cluster_endpoint_public_access == false ? 1 : 0
|
|
type = "ingress"
|
|
from_port = 443
|
|
to_port = 443
|
|
protocol = "tcp"
|
|
cidr_blocks = var.cluster_endpoint_private_access_cidrs
|
|
|
|
security_group_id = aws_eks_cluster.this[0].vpc_config[0].cluster_security_group_id
|
|
}
|
|
|
|
|
|
resource "null_resource" "wait_for_cluster" {
|
|
count = var.create_eks && var.manage_aws_auth ? 1 : 0
|
|
|
|
depends_on = [
|
|
aws_eks_cluster.this[0],
|
|
aws_security_group_rule.cluster_private_access,
|
|
]
|
|
|
|
provisioner "local-exec" {
|
|
command = var.wait_for_cluster_cmd
|
|
interpreter = var.wait_for_cluster_interpreter
|
|
environment = {
|
|
ENDPOINT = aws_eks_cluster.this[0].endpoint
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "aws_security_group" "cluster" {
|
|
count = var.cluster_create_security_group && var.create_eks ? 1 : 0
|
|
name_prefix = var.cluster_name
|
|
description = "EKS cluster security group."
|
|
vpc_id = var.vpc_id
|
|
tags = merge(
|
|
var.tags,
|
|
{
|
|
"Name" = "${var.cluster_name}-eks_cluster_sg"
|
|
},
|
|
)
|
|
}
|
|
|
|
resource "aws_security_group_rule" "cluster_egress_internet" {
|
|
count = var.cluster_create_security_group && var.create_eks ? 1 : 0
|
|
description = "Allow cluster egress access to the Internet."
|
|
protocol = "-1"
|
|
security_group_id = local.cluster_security_group_id
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
from_port = 0
|
|
to_port = 0
|
|
type = "egress"
|
|
}
|
|
|
|
resource "aws_security_group_rule" "cluster_https_worker_ingress" {
|
|
count = var.cluster_create_security_group && var.create_eks ? 1 : 0
|
|
description = "Allow pods to communicate with the EKS cluster API."
|
|
protocol = "tcp"
|
|
security_group_id = local.cluster_security_group_id
|
|
source_security_group_id = local.worker_security_group_id
|
|
from_port = 443
|
|
to_port = 443
|
|
type = "ingress"
|
|
}
|
|
|
|
resource "aws_iam_role" "cluster" {
|
|
count = var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
|
|
name_prefix = var.cluster_name
|
|
assume_role_policy = data.aws_iam_policy_document.cluster_assume_role_policy.json
|
|
permissions_boundary = var.permissions_boundary
|
|
path = var.iam_path
|
|
force_detach_policies = true
|
|
tags = var.tags
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" {
|
|
count = var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
|
|
policy_arn = "${local.policy_arn_prefix}/AmazonEKSClusterPolicy"
|
|
role = local.cluster_iam_role_name
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSServicePolicy" {
|
|
count = var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
|
|
policy_arn = "${local.policy_arn_prefix}/AmazonEKSServicePolicy"
|
|
role = local.cluster_iam_role_name
|
|
}
|
|
|
|
/*
|
|
Adding a policy to cluster IAM role that allow permissions
|
|
required to create AWSServiceRoleForElasticLoadBalancing service-linked role by EKS during ELB provisioning
|
|
*/
|
|
|
|
data "aws_iam_policy_document" "cluster_elb_sl_role_creation" {
|
|
count = var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
|
|
|
|
statement {
|
|
effect = "Allow"
|
|
actions = [
|
|
"ec2:DescribeAccountAttributes",
|
|
"ec2:DescribeInternetGateways"
|
|
]
|
|
resources = ["*"]
|
|
}
|
|
}
|
|
|
|
resource "aws_iam_role_policy" "cluster_elb_sl_role_creation" {
|
|
count = var.manage_cluster_iam_resources && var.create_eks ? 1 : 0
|
|
name_prefix = "${var.cluster_name}-elb-sl-role-creation"
|
|
role = local.cluster_iam_role_name
|
|
policy = data.aws_iam_policy_document.cluster_elb_sl_role_creation[0].json
|
|
}
|