diff --git a/eks-cluster.tf b/eks-cluster.tf index 48bef6f..62fb6f5 100644 --- a/eks-cluster.tf +++ b/eks-cluster.tf @@ -50,9 +50,12 @@ resource "aws_security_group" "cluster" { cidr_blocks = ["0.0.0.0/0"] } - tags { - Name = "terraform-eks-${var.cluster_name}" - } + tags = "${merge( + local.common_tags, + map( + "Name", "terraform-eks-${var.cluster_name}" + ) + )}" } # Allow pods to communicate with the cluster API server @@ -66,16 +69,6 @@ resource "aws_security_group_rule" "cluster-ingress-node-https" { type = "ingress" } -#resource "aws_security_group_rule" "cluster-ingress-workstation-https" { -# cidr_blocks = ["${local.workstation-external-cidr}"] -# description = "Allow workstation to communicate with the cluster API Server" -# from_port = 443 -# protocol = "tcp" -# security_group_id = "${aws_security_group.cluster.id}" -# to_port = 443 -# type = "ingress" -#} - # Create an EKS cluster resource "aws_eks_cluster" "cluster" { name = "${var.cluster_name}" diff --git a/eks-worker-nodes.tf b/eks-worker-nodes.tf index b9c9cc1..6132d18 100644 --- a/eks-worker-nodes.tf +++ b/eks-worker-nodes.tf @@ -64,12 +64,13 @@ resource "aws_security_group" "node" { cidr_blocks = ["0.0.0.0/0"] } - tags = "${ + tags = "${merge( + local.common_tags, map( - "Name", "terraform-eks-${var.cluster_name}-node", - "kubernetes.io/cluster/${var.cluster_name}", "owned", + "Name", "terraform-eks-${var.cluster_name}-node", + "kubernetes.io/cluster/${var.cluster_name}", "owned", ) - }" + )}" } # Allow worker nodes to communicate with each other @@ -102,7 +103,8 @@ data "aws_ami" "eks-worker" { } most_recent = true - owners = ["602401143452"] # Amazon + owners = ["602401143452"] # Amazon + tags = "${local.common_tags}" } # EKS currently documents this required userdata for EKS worker nodes to @@ -150,6 +152,18 @@ resource "aws_launch_configuration" "node" { } } +# Transform local.common_tags (a map) into the structure required by +# aws_autoscaling_group resources (a list of maps) +data "null_data_source" "asg_common_tags" { + count = "${length(keys(local.common_tags))}" + + inputs = { + key = "${element(keys(local.common_tags), count.index)}" + value = "${element(values(local.common_tags), count.index)}" + propagate_at_launch = true + } +} + # Create an EC2 autoscaling group for the worker nodes resource "aws_autoscaling_group" "cluster" { desired_capacity = 2 @@ -159,15 +173,11 @@ resource "aws_autoscaling_group" "cluster" { name = "terraform-eks-${var.cluster_name}" vpc_zone_identifier = ["${aws_subnet.cluster.*.id}"] - tag { - key = "Name" - value = "terraform-eks-${var.cluster_name}" - propagate_at_launch = true - } - - tag { - key = "kubernetes.io/cluster/${var.cluster_name}" - value = "owned" - propagate_at_launch = true - } + tags = ["${concat( + list( + map("key", "Name", "value", "terraform-eks-${var.cluster_name}", "propagate_at_launch", true), + map("key", "kubernetes.io/cluster/${var.cluster_name}", "value", "owned", "propagate_at_launch", true) + ), + data.null_data_source.asg_common_tags.*.outputs + )}"] } diff --git a/locals.tf b/locals.tf new file mode 100644 index 0000000..a9f8a85 --- /dev/null +++ b/locals.tf @@ -0,0 +1,5 @@ +# Define local variables +locals { + # Define the common tags for all resources + common_tags = "${var.tags}" +} diff --git a/variables.tf b/variables.tf index 5d85f12..d5b37bd 100644 --- a/variables.tf +++ b/variables.tf @@ -23,3 +23,9 @@ variable "role_arn" { type = "string" description = "IAM role to be used when accessing the cluster" } + +variable "tags" { + type = "map" + default = {} + description = "Map of tags to be applied to all resources" +} diff --git a/vpc.tf b/vpc.tf index 8fa71a3..63ad718 100644 --- a/vpc.tf +++ b/vpc.tf @@ -9,12 +9,13 @@ resource "aws_vpc" "cluster" { cidr_block = "10.0.0.0/16" - tags = "${ + tags = "${merge( map( "Name", "terraform-eks-${var.cluster_name}", "kubernetes.io/cluster/${var.cluster_name}", "shared", - ) - }" + ), + local.common_tags + )}" } resource "aws_subnet" "cluster" { @@ -24,20 +25,22 @@ resource "aws_subnet" "cluster" { cidr_block = "10.0.${count.index}.0/24" vpc_id = "${aws_vpc.cluster.id}" - tags = "${ + tags = "${merge( map( "Name", "terraform-eks-${var.cluster_name}", "kubernetes.io/cluster/${var.cluster_name}", "shared", - ) - }" + ), + local.common_tags + )}" } resource "aws_internet_gateway" "cluster" { vpc_id = "${aws_vpc.cluster.id}" - tags { - Name = "terraform-eks-${var.cluster_name}" - } + tags = "${merge( + map("Name", "terraform-eks-${var.cluster_name}"), + local.common_tags + )}" } resource "aws_route_table" "cluster" {