Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- AWSTemplateFormatVersion: 2010-09-09
- Parameters:
- LaunchType:
- Type: String
- Default: EC2
- InstanceType:
- Type: String
- Default: t2.medium
- ClusterSize:
- Type: Number
- Default: 2
- Subnets:
- Type: List<AWS::EC2::Subnet::Id>
- VpcId:
- Type: AWS::EC2::VPC::Id
- AMI:
- Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
- Default: /GAMI/latest
- Key:
- Type: String
- Default: PE-GAMI
- Conditions:
- EC2: !Equals [ !Ref LaunchType, "EC2" ]
- Resources:
- ECSRole:
- Type: AWS::IAM::Role
- Condition: EC2
- Properties:
- Path: /
- AssumeRolePolicyDocument:
- Statement:
- - Action: sts:AssumeRole
- Effect: Allow
- Principal:
- Service: ec2.amazonaws.com
- ManagedPolicyArns:
- - arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
- SecurityGroup:
- Type: AWS::EC2::SecurityGroup
- Properties:
- GroupDescription: Allow basic access via SSH
- VpcId: !Ref 'VpcId'
- SecurityGroupIngress:
- - IpProtocol: tcp
- FromPort: 22
- ToPort: 22
- CidrIp: 172.16.0.0/12
- InstanceProfile:
- Type: AWS::IAM::InstanceProfile
- Condition: EC2
- Properties:
- Path: /
- Roles:
- - !Ref ECSRole
- Cluster:
- Type: AWS::ECS::Cluster
- Properties:
- ClusterName: !Ref AWS::StackName
- AutoScalingGroup:
- Type: AWS::AutoScaling::AutoScalingGroup
- Condition: EC2
- Properties:
- VPCZoneIdentifier: !Ref Subnets
- LaunchConfigurationName: !Ref LaunchConfiguration
- MinSize: !Ref ClusterSize
- MaxSize: !Ref ClusterSize
- DesiredCapacity: !Ref ClusterSize
- Tags:
- - Key: Name
- Value: !Sub ${AWS::StackName} - ECS Host
- PropagateAtLaunch: true
- CreationPolicy:
- ResourceSignal:
- Timeout: PT15M
- UpdatePolicy:
- AutoScalingRollingUpdate:
- MinInstancesInService: 1
- MaxBatchSize: 1
- PauseTime: PT15M
- WaitOnResourceSignals: true
- LaunchConfiguration:
- Type: AWS::AutoScaling::LaunchConfiguration
- Condition: EC2
- Metadata:
- AWS::CloudFormation::Init:
- config:
- commands:
- 01_add_instance_to_cluster:
- command: !Sub echo ECS_CLUSTER=${Cluster} > /etc/ecs/ecs.config
- files:
- "/etc/cfn/cfn-hup.conf":
- mode: 000400
- owner: root
- group: root
- content: !Sub |
- [main]
- stack=${AWS::StackId}
- region=${AWS::Region}
- "/etc/cfn/hooks.d/cfn-auto-reloader.conf":
- content: !Sub |
- [cfn-auto-reloader-hook]
- triggers=post.update
- path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init
- action=/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
- services:
- sysvinit:
- cfn-hup:
- enabled: true
- ensureRunning: true
- files:
- - /etc/cfn/cfn-hup.conf
- - /etc/cfn/hooks.d/cfn-auto-reloader.conf
- Properties:
- ImageId: !Ref AMI
- KeyName: !Ref Key
- InstanceType: !Ref InstanceType
- IamInstanceProfile: !Ref InstanceProfile
- SecurityGroups:
- - !Ref SecurityGroup
- UserData:
- Fn::Base64:
- !Sub |
- #!/bin/bash -v
- ## UserData script for version 1
- yum install aws-cfn-bootstrap -y
- /opt/aws/bin/cfn-init -v -c default -s ${AWS::StackId} -r LaunchConfiguration --region ${AWS::Region}
- # disable docker before installing ECS agent
- amazon-linux-extras disable docker
- amazon-linux-extras install -y ecs
- service docker start
- systemd-run --on-active=20 systemctl enable --now ecs
- ## CloudFormation signal that setup is complete
- /opt/aws/bin/cfn-signal -e 0 --stack ${AWS::StackName} --resource AutoScalingGroup --region ${AWS::Region}
- Outputs:
- ClusterName:
- Value: !Ref Cluster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement