Skip to content

Instantly share code, notes, and snippets.

@kuntao
Last active April 29, 2017 08:09
Show Gist options
  • Select an option

  • Save kuntao/13af3e4de40bdd3c0dd7549e6ef9fd01 to your computer and use it in GitHub Desktop.

Select an option

Save kuntao/13af3e4de40bdd3c0dd7549e6ef9fd01 to your computer and use it in GitHub Desktop.
CloudFormation template for ECS
AWSTemplateFormatVersion: 2010-09-09
Description: |-
ECS Cluster test
Parameters:
VpcName:
Type: String
Default: dns_support_only
ClusterName:
Type: String
Default: dns_support_only
VpcCidr:
Type: String
Default: 41.0.0.0/16
Sbn1Cidr:
Type: String
Default: 41.0.0.0/24
Sbn2Cidr:
Type: String
Default: 41.0.1.0/24
Resources:
Vpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsHostnames: false
EnableDnsSupport: true
InstanceTenancy: default
Tags:
- Key: Name
Value: !Ref VpcName
RtbPublic:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref Vpc
Tags:
- Key: Name
Value: rtb_public
# Create public subnets and associates them to a route table
SbnPublicA:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1a
CidrBlock: !Ref Sbn1Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: sbn_public_a
VpcId: !Ref Vpc
PriSbnARtbAssoc:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref SbnPublicA
RouteTableId: !Ref RtbPublic
SbnPublicC:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: ap-northeast-1c
CidrBlock: !Ref Sbn2Cidr
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: sbn_public_c
VpcId: !Ref Vpc
PriSbnCRtbAssoc:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref SbnPublicC
RouteTableId: !Ref RtbPublic
# Create InternetGateway
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: igw_test_cluster
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref Vpc
InternetGatewayId: !Ref InternetGateway
RouteIgw:
Type: AWS::EC2::Route
DependsOn: InternetGateway
Properties:
RouteTableId: !Ref RtbPublic
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
# IAM Role and InstanceProfile
ECSInstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub "ECSClusterTestRole-${VpcName}"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
AssumeRolePolicyDocument: |
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Path: /
ECSInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref ECSInstanceRole
EcsInstanceLc:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId: ami-f63f6f91
InstanceType: t2.small
KeyName: eb_docker
AssociatePublicIpAddress: true
IamInstanceProfile: !GetAtt ECSInstanceProfile.Arn
SecurityGroups: [ !GetAtt Vpc.DefaultSecurityGroup ]
BlockDeviceMappings:
- DeviceName: /dev/xvdcz
Ebs:
VolumeSize: 22
VolumeType: gp2
UserData:
Fn::Base64: !Sub |
#!/bin/bash
echo ECS_CLUSTER=${ClusterName} >> /etc/ecs/ecs.config
EcsInstanceAsg:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref SbnPublicA
- !Ref SbnPublicC
Cooldown: 300
LaunchConfigurationName: !Ref EcsInstanceLc
MinSize: 1
MaxSize: 1
DesiredCapacity: 1
HealthCheckType: EC2
MetricsCollection:
- Granularity: 1Minute
# TargetGroupARNs:
# - !Ref ALBTargetGroup
Tags:
- Key: Name
Value: !Sub "${VpcName} instance"
PropagateAtLaunch: true
- Key: Description
Value: "This instance is the part of the Auto Scaling group which was created through ECS Console"
PropagateAtLaunch: true
EcsCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Ref ClusterName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment