Skip to content

Commit

Permalink
Allow people to override user_data and name_prefix. (#9)
Browse files Browse the repository at this point in the history
* Allow people to override user_data and name_prefix.

* Forgot to add the data file.

* Address PR comments. Update the README accordingly.

* Typo.

* Formatting.
  • Loading branch information
joestump authored and tfhartmann committed Jul 26, 2017
1 parent 5784dfa commit 3341bd7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 54 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ Module Input Variables
----------------------
#### Required
- `name` - ECS cluster name
- `key_name`
- `key_path`
- `subnet_id` - list of subnets
- `vpc_id`
- `key_name` - An EC2 key pair name
- `subnet_id` - A list of subnet IDs
- `vpc_id` - The VPC ID to place the cluster in

#### Optional
- `region` - AWS Region - defaults to us-east-1
Expand All @@ -31,11 +30,12 @@ extra_tags = [
},
]
```
- `allowed_cidr_blocks` - List of subnets to allow into the ECS Security Group. Defaults to ["0.0.0.0/0"]
- `ami` - specific AMI image to use, eg `ami-95f8d2f3`.
- `ami_version` - specific version of the Amazon ECS AMI to use, eg `2016.09`
- `allowed_cidr_blocks` - List of subnets to allow into the ECS Security Group. Defaults to `["0.0.0.0/0"]`.
- `ami` - A specific AMI image to use, eg `ami-95f8d2f3`. Defaults to the latest ECS optimized Amazon Linux AMI.
- `ami_version` - Specific version of the Amazon ECS AMI to use (e.g. `2016.09`). Defaults to `*`. Ignored if `ami` is specified.
- `heartbeat_timeout` - Heartbeat Timeout setting for how long it takes for the graceful shutodwn hook takes to timeout. This is useful when deploying clustered applications like consul that benifit from having a deploy between autoscaling create/destroy actions. Defaults to 180"
- `security_group_ids` - a list of security group IDs to apply to the launch configuration
- `user_data` - The instance user data (e.g. a `cloud-init` config) to use in the `aws_launch_configuration`

Usage
-----
Expand All @@ -54,12 +54,16 @@ module "ecs-cluster" {
Outputs
=======

- `cluster_id` - ECS Cluster id for use in ECS task and service definitions
- `cluster_id` - _(String)_ ECS Cluster id for use in ECS task and service definitions.
- `autoscaling_group` _(Map)_ A map with keys `id`, `name`, and `arn` of the `aws_autoscaling_group` created.

Authors
=======

[Tim Hartmann](https://github.com/tfhartmann)
* [Tim Hartmann](https://github.com/tfhartmann)
* [Joe Stump](https://github.com/joestump)

License
=======

[MIT](LICENSE)
8 changes: 4 additions & 4 deletions graceful_shutdown.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# https://circleci.com/blog/graceful-shutdown-using-aws/

resource "aws_autoscaling_lifecycle_hook" "graceful_shutdown_asg_hook" {
name = "graceful_shutdown_asg"
name = "graceful_shutdown_asg"
autoscaling_group_name = "${aws_autoscaling_group.ecs.name}"
default_result = "CONTINUE"
heartbeat_timeout = "${var.heartbeat_timeout}"
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
default_result = "CONTINUE"
heartbeat_timeout = "${var.heartbeat_timeout}"
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
}
15 changes: 8 additions & 7 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
resource "aws_iam_instance_profile" "ecs_profile" {
name = "tf-created-AmazonECSContainerProfile-${var.name}"
role = "${aws_iam_role.ecs-role.name}"
name = "tf-created-AmazonECSContainerProfile-${var.name}"
role = "${aws_iam_role.ecs-role.name}"
}

resource "aws_iam_role" "ecs-role" {
name = "tf-AmazonECSInstanceRole-${var.name}"
name = "tf-AmazonECSInstanceRole-${var.name}"

assume_role_policy = <<EOF
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
Expand All @@ -30,9 +30,10 @@ EOF
# "autoscaling:Describe*",

resource "aws_iam_policy" "ecs-policy" {
name = "tf-created-AmazonECSContainerInstancePolicy-${var.name}"
description = "A terraform created policy for ECS"
policy = <<EOF
name = "tf-created-AmazonECSContainerInstancePolicy-${var.name}"
description = "A terraform created policy for ECS"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
Expand Down
55 changes: 34 additions & 21 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@ data "aws_ami" "ecs_ami" {
most_recent = true

filter {
name = "owner-alias"
name = "owner-alias"
values = ["amazon"]
}

filter {
name = "name"
name = "name"
values = ["amzn-ami-${var.ami_version}-amazon-ecs-optimized"]
}
}

data "template_file" "user_data" {
template = "${file("${path.module}/templates/user_data.tpl")}"

vars {
cluster_name = "${aws_ecs_cluster.cluster.name}"
docker_storage_size = "${var.docker_storage_size}"
dockerhub_token = "${var.dockerhub_token}"
dockerhub_email = "${var.dockerhub_email}"
}
}

resource "aws_launch_configuration" "ecs" {
name_prefix = "ecs-${var.name}-"
image_id = "${var.ami == "" ? format("%s", data.aws_ami.ecs_ami.id) : var.ami}" # Workaround until 0.9.6
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
iam_instance_profile = "${aws_iam_instance_profile.ecs_profile.name}"
security_groups = ["${aws_security_group.ecs.id}", "${var.security_group_ids}"]
name_prefix = "${coalesce(var.name_prefix, "ecs-${var.name}-")}"
image_id = "${var.ami == "" ? format("%s", data.aws_ami.ecs_ami.id) : var.ami}" # Workaround until 0.9.6
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
iam_instance_profile = "${aws_iam_instance_profile.ecs_profile.name}"
security_groups = "${concat([aws_security_group.ecs.id], var.security_group_ids)}"
associate_public_ip_address = "${var.associate_public_ip_address}"

ebs_block_device {
device_name = "/dev/xvdcz"
volume_size = "${var.docker_storage_size}"
volume_type = "gp2"
device_name = "/dev/xvdcz"
volume_size = "${var.docker_storage_size}"
volume_type = "gp2"
delete_on_termination = true
}
user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${aws_ecs_cluster.cluster.name} >> /etc/ecs/ecs.config
echo 'OPTIONS="$${OPTIONS} --storage-opt dm.basesize=${var.docker_storage_size}G"' >> /etc/sysconfig/docker
/etc/init.d/docker restart
echo ECS_ENGINE_AUTH_TYPE=dockercfg >> /etc/ecs/ecs.config
echo 'ECS_ENGINE_AUTH_DATA={"https://index.docker.io/v1/": { "auth": "${var.dockerhub_token}", "email": "${var.dockerhub_email}"}}' >> /etc/ecs/ecs.config
EOF

user_data = "${coalesce(var.user_data, data.template_file.user_data.rendered)}"

lifecycle {
create_before_destroy = true
}
Expand All @@ -47,12 +54,15 @@ resource "aws_autoscaling_group" "ecs" {
max_size = 10
desired_capacity = "${var.servers}"
termination_policies = ["OldestLaunchConfiguration", "ClosestToNextInstanceHour", "Default"]

tags = [{
key = "Name"
value = "${var.name} ${var.tagName}"
key = "Name"
value = "${var.name} ${var.tagName}"
propagate_at_launch = true
}]

tags = ["${var.extra_tags}"]

lifecycle {
create_before_destroy = true
}
Expand All @@ -62,18 +72,21 @@ resource "aws_security_group" "ecs" {
name = "ecs-sg-${var.name}"
description = "Container Instance Allowed Ports"
vpc_id = "${var.vpc_id}"

ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = "${var.allowed_cidr_blocks}"
}

ingress {
from_port = 0
to_port = 65535
protocol = "udp"
cidr_blocks = "${var.allowed_cidr_blocks}"
}

egress {
from_port = 0
to_port = 0
Expand Down
13 changes: 8 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#output "ecs_cluster" {
# value = ["${aws_subnet.private.*.id}"]
#}

output "cluster_id" {
value = "${aws_ecs_cluster.cluster.id}"
}
# ECS servers

output "autoscaling_group" {
value = {
id = "${aws_autoscaling_group.ecs.id}"
name = "${aws_autoscaling_group.ecs.name}"
arn = "${aws_autoscaling_group.ecs.arn}"
}
}
6 changes: 6 additions & 0 deletions templates/user_data.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
echo ECS_CLUSTER=${cluster_name} >> /etc/ecs/ecs.config
echo 'OPTIONS="$${OPTIONS} --storage-opt dm.basesize=${docker_storage_size}G"' >> /etc/sysconfig/docker
/etc/init.d/docker restart
echo ECS_ENGINE_AUTH_TYPE=dockercfg >> /etc/ecs/ecs.config
echo 'ECS_ENGINE_AUTH_DATA={"https://index.docker.io/v1/": { "auth": "${dockerhub_token}", "email": "${var.dockerhub_email}"}}' >> /etc/ecs/ecs.config
17 changes: 9 additions & 8 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ variable "allowed_cidr_blocks" {
description = "List of subnets to allow into the ECS Security Group. Defaults to ['0.0.0.0/0']"
}

variable "name_prefix" {
default = ""
}

variable "ami" {
default = ""
}
Expand All @@ -12,6 +16,10 @@ variable "ami_version" {
default = "*"
}

variable "user_data" {
default = ""
}

variable "docker_storage_size" {
default = "22"
description = "EBS Volume size in Gib that the ECS Instance uses for Docker images and metadata "
Expand All @@ -30,6 +38,7 @@ variable "dockerhub_token" {
variable "extra_tags" {
default = []
}

variable "heartbeat_timeout" {
description = "Heartbeat Timeout setting for how long it takes for the graceful shutodwn hook takes to timeout. This is useful when deploying clustered applications like consul that benifit from having a deploy between autoscaling create/destroy actions. Defaults to 180"
default = "180"
Expand All @@ -42,14 +51,6 @@ variable "instance_type" {

variable "key_name" {
description = "SSH key name in your AWS account for AWS instances."
default = "amazonhosts"
}

variable "key_path" {
description = "Path to the private key specified by key_name."
default = {
key_path = "/Users/alaric/amazonhosts.pem"
}
}

variable "name" {
Expand Down

0 comments on commit 3341bd7

Please sign in to comment.