infrastructure.yml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 interrupt the installation
  117. export DEBIAN_FRONTEND=noninteractive
  118. # Install the updates
  119. apt-get upgrade -y
  120. # Get latest cfn scripts and install them;
  121. apt-get install -y python3-setuptools
  122. mkdir -p /opt/aws/bin
  123. wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
  124. python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
  125. /opt/aws/bin/cfn-signal -e $? -r "Instance Created" '${WaitHandle}'
  126. AutoScalingGroup:
  127. Type: AWS::AutoScaling::AutoScalingGroup
  128. Properties:
  129. MinSize: '0'
  130. MaxSize: '10'
  131. DesiredCapacity: !Ref NumberOfValidators
  132. AvailabilityZones:
  133. Fn::GetAZs:
  134. Ref: "AWS::Region"
  135. MixedInstancesPolicy:
  136. LaunchTemplate:
  137. LaunchTemplateSpecification:
  138. LaunchTemplateId: !Ref InstanceLaunchTemplate
  139. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  140. Overrides:
  141. - InstanceType: !Ref ValidatorEC2InstanceType
  142. Tags:
  143. - Key: Name
  144. Value: !Sub '${AWS::StackName}'
  145. PropagateAtLaunch: "true"
  146. RPCInstance:
  147. Type: AWS::EC2::Instance
  148. Properties:
  149. SecurityGroupIds:
  150. - !GetAtt RPCSecurityGroup.GroupId
  151. InstanceType: !Ref RPCEC2InstanceType
  152. LaunchTemplate:
  153. LaunchTemplateId: !Ref InstanceLaunchTemplate
  154. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  155. Tags:
  156. - Key: Name
  157. Value: !Sub '${AWS::StackName}_rpc'
  158. BuildInstance:
  159. Type: AWS::EC2::Instance
  160. Properties:
  161. InstanceType: !Ref BuildEC2InstanceType
  162. LaunchTemplate:
  163. LaunchTemplateId: !Ref InstanceLaunchTemplate
  164. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  165. Tags:
  166. - Key: Name
  167. Value: !Sub '${AWS::StackName}_build'
  168. WaitHandle:
  169. Type: AWS::CloudFormation::WaitConditionHandle
  170. WaitCondition:
  171. Type: AWS::CloudFormation::WaitCondition
  172. Properties:
  173. Handle: !Ref 'WaitHandle'
  174. Timeout: '600'
  175. Count: !Ref NumberOfValidators
  176. S3Bucket:
  177. Type: AWS::S3::Bucket
  178. Properties:
  179. AccessControl: PublicRead
  180. WebsiteConfiguration:
  181. IndexDocument: index.html
  182. BucketPolicy:
  183. Type: AWS::S3::BucketPolicy
  184. Properties:
  185. PolicyDocument:
  186. Id: PublicPolicy
  187. Version: 2012-10-17
  188. Statement:
  189. - Sid: PublicReadForGetBucketObjects
  190. Effect: Allow
  191. Principal: '*'
  192. Action: 's3:GetObject'
  193. Resource: !Sub "arn:aws:s3:::${S3Bucket}/*"
  194. Bucket: !Ref S3Bucket
  195. CloudFrontDistribution:
  196. Type: AWS::CloudFront::Distribution
  197. Properties:
  198. DistributionConfig:
  199. Origins:
  200. - DomainName: !Select [1, !Split ["//", !GetAtt S3Bucket.WebsiteURL]]
  201. Id: pioneer-origin-s3
  202. CustomOriginConfig:
  203. OriginProtocolPolicy: http-only
  204. DefaultCacheBehavior:
  205. TargetOriginId: pioneer-origin-s3
  206. ViewerProtocolPolicy: redirect-to-https
  207. ForwardedValues:
  208. QueryString: true
  209. Enabled: true
  210. HttpVersion: http2
  211. Outputs:
  212. AutoScalingId:
  213. Description: The Auto Scaling ID
  214. Value: !Ref AutoScalingGroup
  215. Export:
  216. Name: !Sub "${AWS::StackName}AutoScalingGroup"
  217. RPCPublicIp:
  218. Description: The DNS name for the created instance
  219. Value: !Sub "${RPCInstance.PublicIp}"
  220. Export:
  221. Name: !Sub "${AWS::StackName}RPCPublicIp"
  222. BuildPublicIp:
  223. Description: The DNS name for the created instance
  224. Value: !Sub "${BuildInstance.PublicIp}"
  225. Export:
  226. Name: !Sub "${AWS::StackName}BuildPublicIp"
  227. S3BucketName:
  228. Value: !Ref S3Bucket
  229. Description: Name of S3 bucket to hold website content
  230. Export:
  231. Name: !Sub "${AWS::StackName}S3BucketName"
  232. DomainName:
  233. Description: CloudFront Domain Name
  234. Value: !Sub "${CloudFrontDistribution.DomainName}"
  235. Export:
  236. Name: !Sub "${AWS::StackName}DomainName"