infrastructure.yml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. AWSTemplateFormatVersion: 2010-09-09
  2. Parameters:
  3. EC2InstanceType:
  4. Type: String
  5. Default: t2.micro
  6. ValidatorEC2InstanceType:
  7. Type: String
  8. Default: t2.micro
  9. RPCEC2InstanceType:
  10. Type: String
  11. Default: t2.micro
  12. BuildEC2InstanceType:
  13. Type: String
  14. Default: t2.micro
  15. EC2AMI:
  16. Type: String
  17. Default: 'ami-09e67e426f25ce0d7'
  18. DefaultAMI:
  19. Type: String
  20. Default: 'ami-09e67e426f25ce0d7'
  21. KeyName:
  22. Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
  23. Type: 'AWS::EC2::KeyPair::KeyName'
  24. Default: 'joystream-key'
  25. ConstraintDescription: must be the name of an existing EC2 KeyPair.
  26. NumberOfValidators:
  27. Description: Number of validator instances to launch
  28. Type: Number
  29. Default: 2
  30. Conditions:
  31. HasAMIId: !Not [!Equals [!Ref EC2AMI, ""]]
  32. Resources:
  33. SecurityGroup:
  34. Type: AWS::EC2::SecurityGroup
  35. Properties:
  36. GroupDescription:
  37. !Sub 'Internal Security group for validator nodes ${AWS::StackName}'
  38. SecurityGroupIngress:
  39. - IpProtocol: tcp
  40. FromPort: 30333
  41. ToPort: 30333
  42. CidrIp: 0.0.0.0/0
  43. - IpProtocol: tcp
  44. FromPort: 22
  45. ToPort: 22
  46. CidrIp: 0.0.0.0/0
  47. Tags:
  48. - Key: Name
  49. Value: !Sub '${AWS::StackName}_validator'
  50. RPCSecurityGroup:
  51. Type: AWS::EC2::SecurityGroup
  52. Properties:
  53. GroupDescription:
  54. !Sub 'Internal Security group for RPC nodes ${AWS::StackName}'
  55. SecurityGroupIngress:
  56. - IpProtocol: tcp
  57. FromPort: 9933
  58. ToPort: 9933
  59. CidrIp: 0.0.0.0/0
  60. - IpProtocol: tcp
  61. FromPort: 9944
  62. ToPort: 9944
  63. CidrIp: 0.0.0.0/0
  64. - IpProtocol: tcp
  65. FromPort: 30333
  66. ToPort: 30333
  67. CidrIp: 0.0.0.0/0
  68. - IpProtocol: tcp
  69. FromPort: 443
  70. ToPort: 443
  71. CidrIp: 0.0.0.0/0
  72. - IpProtocol: tcp
  73. FromPort: 80
  74. ToPort: 80
  75. CidrIp: 0.0.0.0/0
  76. - IpProtocol: tcp
  77. FromPort: 22
  78. ToPort: 22
  79. CidrIp: 0.0.0.0/0
  80. Tags:
  81. - Key: Name
  82. Value: !Sub '${AWS::StackName}_rpc'
  83. InstanceLaunchTemplate:
  84. Type: AWS::EC2::LaunchTemplate
  85. Metadata:
  86. AWS::CloudFormation::Init:
  87. config:
  88. packages:
  89. apt:
  90. wget: []
  91. unzip: []
  92. Properties:
  93. LaunchTemplateName: !Sub 'LaunchTemplate_${AWS::StackName}'
  94. LaunchTemplateData:
  95. ImageId: !If [HasAMIId, !Ref EC2AMI, !Ref DefaultAMI]
  96. InstanceType: !Ref EC2InstanceType
  97. KeyName: !Ref KeyName
  98. SecurityGroupIds:
  99. - !GetAtt SecurityGroup.GroupId
  100. BlockDeviceMappings:
  101. - DeviceName: /dev/sda1
  102. Ebs:
  103. VolumeSize: '40'
  104. UserData:
  105. Fn::Base64: !Sub |
  106. #!/bin/bash -xe
  107. # send script output to /tmp so we can debug boot failures
  108. exec > /tmp/userdata.log 2>&1
  109. # Update all packages
  110. apt-get update -y
  111. # Install the updates except docker, to avoid interactive prompt which blocks the flow of the script
  112. apt-mark hold docker.io
  113. apt-get upgrade -y
  114. # Get latest cfn scripts and install them;
  115. apt-get install -y python3-setuptools
  116. mkdir -p /opt/aws/bin
  117. wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
  118. python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
  119. /opt/aws/bin/cfn-signal -e $? -r "Instance Created" '${WaitHandle}'
  120. AutoScalingGroup:
  121. Type: AWS::AutoScaling::AutoScalingGroup
  122. Properties:
  123. MinSize: '0'
  124. MaxSize: '10'
  125. DesiredCapacity: !Ref NumberOfValidators
  126. AvailabilityZones:
  127. Fn::GetAZs:
  128. Ref: "AWS::Region"
  129. MixedInstancesPolicy:
  130. LaunchTemplate:
  131. LaunchTemplateSpecification:
  132. LaunchTemplateId: !Ref InstanceLaunchTemplate
  133. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  134. Overrides:
  135. - InstanceType: !Ref ValidatorEC2InstanceType
  136. Tags:
  137. - Key: Name
  138. Value: !Sub '${AWS::StackName}'
  139. PropagateAtLaunch: "true"
  140. RPCInstance:
  141. Type: AWS::EC2::Instance
  142. Properties:
  143. SecurityGroupIds:
  144. - !GetAtt RPCSecurityGroup.GroupId
  145. InstanceType: !Ref RPCEC2InstanceType
  146. LaunchTemplate:
  147. LaunchTemplateId: !Ref InstanceLaunchTemplate
  148. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  149. Tags:
  150. - Key: Name
  151. Value: !Sub '${AWS::StackName}_rpc'
  152. BuildInstance:
  153. Type: AWS::EC2::Instance
  154. Properties:
  155. InstanceType: !Ref BuildEC2InstanceType
  156. LaunchTemplate:
  157. LaunchTemplateId: !Ref InstanceLaunchTemplate
  158. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  159. Tags:
  160. - Key: Name
  161. Value: !Sub '${AWS::StackName}_build'
  162. WaitHandle:
  163. Type: AWS::CloudFormation::WaitConditionHandle
  164. WaitCondition:
  165. Type: AWS::CloudFormation::WaitCondition
  166. Properties:
  167. Handle: !Ref 'WaitHandle'
  168. Timeout: '600'
  169. Count: !Ref NumberOfValidators
  170. S3Bucket:
  171. Type: AWS::S3::Bucket
  172. Properties:
  173. AccessControl: PublicRead
  174. WebsiteConfiguration:
  175. IndexDocument: index.html
  176. BucketPolicy:
  177. Type: AWS::S3::BucketPolicy
  178. Properties:
  179. PolicyDocument:
  180. Id: PublicPolicy
  181. Version: 2012-10-17
  182. Statement:
  183. - Sid: PublicReadForGetBucketObjects
  184. Effect: Allow
  185. Principal: '*'
  186. Action: 's3:GetObject'
  187. Resource: !Sub "arn:aws:s3:::${S3Bucket}/*"
  188. Bucket: !Ref S3Bucket
  189. CloudFrontDistribution:
  190. Type: AWS::CloudFront::Distribution
  191. Properties:
  192. DistributionConfig:
  193. Origins:
  194. - DomainName: !Select [1, !Split ["//", !GetAtt S3Bucket.WebsiteURL]]
  195. Id: pioneer-origin-s3
  196. CustomOriginConfig:
  197. OriginProtocolPolicy: http-only
  198. DefaultCacheBehavior:
  199. TargetOriginId: pioneer-origin-s3
  200. ViewerProtocolPolicy: redirect-to-https
  201. ForwardedValues:
  202. QueryString: true
  203. Enabled: true
  204. HttpVersion: http2
  205. Outputs:
  206. AutoScalingId:
  207. Description: The Auto Scaling ID
  208. Value: !Ref AutoScalingGroup
  209. Export:
  210. Name: !Sub "${AWS::StackName}AutoScalingGroup"
  211. RPCPublicIp:
  212. Description: The DNS name for the created instance
  213. Value: !Sub "${RPCInstance.PublicIp}"
  214. Export:
  215. Name: !Sub "${AWS::StackName}RPCPublicIp"
  216. BuildPublicIp:
  217. Description: The DNS name for the created instance
  218. Value: !Sub "${BuildInstance.PublicIp}"
  219. Export:
  220. Name: !Sub "${AWS::StackName}BuildPublicIp"
  221. S3BucketName:
  222. Value: !Ref S3Bucket
  223. Description: Name of S3 bucket to hold website content
  224. Export:
  225. Name: !Sub "${AWS::StackName}S3BucketName"
  226. DomainName:
  227. Description: CloudFront Domain Name
  228. Value: !Sub "${CloudFrontDistribution.DomainName}"
  229. Export:
  230. Name: !Sub "${AWS::StackName}DomainName"