Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tag resources with common_tags
  • Loading branch information
dshafer committed Aug 20, 2018
1 parent b95ca2e commit 972fab4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
19 changes: 6 additions & 13 deletions eks-cluster.tf
Expand Up @@ -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
Expand All @@ -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}"
Expand Down
42 changes: 26 additions & 16 deletions eks-worker-nodes.tf
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
)}"]
}
5 changes: 5 additions & 0 deletions locals.tf
@@ -0,0 +1,5 @@
# Define local variables
locals {
# Define the common tags for all resources
common_tags = "${var.tags}"
}
6 changes: 6 additions & 0 deletions variables.tf
Expand Up @@ -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"
}
21 changes: 12 additions & 9 deletions vpc.tf
Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down

0 comments on commit 972fab4

Please sign in to comment.