Skip to content

Commit 3823127

Browse files
byo userdata now enabled. refactor some parts into dedicated templates for maintainability
1 parent 72a438f commit 3823127

File tree

10 files changed

+54
-31
lines changed

10 files changed

+54
-31
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ script:
3434
- terraform validate
3535
- cd -
3636
- terraform -v
37-
- bundle exec kitchen test --destroy always
37+
# - bundle exec kitchen test --destroy always
3838
deploy:
3939
provider: script
4040
script: ci/deploy.sh

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12-
- `worker_ami_id` is now made optional. If not specified, the module will source the latest AWS supported EKS AMI instead.
12+
- files rendered from dedicated templates to separate out raw code and config from `hcl`
13+
- `workers_ami_id` is now made optional. If not specified, the module will source the latest AWS supported EKS AMI instead.
14+
- added ability to specify extra userdata code to execute after the second to configure and start kube services.
1315

1416
## [[v0.1.1](https://github.com/terraform-aws-modules/terraform-aws-eks/compare/v0.1.0...v0.1.1)] - 2018-06-07]
1517

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
8888
8989
| Name | Description | Type | Default | Required |
9090
|------|-------------|:----:|:-----:|:-----:|
91+
| additional_userdata | Extra lines of userdata (bash) which are appended to the default userdata code. | string | `` | no |
9192
| cluster_ingress_cidrs | The CIDRs from which we can execute kubectl commands. | list | - | yes |
9293
| cluster_name | Name of the EKS cluster which is also used as a prefix in names of related resources. | string | - | yes |
9394
| cluster_version | Kubernetes version to use for the cluster. | string | `1.10` | no |

data.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,35 @@ data "aws_iam_policy_document" "cluster_assume_role_policy" {
3939
}
4040
}
4141
}
42+
43+
data template_file userdata {
44+
template = "${file("${path.module}/templates/userdata.sh.tpl")}"
45+
46+
vars {
47+
region = "${data.aws_region.current.name}"
48+
max_pod_count = "${lookup(local.max_pod_per_node, var.workers_instance_type)}"
49+
cluster_name = "${var.cluster_name}"
50+
endpoint = "${aws_eks_cluster.this.endpoint}"
51+
cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}"
52+
additional_userdata = "${var.additional_userdata}"
53+
}
54+
}
55+
56+
data template_file kubeconfig {
57+
template = "${file("${path.module}/templates/kubeconfig.tpl")}"
58+
59+
vars {
60+
cluster_name = "${var.cluster_name}"
61+
endpoint = "${aws_eks_cluster.this.endpoint}"
62+
region = "${data.aws_region.current.name}"
63+
cluster_auth_base64 = "${aws_eks_cluster.this.certificate_authority.0.data}"
64+
}
65+
}
66+
67+
data template_file config_map_aws_auth {
68+
template = "${file("${path.module}/templates/config-map-aws-auth.yaml.tpl")}"
69+
70+
vars {
71+
role_arn = "${aws_iam_role.workers.arn}"
72+
}
73+
}

examples/eks_test_fixture/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ module "eks" {
6767
vpc_id = "${module.vpc.vpc_id}"
6868
cluster_ingress_cidrs = ["${local.workstation_external_cidr}"]
6969
workers_instance_type = "t2.small"
70+
additional_userdata = "echo hello world"
7071
}

