Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. ---
  2. AWSTemplateFormatVersion: 2010-09-09
  3.  
  4. Parameters:
  5. LaunchType:
  6. Type: String
  7. Default: EC2
  8. InstanceType:
  9. Type: String
  10. Default: t2.medium
  11. ClusterSize:
  12. Type: Number
  13. Default: 2
  14. Subnets:
  15. Type: List<AWS::EC2::Subnet::Id>
  16. VpcId:
  17. Type: AWS::EC2::VPC::Id
  18. AMI:
  19. Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
  20. Default: /GAMI/latest
  21. Key:
  22. Type: String
  23. Default: PE-GAMI
  24. Conditions:
  25. EC2: !Equals [ !Ref LaunchType, "EC2" ]
  26. Resources:
  27. ECSRole:
  28. Type: AWS::IAM::Role
  29. Condition: EC2
  30. Properties:
  31. Path: /
  32. AssumeRolePolicyDocument:
  33. Statement:
  34. - Action: sts:AssumeRole
  35. Effect: Allow
  36. Principal:
  37. Service: ec2.amazonaws.com
  38. ManagedPolicyArns:
  39. - arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
  40. SecurityGroup:
  41. Type: AWS::EC2::SecurityGroup
  42. Properties:
  43. GroupDescription: Allow basic access via SSH
  44. VpcId: !Ref 'VpcId'
  45. SecurityGroupIngress:
  46. - IpProtocol: tcp
  47. FromPort: 22
  48. ToPort: 22
  49. CidrIp: 172.16.0.0/12
  50. InstanceProfile:
  51. Type: AWS::IAM::InstanceProfile
  52. Condition: EC2
  53. Properties:
  54. Path: /
  55. Roles:
  56. - !Ref ECSRole
  57. Cluster:
  58. Type: AWS::ECS::Cluster
  59. Properties:
  60. ClusterName: !Ref AWS::StackName
  61. AutoScalingGroup:
  62. Type: AWS::AutoScaling::AutoScalingGroup
  63. Condition: EC2
  64. Properties:
  65. VPCZoneIdentifier: !Ref Subnets
  66. LaunchConfigurationName: !Ref LaunchConfiguration
  67. MinSize: !Ref ClusterSize
  68. MaxSize: !Ref ClusterSize
  69. DesiredCapacity: !Ref ClusterSize
  70. Tags:
  71. - Key: Name
  72. Value: !Sub ${AWS::StackName} - ECS Host
  73. PropagateAtLaunch: true
  74. CreationPolicy:
  75. ResourceSignal:
  76. Timeout: PT15M
  77. UpdatePolicy:
  78. AutoScalingRollingUpdate:
  79. MinInstancesInService: 1
  80. MaxBatchSize: 1
  81. PauseTime: PT15M
  82. WaitOnResourceSignals: true
  83. LaunchConfiguration:
  84. Type: AWS::AutoScaling::LaunchConfiguration
  85. Condition: EC2
  86. Metadata:
  87. AWS::CloudFormation::Init:
  88. config:
  89. commands:
  90. 01_add_instance_to_cluster:
  91. command: !Sub echo ECS_CLUSTER=${Cluster} > /etc/ecs/ecs.config
  92. files:
  93. "/etc/cfn/cfn-hup.conf":
  94. mode: 000400
  95. owner: root
  96. group: root
  97. content: !Sub |
  98. [main]
  99. stack=${AWS::StackId}
  100. region=${AWS::Region}
  101. "/etc/cfn/hooks.d/cfn-auto-reloader.conf":
  102. content: !Sub |
  103. [cfn-auto-reloader-hook]
  104. triggers=post.update
  105. path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init
  106. action=/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
  107. services:
  108. sysvinit:
  109. cfn-hup:
  110. enabled: true
  111. ensureRunning: true
  112. files:
  113. - /etc/cfn/cfn-hup.conf
  114. - /etc/cfn/hooks.d/cfn-auto-reloader.conf
  115. Properties:
  116. ImageId: !Ref AMI
  117. KeyName: !Ref Key
  118. InstanceType: !Ref InstanceType
  119. IamInstanceProfile: !Ref InstanceProfile
  120. SecurityGroups:
  121. - !Ref SecurityGroup
  122. UserData:
  123. Fn::Base64:
  124. !Sub |
  125. #!/bin/bash -v
  126. ## UserData script for version 1
  127. yum install aws-cfn-bootstrap -y
  128. /opt/aws/bin/cfn-init -v -c default -s ${AWS::StackId} -r LaunchConfiguration --region ${AWS::Region}
  129. # disable docker before installing ECS agent
  130. amazon-linux-extras disable docker
  131. amazon-linux-extras install -y ecs
  132. service docker start
  133. systemd-run --on-active=20 systemctl enable --now ecs
  134. ## CloudFormation signal that setup is complete
  135. /opt/aws/bin/cfn-signal -e 0 --stack ${AWS::StackName} --resource AutoScalingGroup --region ${AWS::Region}
  136. Outputs:
  137. ClusterName:
  138. Value: !Ref Cluster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement