├── LICENSE ├── README.md ├── ecs-autoscaling.tf ├── launch-configuration.tf ├── outputs.tf └── variables.tf /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Robots and Pencils 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS ECS Autoscaling Group Terraform module 2 | 3 | A terraform module that creates an EC2 Launch Configuration and Autoscaling Group 4 | and configures EC2 instances for an ECS cluster with private Docker registry credentials. 5 | 6 | ### Using 7 | 8 | Create an ECS cluster first and use the name as the cluster for the `cluster_name` variable value. 9 | 10 | The default AMI is the [Amazon ECS-Optimized Amazon Linux AMI](https://aws.amazon.com/marketplace/pp/B00U6QTYI2) for `us-east-1`. 11 | 12 | Look in the `variables.tf` file for configuration options. Here's a sample config 13 | with sensitive details changed to protect the innocent. 14 | 15 | ```json 16 | module "ecs-autoscaling" { 17 | source = "git@github.com:RobotsAndPencils/terraform-ecs-autoscaling.git" 18 | cluster_name = "terraform_testing" 19 | key_name = "staging" 20 | instance_type = "t2.micro" 21 | region = "us-east-1" 22 | availability_zones = "us-east-1a,us-east-1c,us-east-1d" 23 | subnet_ids = "subnet-c5493a9e,subnet-eaf89dd2,subnet-52316827" 24 | security_group_ids="sg-7be8020d,sg-65e80204" 25 | min_size = "3" 26 | max_size = "10" 27 | desired_capacity ="4" 28 | iam_instance_profile = "AmazonECSContainerInstanceRole" 29 | registry_url = "https://index.docker.io/v1/" 30 | registry_email = "your_email@" 31 | registry_auth = "your_registry_auth_token" 32 | environment_name = "staging" 33 | } 34 | ``` 35 | 36 | ### Contact 37 | 38 | [![Robots & Pencils Logo](http://f.cl.ly/items/2W3n1r2R0j2p2b3n3j3c/rnplogo.png)](http://www.robotsandpencils.com) 39 | 40 | Made with :heart: by Robots & Pencils ([@robotsNpencils](https://twitter.com/robotsNpencils)) 41 | 42 | #### Maintainers 43 | 44 | - [Mike Brevoort](http://github.com/mbrevoort) ([@mbrevoort](https://twitter.com/mbrevoort)) 45 | -------------------------------------------------------------------------------- /ecs-autoscaling.tf: -------------------------------------------------------------------------------- 1 | resource "aws_autoscaling_group" "ecs-cluster" { 2 | availability_zones = ["${split(",", var.availability_zones)}"] 3 | vpc_zone_identifier = ["${split(",", var.subnet_ids)}"] 4 | name = "ECS ${var.cluster_name}" 5 | min_size = "${var.min_size}" 6 | max_size = "${var.max_size}" 7 | desired_capacity = "${var.desired_capacity}" 8 | health_check_type = "EC2" 9 | launch_configuration = "${aws_launch_configuration.ecs.name}" 10 | health_check_grace_period = "${var.health_check_grace_period}" 11 | 12 | tag { 13 | key = "Env" 14 | value = "${var.environment_name}" 15 | propagate_at_launch = true 16 | } 17 | 18 | tag { 19 | key = "Name" 20 | value = "ECS ${var.cluster_name}" 21 | propagate_at_launch = true 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /launch-configuration.tf: -------------------------------------------------------------------------------- 1 | resource "aws_launch_configuration" "ecs" { 2 | name = "ECS ${var.cluster_name}" 3 | image_id = "${var.ami}" 4 | instance_type = "${var.instance_type}" 5 | iam_instance_profile = "${var.iam_instance_profile}" 6 | key_name = "${var.key_name}" 7 | security_groups = ["${split(",", var.security_group_ids)}"] 8 | user_data = "#!/bin/bash\necho ECS_CLUSTER=${var.cluster_name} > /etc/ecs/ecs.config && echo ECS_ENGINE_AUTH_TYPE=dockercfg >> /etc/ecs/ecs.config && echo ECS_ENGINE_AUTH_DATA='{\"${var.registry_url}\":{\"auth\":\"${var.registry_auth}\",\"email\":\"${var.registry_email}\"}}' >> /etc/ecs/ecs.config" 9 | } 10 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "autoscaling.id" { 2 | value = "${aws_autoscaling_group.ecs-cluster.id}" 3 | } 4 | 5 | output "autoscaling.availability_zones" { 6 | value = "${aws_autoscaling_group.ecs-cluster.availability_zones}" 7 | } 8 | 9 | output "autoscaling.min_size" { 10 | value = "${aws_autoscaling_group.ecs-cluster.min_size}" 11 | } 12 | 13 | output "autoscaling.max_size" { 14 | value = "${aws_autoscaling_group.ecs-cluster.max_size}" 15 | } 16 | 17 | output "autoscaling.default_cooldown" { 18 | value = "${aws_autoscaling_group.ecs-cluster.default_cooldown}" 19 | } 20 | 21 | output "autoscaling.name" { 22 | value = "${aws_autoscaling_group.ecs-cluster.name}" 23 | } 24 | 25 | output "autoscaling.health_check_grace_period" { 26 | value = "${aws_autoscaling_group.ecs-cluster.health_check_grace_period}" 27 | } 28 | 29 | output "autoscaling.health_check_type" { 30 | value = "${aws_autoscaling_group.ecs-cluster.health_check_type}" 31 | } 32 | 33 | output "autoscaling.desired_capacity" { 34 | value = "${aws_autoscaling_group.ecs-cluster.desired_capacity}" 35 | } 36 | 37 | output "autoscaling.launch_configuration" { 38 | value = "${aws_autoscaling_group.ecs-cluster.launch_configuration}" 39 | } 40 | 41 | output "autoscaling.vpc_zone_identifier" { 42 | value = "${aws_autoscaling_group.ecs-cluster.vpc_zone_identifier}" 43 | } 44 | 45 | output "autoscaling.load_balancers" { 46 | value = "${aws_autoscaling_group.ecs-cluster.load_balancers}" 47 | } 48 | 49 | output "launch_configuration.id" { 50 | value = "${aws_launch_configuration.ecs.id}" 51 | } 52 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "cluster_name" { 2 | description = "The name of the ECS Cluster" 3 | } 4 | 5 | variable "ami" { 6 | /* us-east-1, version 2015.03.d */ 7 | default = "ami-e1c33f8a" 8 | description = "AMI id to launch, must be in the region specified by the region variable" 9 | } 10 | 11 | variable "key_name" { 12 | description = "SSH key name in your AWS account for AWS instances." 13 | } 14 | 15 | variable "region" { 16 | default = "us-east-1" 17 | description = "The region of AWS" 18 | } 19 | 20 | variable "availability_zones" { 21 | description = "Comma separated list of EC2 availability zones to launch instances, must be within region" 22 | } 23 | 24 | variable "subnet_ids" { 25 | description = "Comma separated list of subnet ids, must match availability zones" 26 | } 27 | 28 | variable "security_group_ids" { 29 | description = "Comma separated list of security group ids" 30 | default = "" 31 | } 32 | 33 | variable "instance_type" { 34 | default = "m1.small" 35 | description = "Name of the AWS instance type" 36 | } 37 | 38 | variable "min_size" { 39 | default = "1" 40 | description = "Minimum number of instances to run in the group" 41 | } 42 | 43 | variable "max_size" { 44 | default = "5" 45 | description = "Maximum number of instances to run in the group" 46 | } 47 | 48 | variable "desired_capacity" { 49 | default = "1" 50 | description = "Desired number of instances to run in the group" 51 | } 52 | 53 | variable "health_check_grace_period" { 54 | default = "300" 55 | description = "Time after instance comes into service before checking health" 56 | } 57 | variable "iam_instance_profile" { 58 | description = "The IAM Instance Profile (e.g. right side of Name=AmazonECSContainerInstanceRole)" 59 | } 60 | 61 | variable "registry_url" { 62 | default = "https://index.docker.io/v1/" 63 | description = "Docker private registry URL, defaults to Docker index" 64 | } 65 | 66 | variable "registry_email" { 67 | default = "" 68 | description = "Docker private registry login email address" 69 | } 70 | 71 | variable "registry_auth" { 72 | default = "" 73 | description = "Docker private registry login auth token (from ~/.dockercgf)" 74 | } 75 | 76 | variable "environment_name" { 77 | default = "" 78 | description = "Environment name to tag EC2 resources with (tag=environment)" 79 | } 80 | --------------------------------------------------------------------------------