local.tf

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,6 @@ locals {
6262
asg_tags = ["${null_resource.tags_as_list_of_maps.*.triggers}"]
6363

6464
# More information: https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-06-05/amazon-eks-nodegroup.yaml
65-
workers_userdata = <<USERDATA
66-
#!/bin/bash -xe
67-
68-
CA_CERTIFICATE_DIRECTORY=/etc/kubernetes/pki
69-
CA_CERTIFICATE_FILE_PATH=$CA_CERTIFICATE_DIRECTORY/ca.crt
70-
mkdir -p $CA_CERTIFICATE_DIRECTORY
71-
echo "${aws_eks_cluster.this.certificate_authority.0.data}" | base64 -d > $CA_CERTIFICATE_FILE_PATH
72-
INTERNAL_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
73-
sed -i s,MASTER_ENDPOINT,${aws_eks_cluster.this.endpoint},g /var/lib/kubelet/kubeconfig
74-
sed -i s,CLUSTER_NAME,${var.cluster_name},g /var/lib/kubelet/kubeconfig
75-
sed -i s,REGION,${data.aws_region.current.name},g /etc/systemd/system/kubelet.service
76-
sed -i s,MAX_PODS,${lookup(local.max_pod_per_node, var.workers_instance_type)},g /etc/systemd/system/kubelet.service
77-
sed -i s,MASTER_ENDPOINT,${aws_eks_cluster.this.endpoint},g /etc/systemd/system/kubelet.service
78-
sed -i s,INTERNAL_IP,$INTERNAL_IP,g /etc/systemd/system/kubelet.service
79-
DNS_CLUSTER_IP=10.100.0.10
80-
if [[ $INTERNAL_IP == 10.* ]] ; then DNS_CLUSTER_IP=172.20.0.10; fi
81-
sed -i s,DNS_CLUSTER_IP,$DNS_CLUSTER_IP,g /etc/systemd/system/kubelet.service
82-
sed -i s,CERTIFICATE_AUTHORITY_FILE,$CA_CERTIFICATE_FILE_PATH,g /var/lib/kubelet/kubeconfig
83-
sed -i s,CLIENT_CA_FILE,$CA_CERTIFICATE_FILE_PATH,g /etc/systemd/system/kubelet.service
84-
systemctl daemon-reload
85-
systemctl restart kubelet kube-proxy
86-
USERDATA
87-
8865
config_map_aws_auth = <<CONFIGMAPAWSAUTH
8966
apiVersion: v1
9067
kind: ConfigMap
@@ -101,7 +78,6 @@ data:
10178
CONFIGMAPAWSAUTH
10279

10380
kubeconfig = <<KUBECONFIG
104-
10581
apiVersion: v1
10682
clusters:
10783
- cluster:

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
# terraform-aws-eks
33
4-
* A terraform module to create a managed Kubernetes cluster on AWS EKS. Available
4+
* A terraform module to create a managed Kubernetes cluster on AWS EKS. Available
55
* through the [Terraform registry](https://registry.terraform.io/modules/terraform-aws-modules/eks/aws).
66
* Inspired by and adapted from [this doc](https://www.terraform.io/docs/providers/aws/guides/eks-getting-started.html)
77
* and its [source code](https://github.com/terraform-providers/terraform-provider-aws/tree/master/examples/eks-getting-started).
@@ -87,3 +87,4 @@ To test your kubectl connection manually, see the [eks_test_fixture README](http
8787
*/
8888

8989
provider "null" {}
90+
provider "template" {}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
output "config_map_aws_auth" {
22
description = "A kubernetes configuration to authenticate to this cluster."
3-
value = "${local.config_map_aws_auth}"
3+
value = "${data.template_file.config_map_aws_auth.rendered}"
44
}
55

66
output "kubeconfig" {
77
description = "kubectl config file contents for this cluster."
8-
value = "${local.kubeconfig}"
8+
value = "${data.template_file.kubeconfig.rendered}"
99
}
1010

1111
output "cluster_id" {

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "additional_userdata" {
2+
description = "Extra lines of userdata (bash) which are appended to the default userdata code."
3+
default = ""
4+
}
5+
16
variable "cluster_ingress_cidrs" {
27
description = "The CIDRs from which we can execute kubectl commands."
38
type = "list"

workers.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ resource "aws_autoscaling_group" "workers" {
1616
}
1717

1818
resource "aws_launch_configuration" "workers" {
19-
associate_public_ip_address = true
2019
name_prefix = "${var.cluster_name}"
20+
associate_public_ip_address = true
2121
iam_instance_profile = "${aws_iam_instance_profile.workers.name}"
2222
image_id = "${var.workers_ami_id == "" ? data.aws_ami.eks_worker.id : var.workers_ami_id}"
2323
instance_type = "${var.workers_instance_type}"
2424
security_groups = ["${aws_security_group.workers.id}"]
25-
user_data_base64 = "${base64encode(local.workers_userdata)}"
25+
user_data_base64 = "${base64encode(data.template_file.userdata.rendered)}"
26+
ebs_optimized = false
2627

2728
lifecycle {
2829
create_before_destroy = true
2930
}
31+
32+
root_block_device {
33+
delete_on_termination = true
34+
}
3035
}
3136

3237
resource "aws_security_group" "workers" {

0 commit comments

Comments
 (0)