Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
terraform-aws-eks/eks-cluster.tf
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
88 lines (77 sloc)
2.54 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# EKS Cluster Resources | |
# * IAM Role to allow EKS service to manage other AWS services | |
# * EC2 Security Group to allow networking traffic with EKS cluster | |
# * EKS Cluster | |
# | |
# Create an IAM role for EKS to manage other AWS resources on our behalf | |
resource "aws_iam_role" "eksServiceRole" { | |
name = "terraform-eks-${var.cluster_name}-eksServiceRole" | |
assume_role_policy = <<POLICY | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": "eks.amazonaws.com" | |
}, | |
"Action": "sts:AssumeRole" | |
} | |
] | |
} | |
POLICY | |
} | |
# Attach Amazon's managed AmazonEKSClusterPolicy to our EKS service role | |
resource "aws_iam_role_policy_attachment" "AmazonEKSClusterPolicy" { | |
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy" | |
role = "${aws_iam_role.eksServiceRole.name}" | |
} | |
# Attach Amazon's managed AmazonEKSServicePolicy to our EKS service role | |
resource "aws_iam_role_policy_attachment" "AmazonEKSServicePolicy" { | |
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy" | |
role = "${aws_iam_role.eksServiceRole.name}" | |
} | |
# Create a security group for cluster internal communication | |
resource "aws_security_group" "cluster" { | |
name = "terraform-eks-${var.cluster_name}-cluster" | |
description = "Cluster communication with worker nodes" | |
vpc_id = "${aws_vpc.cluster.id}" | |
egress { | |
from_port = 0 | |
to_port = 0 | |
protocol = "-1" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
tags = "${merge( | |
local.common_tags, | |
map( | |
"Name", "terraform-eks-${var.cluster_name}" | |
) | |
)}" | |
} | |
# Allow pods to communicate with the cluster API server | |
resource "aws_security_group_rule" "cluster-ingress-node-https" { | |
description = "Allow pods to communicate with the cluster API Server" | |
from_port = 443 | |
protocol = "tcp" | |
security_group_id = "${aws_security_group.cluster.id}" | |
source_security_group_id = "${aws_security_group.node.id}" | |
to_port = 443 | |
type = "ingress" | |
} | |
# Create an EKS cluster | |
resource "aws_eks_cluster" "cluster" { | |
name = "${var.cluster_name}" | |
role_arn = "${aws_iam_role.eksServiceRole.arn}" | |
vpc_config { | |
security_group_ids = ["${aws_security_group.cluster.id}"] | |
subnet_ids = ["${aws_subnet.cluster.*.id}"] | |
} | |
# Ensure the policies are attached to our EKS service role before creating | |
# the EKS cluster | |
depends_on = [ | |
"aws_iam_role_policy_attachment.AmazonEKSClusterPolicy", | |
"aws_iam_role_policy_attachment.AmazonEKSServicePolicy", | |
] | |
} |