Skip to content

Commit 4043504

Browse files
authored
Add eks module. Add eks-backing-services-peering module (#42)
* Add EKS modules * Update EKS modules * Update EKS modules * Update EKS modules * Add triggers
1 parent 31116b6 commit 4043504

File tree

11 files changed

+471
-1
lines changed

11 files changed

+471
-1
lines changed

aws/backing-services/elasticsearch.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ locals {
6363
}
6464

6565
module "elasticsearch" {
66-
source = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=tags/0.1.1"
66+
source = "git::https://github.com/cloudposse/terraform-aws-elasticsearch.git?ref=tags/0.1.2"
6767
namespace = "${var.namespace}"
6868
stage = "${var.stage}"
6969
name = "${var.ELASTICSEARCH_NAME}"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Lookup the EKS VPC
2+
data "aws_vpc" "eks_vpc" {
3+
filter {
4+
name = "tag:Name"
5+
values = ["${var.namespace}${var.delimiter}${var.stage}${var.delimiter}${var.name}"]
6+
}
7+
}
8+
9+
# Lookup the backing services VPC
10+
data "aws_vpc" "backing_services_vpc" {
11+
filter {
12+
name = "tag:Name"
13+
values = ["${var.namespace}${var.delimiter}${var.stage}${var.delimiter}backing-services"]
14+
}
15+
}
16+
17+
module "vpc_peering" {
18+
source = "git::https://github.com/cloudposse/terraform-aws-vpc-peering.git?ref=tags/0.1.2"
19+
namespace = "${var.namespace}"
20+
stage = "${var.stage}"
21+
name = "${var.name}"
22+
delimiter = "${var.delimiter}"
23+
attributes = ["${compact(concat(var.attributes, list("peering")))}"]
24+
tags = "${var.tags}"
25+
requestor_vpc_id = "${data.aws_vpc.eks_vpc.id}"
26+
acceptor_vpc_id = "${data.aws_vpc.backing_services_vpc.id}"
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "vpc_peering_connection_id" {
2+
value = "${module.vpc_peering.connection_id}"
3+
description = "VPC peering connection ID"
4+
}
5+
6+
output "vpc_peering_accept_status" {
7+
value = "${module.vpc_peering.accept_status}"
8+
description = "The status of the VPC peering connection request"
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace="cp"
2+
stage="staging"
3+
name="eks"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
variable "namespace" {
2+
type = "string"
3+
description = "Namespace, which could be your organization name, e.g. 'eg' or 'cp'"
4+
}
5+
6+
variable "stage" {
7+
type = "string"
8+
description = "Stage, e.g. 'prod', 'staging', 'dev' or 'testing'"
9+
}
10+
11+
variable "name" {
12+
type = "string"
13+
default = "eks"
14+
description = "Solution name, e.g. 'app' or 'cluster'"
15+
}
16+
17+
variable "delimiter" {
18+
type = "string"
19+
default = "-"
20+
description = "Delimiter to be used between `name`, `namespace`, `stage`, etc."
21+
}
22+
23+
variable "attributes" {
24+
type = "list"
25+
default = []
26+
description = "Additional attributes (e.g. `1`)"
27+
}
28+
29+
variable "tags" {
30+
type = "map"
31+
default = {}
32+
description = "Additional tags (e.g. `map('BusinessUnit`,`XYZ`)"
33+
}

aws/eks/eks.tf

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
module "label" {
2+
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.6"
3+
namespace = "${var.namespace}"
4+
name = "${var.name}"
5+
stage = "${var.stage}"
6+
delimiter = "${var.delimiter}"
7+
attributes = "${var.attributes}"
8+
tags = "${var.tags}"
9+
enabled = "${var.enabled}"
10+
}
11+
12+
locals {
13+
# The usage of the specific kubernetes.io/cluster/* resource tags below are required
14+
# for EKS and Kubernetes to discover and manage networking resources
15+
# https://www.terraform.io/docs/providers/aws/guides/eks-getting-started.html#base-vpc-networking
16+
tags = "${merge(var.tags, map("kubernetes.io/cluster/${module.label.id}", "shared"))}"
17+
}
18+
19+
data "aws_availability_zones" "available" {}
20+
21+
module "vpc" {
22+
source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.3.4"
23+
namespace = "${var.namespace}"
24+
stage = "${var.stage}"
25+
name = "${var.name}"
26+
attributes = "${var.attributes}"
27+
tags = "${local.tags}"
28+
cidr_block = "${var.vpc_cidr_block}"
29+
}
30+
31+
module "subnets" {
32+
source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/0.3.6"
33+
availability_zones = ["${data.aws_availability_zones.available.names}"]
34+
namespace = "${var.namespace}"
35+
stage = "${var.stage}"
36+
name = "${var.name}"
37+
attributes = "${var.attributes}"
38+
tags = "${local.tags}"
39+
region = "${var.region}"
40+
vpc_id = "${module.vpc.vpc_id}"
41+
igw_id = "${module.vpc.igw_id}"
42+
cidr_block = "${module.vpc.vpc_cidr_block}"
43+
nat_gateway_enabled = "true"
44+
}
45+
46+
module "eks_cluster" {
47+
source = "git::https://github.com/cloudposse/terraform-aws-eks-cluster.git?ref=tags/0.1.1"
48+
namespace = "${var.namespace}"
49+
stage = "${var.stage}"
50+
name = "${var.name}"
51+
attributes = "${var.attributes}"
52+
tags = "${var.tags}"
53+
vpc_id = "${module.vpc.vpc_id}"
54+
subnet_ids = ["${module.subnets.public_subnet_ids}"]
55+
allowed_security_groups = ["${distinct(compact(concat(var.allowed_security_groups_cluster, list(module.eks_workers.security_group_id))))}"]
56+
allowed_cidr_blocks = ["${var.allowed_cidr_blocks_cluster}"]
57+
enabled = "${var.enabled}"
58+
}
59+
60+
module "eks_workers" {
61+
source = "git::https://github.com/cloudposse/terraform-aws-eks-workers.git?ref=tags/0.1.1"
62+
namespace = "${var.namespace}"
63+
stage = "${var.stage}"
64+
name = "${var.name}"
65+
attributes = "${var.attributes}"
66+
tags = "${var.tags}"
67+
image_id = "${var.image_id}"
68+
eks_worker_ami_name_filter = "${var.eks_worker_ami_name_filter}"
69+
instance_type = "${var.instance_type}"
70+
vpc_id = "${module.vpc.vpc_id}"
71+
subnet_ids = ["${module.subnets.public_subnet_ids}"]
72+
health_check_type = "${var.health_check_type}"
73+
min_size = "${var.min_size}"
74+
max_size = "${var.max_size}"
75+
wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}"
76+
associate_public_ip_address = "${var.associate_public_ip_address}"
77+
cluster_name = "${module.eks_cluster.eks_cluster_id}"
78+
cluster_endpoint = "${module.eks_cluster.eks_cluster_endpoint}"
79+
cluster_certificate_authority_data = "${module.eks_cluster.eks_cluster_certificate_authority_data}"
80+
cluster_security_group_id = "${module.eks_cluster.security_group_id}"
81+
allowed_security_groups = ["${var.allowed_security_groups_workers}"]
82+
allowed_cidr_blocks = ["${var.allowed_cidr_blocks_workers}"]
83+
enabled = "${var.enabled}"
84+
85+
# Auto-scaling policies and CloudWatch metric alarms
86+
autoscaling_policies_enabled = "${var.autoscaling_policies_enabled}"
87+
cpu_utilization_high_threshold_percent = "${var.cpu_utilization_high_threshold_percent}"
88+
cpu_utilization_low_threshold_percent = "${var.cpu_utilization_low_threshold_percent}"
89+
}

aws/eks/kubectl.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
locals {
2+
kubeconfig_filename = "${path.module}/kubeconfig${var.delimiter}${module.eks_cluster.eks_cluster_id}.yaml"
3+
config_map_aws_auth_filename = "${path.module}/config-map-aws-auth${var.delimiter}${module.eks_cluster.eks_cluster_id}.yaml"
4+
}
5+
6+
resource "local_file" "kubeconfig" {
7+
count = "${var.enabled == "true" && var.apply_config_map_aws_auth == "true" ? 1 : 0}"
8+
content = "${module.eks_cluster.kubeconfig}"
9+
filename = "${local.kubeconfig_filename}"
10+
}
11+
12+
resource "local_file" "config_map_aws_auth" {
13+
count = "${var.enabled == "true" && var.apply_config_map_aws_auth == "true" ? 1 : 0}"
14+
content = "${module.eks_workers.config_map_aws_auth}"
15+
filename = "${local.config_map_aws_auth_filename}"
16+
}
17+
18+
resource "null_resource" "apply_config_map_aws_auth" {
19+
count = "${var.enabled == "true" && var.apply_config_map_aws_auth == "true" ? 1 : 0}"
20+
21+
provisioner "local-exec" {
22+
command = "kubectl apply -f ${local.config_map_aws_auth_filename} --kubeconfig ${local.kubeconfig_filename}"
23+
}
24+
25+
triggers {
26+
kubeconfig_rendered = "${module.eks_cluster.kubeconfig}"
27+
config_map_aws_auth_rendered = "${module.eks_workers.config_map_aws_auth}"
28+
}
29+
}

aws/eks/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_version = ">= 0.11.2"
3+
4+
backend "s3" {}
5+
}
6+
7+
variable "aws_assume_role_arn" {
8+
type = "string"
9+
}
10+
11+
provider "aws" {
12+
assume_role {
13+
role_arn = "${var.aws_assume_role_arn}"
14+
}
15+
}

aws/eks/outputs.tf

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
output "kubeconfig" {
2+
description = "`kubeconfig` configuration to connect to the cluster using `kubectl`. https://www.terraform.io/docs/providers/aws/guides/eks-getting-started.html#obtaining-kubectl-configuration-from-terraform"
3+
value = "${module.eks_cluster.kubeconfig}"
4+
}
5+
6+
output "config_map_aws_auth" {
7+
description = "Kubernetes ConfigMap configuration to allow the worker nodes to join the EKS cluster. https://www.terraform.io/docs/providers/aws/guides/eks-getting-started.html#required-kubernetes-configuration-to-join-worker-nodes"
8+
value = "${module.eks_workers.config_map_aws_auth}"
9+
}
10+
11+
output "eks_cluster_security_group_id" {
12+
description = "ID of the EKS cluster Security Group"
13+
value = "${module.eks_cluster.security_group_id}"
14+
}
15+
16+
output "eks_cluster_security_group_arn" {
17+
description = "ARN of the EKS cluster Security Group"
18+
value = "${module.eks_cluster.security_group_arn}"
19+
}
20+
21+
output "eks_cluster_security_group_name" {
22+
description = "Name of the EKS cluster Security Group"
23+
value = "${module.eks_cluster.security_group_name}"
24+
}
25+
26+
output "eks_cluster_id" {
27+
description = "The name of the cluster"
28+
value = "${module.eks_cluster.eks_cluster_id}"
29+
}
30+
31+
output "eks_cluster_arn" {
32+
description = "The Amazon Resource Name (ARN) of the cluster"
33+
value = "${module.eks_cluster.eks_cluster_arn}"
34+
}
35+
36+
output "eks_cluster_certificate_authority_data" {
37+
description = "The base64 encoded certificate data required to communicate with the cluster"
38+
value = "${module.eks_cluster.eks_cluster_certificate_authority_data}"
39+
}
40+
41+
output "eks_cluster_endpoint" {
42+
description = "The endpoint for the Kubernetes API server"
43+
value = "${module.eks_cluster.eks_cluster_endpoint}"
44+
}
45+
46+
output "eks_cluster_version" {
47+
description = "The Kubernetes server version of the cluster"
48+
value = "${module.eks_cluster.eks_cluster_version}"
49+
}
50+
51+
output "workers_launch_template_id" {
52+
description = "ID of the launch template"
53+
value = "${module.eks_workers.launch_template_id}"
54+
}
55+
56+
output "workers_launch_template_arn" {
57+
description = "ARN of the launch template"
58+
value = "${module.eks_workers.launch_template_arn}"
59+
}
60+
61+
output "workers_autoscaling_group_id" {
62+
description = "The AutoScaling Group ID"
63+
value = "${module.eks_workers.autoscaling_group_id}"
64+
}
65+
66+
output "workers_autoscaling_group_name" {
67+
description = "The AutoScaling Group name"
68+
value = "${module.eks_workers.autoscaling_group_name}"
69+
}
70+
71+
output "workers_autoscaling_group_arn" {
72+
description = "ARN of the AutoScaling Group"
73+
value = "${module.eks_workers.autoscaling_group_arn}"
74+
}
75+
76+
output "workers_autoscaling_group_min_size" {
77+
description = "The minimum size of the AutoScaling Group"
78+
value = "${module.eks_workers.autoscaling_group_min_size}"
79+
}
80+
81+
output "workers_autoscaling_group_max_size" {
82+
description = "The maximum size of the AutoScaling Group"
83+
value = "${module.eks_workers.autoscaling_group_max_size}"
84+
}
85+
86+
output "workers_autoscaling_group_desired_capacity" {
87+
description = "The number of Amazon EC2 instances that should be running in the group"
88+
value = "${module.eks_workers.autoscaling_group_desired_capacity}"
89+
}
90+
91+
output "workers_autoscaling_group_default_cooldown" {
92+
description = "Time between a scaling activity and the succeeding scaling activity"
93+
value = "${module.eks_workers.autoscaling_group_default_cooldown}"
94+
}
95+
96+
output "workers_autoscaling_group_health_check_grace_period" {
97+
description = "Time after instance comes into service before checking health"
98+
value = "${module.eks_workers.autoscaling_group_health_check_grace_period}"
99+
}
100+
101+
output "workers_autoscaling_group_health_check_type" {
102+
description = "`EC2` or `ELB`. Controls how health checking is done"
103+
value = "${module.eks_workers.autoscaling_group_health_check_type}"
104+
}
105+
106+
output "workers_security_group_id" {
107+
description = "ID of the worker nodes Security Group"
108+
value = "${module.eks_workers.security_group_id}"
109+
}
110+
111+
output "workers_security_group_arn" {
112+
description = "ARN of the worker nodes Security Group"
113+
value = "${module.eks_workers.security_group_arn}"
114+
}
115+
116+
output "workers_security_group_name" {
117+
description = "Name of the worker nodes Security Group"
118+
value = "${module.eks_workers.security_group_name}"
119+
}

aws/eks/terraform.tfvars.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace="cp"
2+
stage="staging"
3+
region="us-west-2"

0 commit comments

Comments
 (0)