infrastructure.yml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
  121. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  122. echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  123. apt-get update -y
  124. apt-get install -y docker-ce docker-ce-cli containerd.io
  125. usermod -aG docker ubuntu
  126. # Update docker-compose to 1.28+
  127. curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  128. chmod +x /usr/local/bin/docker-compose
  129. ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
  130. # Get latest cfn scripts and install them;
  131. apt-get install -y python3-setuptools
  132. mkdir -p /opt/aws/bin
  133. wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
  134. python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
  135. /opt/aws/bin/cfn-signal -e $? -r "Instance Created" '${WaitHandle}'
  136. AutoScalingGroup:
  137. Type: AWS::AutoScaling::AutoScalingGroup
  138. Properties:
  139. MinSize: '0'
  140. MaxSize: '10'
  141. DesiredCapacity: !Ref NumberOfValidators
  142. AvailabilityZones:
  143. Fn::GetAZs:
  144. Ref: "AWS::Region"
  145. MixedInstancesPolicy:
  146. LaunchTemplate:
  147. LaunchTemplateSpecification:
  148. LaunchTemplateId: !Ref InstanceLaunchTemplate
  149. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  150. Overrides:
  151. - InstanceType: !Ref ValidatorEC2InstanceType
  152. Tags:
  153. - Key: Name
  154. Value: !Sub '${AWS::StackName}'
  155. PropagateAtLaunch: "true"
  156. RPCInstance:
  157. Type: AWS::EC2::Instance
  158. Properties:
  159. SecurityGroupIds:
  160. - !GetAtt RPCSecurityGroup.GroupId
  161. InstanceType: !Ref RPCEC2InstanceType
  162. LaunchTemplate:
  163. LaunchTemplateId: !Ref InstanceLaunchTemplate
  164. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  165. Tags:
  166. - Key: Name
  167. Value: !Sub '${AWS::StackName}_rpc'
  168. BuildInstance:
  169. Type: AWS::EC2::Instance
  170. Properties:
  171. InstanceType: !Ref BuildEC2InstanceType
  172. LaunchTemplate:
  173. LaunchTemplateId: !Ref InstanceLaunchTemplate
  174. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  175. Tags:
  176. - Key: Name
  177. Value: !Sub '${AWS::StackName}_build'
  178. WaitHandle:
  179. Type: AWS::CloudFormation::WaitConditionHandle
  180. WaitCondition:
  181. Type: AWS::CloudFormation::WaitCondition
  182. Properties:
  183. Handle: !Ref 'WaitHandle'
  184. Timeout: '600'
  185. Count: !Ref NumberOfValidators
  186. S3Bucket:
  187. Type: AWS::S3::Bucket
  188. Properties:
  189. AccessControl: PublicRead
  190. WebsiteConfiguration:
  191. IndexDocument: index.html
  192. BucketPolicy:
  193. Type: AWS::S3::BucketPolicy
  194. Properties:
  195. PolicyDocument:
  196. Id: PublicPolicy
  197. Version: 2012-10-17
  198. Statement:
  199. - Sid: PublicReadForGetBucketObjects
  200. Effect: Allow
  201. Principal: '*'
  202. Action: 's3:GetObject'
  203. Resource: !Sub "arn:aws:s3:::${S3Bucket}/*"
  204. Bucket: !Ref S3Bucket
  205. CloudFrontDistribution:
  206. Type: AWS::CloudFront::Distribution
  207. Properties:
  208. DistributionConfig:
  209. Origins:
  210. - DomainName: !Select [1, !Split ["//", !GetAtt S3Bucket.WebsiteURL]]
  211. Id: pioneer-origin-s3
  212. CustomOriginConfig:
  213. OriginProtocolPolicy: http-only
  214. DefaultCacheBehavior:
  215. TargetOriginId: pioneer-origin-s3
  216. ViewerProtocolPolicy: redirect-to-https
  217. ForwardedValues:
  218. QueryString: true
  219. Enabled: true
  220. HttpVersion: http2
  221. Outputs:
  222. AutoScalingId:
  223. Description: The Auto Scaling ID
  224. Value: !Ref AutoScalingGroup
  225. Export:
  226. Name: !Sub "${AWS::StackName}AutoScalingGroup"
  227. RPCPublicIp:
  228. Description: The DNS name for the created instance
  229. Value: !Sub "${RPCInstance.PublicIp}"
  230. Export:
  231. Name: !Sub "${AWS::StackName}RPCPublicIp"
  232. BuildPublicIp:
  233. Description: The DNS name for the created instance
  234. Value: !Sub "${BuildInstance.PublicIp}"
  235. Export:
  236. Name: !Sub "${AWS::StackName}BuildPublicIp"
  237. S3BucketName:
  238. Value: !Ref S3Bucket
  239. Description: Name of S3 bucket to hold website content
  240. Export:
  241. Name: !Sub "${AWS::StackName}S3BucketName"
  242. DomainName:
  243. Description: CloudFront Domain Name
  244. Value: !Sub "${CloudFrontDistribution.DomainName}"
  245. Export:
  246. Name: !Sub "${AWS::StackName}DomainName"