Created
November 17, 2015 10:38
-
-
Save caevyn/55a2ac09322d6e980858 to your computer and use it in GitHub Desktop.
ecs cloudformation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AWSTemplateFormatVersion" : "2010-09-09", | |
"Parameters" : { | |
"KeyName": { | |
"Type": "AWS::EC2::KeyPair::KeyName", | |
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the ECS instances" | |
}, | |
"SubnetID": { | |
"Type": "List<AWS::EC2::Subnet::Id>", | |
"Description": "List of an existing subnet IDs to use for the load balancer and auto scaling group" | |
}, | |
"DesiredCapacity": { | |
"Type": "Number", | |
"Default" : "1", | |
"Description": "Number of instances to launch in your ECS cluster" | |
}, | |
"MaxSize": { | |
"Type": "Number", | |
"Default" : "1", | |
"Description": "Maximum number of instances that can be launched in your ECS cluster" | |
}, | |
"InstanceType" : { | |
"Description" : "The EC2 instance type", | |
"Type" : "String", | |
"Default" : "t2.micro", | |
"AllowedValues" : [ "t2.micro", "t2.small", "t2.medium", "m3.medium", "m3.large", "m3.xlarge", | |
"m3.2xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c4.large", "c4.xlarge", | |
"c4.2xlarge", "c4.4xlarge", "c4.8xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", | |
"i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "d2.xlarge", "d2.2xlarge", "d2.4xlarge", "d2.8xlarge", | |
"hi1.4xlarge", "hs1.8xlarge", "cr1.8xlarge", "cc2.8xlarge"], | |
"ConstraintDescription" : "must be a valid EC2 instance type." | |
}, | |
"SSHLocation" : { | |
"Description" : " The IP address range that can be used to SSH to the EC2 instances", | |
"Type": "String", | |
"MinLength": "9", | |
"MaxLength": "18", | |
"Default": "0.0.0.0/0", | |
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})", | |
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x." | |
} | |
}, | |
"Mappings" : { | |
"AWSRegionToAMI" : { | |
"ap-southeast-2" : { "AMIID" : "ami-fb0d45c1" } | |
} | |
}, | |
"Resources" : { | |
"ECSCluster": { | |
"Type": "AWS::ECS::Cluster" | |
}, | |
"taskdefinition": { | |
"Type": "AWS::ECS::TaskDefinition", | |
"Properties" : { | |
"ContainerDefinitions" : [ | |
{ | |
"Name": "node-app", | |
"Cpu": "10", | |
"Essential": "true", | |
"Image":"aterreno/docker-node-hello", | |
"Memory":"300", | |
"PortMappings": [ | |
{ "HostPort": 80, "ContainerPort": 8080 } | |
] | |
} | |
] | |
} | |
}, | |
"EcsElasticLoadBalancer" : { | |
"Type" : "AWS::ElasticLoadBalancing::LoadBalancer", | |
"Properties" : { | |
"Subnets" : { "Ref" : "SubnetID" }, | |
"Listeners" : [ { | |
"LoadBalancerPort" : "80", | |
"InstancePort" : "80", | |
"Protocol" : "HTTP" | |
} ], | |
"HealthCheck" : { | |
"Target" : "HTTP:80/", | |
"HealthyThreshold" : "2", | |
"UnhealthyThreshold" : "10", | |
"Interval" : "30", | |
"Timeout" : "5" | |
} | |
} | |
}, | |
"ECSAutoScalingGroup" : { | |
"Type" : "AWS::AutoScaling::AutoScalingGroup", | |
"Properties" : { | |
"VPCZoneIdentifier" : { "Ref" : "SubnetID" }, | |
"LaunchConfigurationName" : { "Ref" : "ContainerInstances" }, | |
"MinSize" : "1", | |
"MaxSize" : { "Ref" : "MaxSize" }, | |
"DesiredCapacity" : { "Ref" : "DesiredCapacity" } | |
}, | |
"CreationPolicy" : { | |
"ResourceSignal" : { | |
"Timeout" : "PT5M" | |
} | |
}, | |
"UpdatePolicy": { | |
"AutoScalingRollingUpdate": { | |
"MinInstancesInService": "1", | |
"MaxBatchSize": "1", | |
"PauseTime" : "PT1M", | |
"WaitOnResourceSignals": "true" | |
} | |
} | |
}, | |
"ContainerInstances": { | |
"Type": "AWS::AutoScaling::LaunchConfiguration", | |
"Metadata" : { | |
"AWS::CloudFormation::Init" : { | |
"config" : { | |
"commands" : { | |
"01_add_instance_to_cluster" : { | |
"command" : { "Fn::Join": [ "", [ "#!/bin/bash\n", "echo ECS_CLUSTER=", { "Ref": "ECSCluster" }, " >> /etc/ecs/ecs.config" ] ] } | |
} | |
}, | |
"files" : { | |
"/etc/cfn/cfn-hup.conf" : { | |
"content" : { "Fn::Join" : ["", [ | |
"[main]\n", | |
"stack=", { "Ref" : "AWS::StackId" }, "\n", | |
"region=", { "Ref" : "AWS::Region" }, "\n" | |
]]}, | |
"mode" : "000400", | |
"owner" : "root", | |
"group" : "root" | |
}, | |
"/etc/cfn/hooks.d/cfn-auto-reloader.conf" : { | |
"content": { "Fn::Join" : ["", [ | |
"[cfn-auto-reloader-hook]\n", | |
"triggers=post.update\n", | |
"path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init\n", | |
"action=/opt/aws/bin/cfn-init -v ", | |
" --stack ", { "Ref" : "AWS::StackName" }, | |
" --resource ContainerInstances ", | |
" --region ", { "Ref" : "AWS::Region" }, "\n", | |
"runas=root\n" | |
]]} | |
} | |
}, | |
"services" : { | |
"sysvinit" : { | |
"cfn-hup" : { "enabled" : "true", "ensureRunning" : "true", "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"] } | |
} | |
} | |
} | |
} | |
}, | |
"Properties": { | |
"ImageId" : { "Fn::FindInMap" : [ "AWSRegionToAMI", { "Ref" : "AWS::Region" }, "AMIID" ] }, | |
"InstanceType" : { "Ref" : "InstanceType" }, | |
"IamInstanceProfile": { "Ref": "EC2InstanceProfile" }, | |
"KeyName" : { "Ref" : "KeyName" }, | |
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ | |
"#!/bin/bash -xe\n", | |
"yum install -y aws-cfn-bootstrap\n", | |
"/opt/aws/bin/cfn-init -v ", | |
" --stack ", { "Ref" : "AWS::StackName" }, | |
" --resource ContainerInstances ", | |
" --region ", { "Ref" : "AWS::Region" }, "\n", | |
"/opt/aws/bin/cfn-signal -e $? ", | |
" --stack ", { "Ref" : "AWS::StackName" }, | |
" --resource ECSAutoScalingGroup ", | |
" --region ", { "Ref" : "AWS::Region" }, "\n" | |
]]}} | |
} | |
}, | |
"service": { | |
"Type": "AWS::ECS::Service", | |
"DependsOn": ["ECSAutoScalingGroup"], | |
"Properties" : { | |
"Cluster": {"Ref": "ECSCluster"}, | |
"DesiredCount": "2", | |
"LoadBalancers": [ | |
{ | |
"ContainerName": "node-app", | |
"ContainerPort": "8080", | |
"LoadBalancerName" : { "Ref" : "EcsElasticLoadBalancer" } | |
} | |
], | |
"Role" : {"Ref":"ECSServiceRole"}, | |
"TaskDefinition" : {"Ref":"taskdefinition"} | |
} | |
}, | |
"ECSServiceRole": { | |
"Type": "AWS::IAM::Role", | |
"Properties": { | |
"AssumeRolePolicyDocument": { | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": [ | |
"ecs.amazonaws.com" | |
] | |
}, | |
"Action": [ | |
"sts:AssumeRole" | |
] | |
} | |
] | |
}, | |
"Path": "/", | |
"Policies": [ | |
{ | |
"PolicyName": "ecs-service", | |
"PolicyDocument": { | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"elasticloadbalancing:Describe*", | |
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer", | |
"elasticloadbalancing:RegisterInstancesWithLoadBalancer", | |
"ec2:Describe*", | |
"ec2:AuthorizeSecurityGroupIngress" | |
], | |
"Resource": "*" | |
} | |
] | |
} | |
} | |
] | |
} | |
}, | |
"EC2Role": { | |
"Type": "AWS::IAM::Role", | |
"Properties": { | |
"AssumeRolePolicyDocument": { | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": [ | |
"ec2.amazonaws.com" | |
] | |
}, | |
"Action": [ | |
"sts:AssumeRole" | |
] | |
} | |
] | |
}, | |
"Path": "/", | |
"Policies": [ | |
{ | |
"PolicyName": "ecs-service", | |
"PolicyDocument": { | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"ecs:CreateCluster", | |
"ecs:RegisterContainerInstance", | |
"ecs:DeregisterContainerInstance", | |
"ecs:DiscoverPollEndpoint", | |
"ecs:Submit*", | |
"ecs:Poll" | |
], | |
"Resource": "*" | |
} | |
] | |
} | |
} | |
] | |
} | |
}, | |
"EC2InstanceProfile": { | |
"Type": "AWS::IAM::InstanceProfile", | |
"Properties": { | |
"Path": "/", | |
"Roles": [ | |
{ | |
"Ref": "EC2Role" | |
} | |
] | |
} | |
} | |
}, | |
"Outputs" : { | |
"ecsservice" : { | |
"Value" : { "Ref" : "service" } | |
}, | |
"ecscluster" : { | |
"Value" : { "Ref" : "ECSCluster" } | |
}, | |
"taskdef" : { | |
"Value" : { "Ref" : "taskdefinition" } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment