infrastructure.yml 7.3 KB

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