infrastructure.yml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Deploy inftrastructure required to run a new joystream chain.
  2. # This is comprised of:
  3. # - N validators
  4. # - One RPC node
  5. # - One Build instance
  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. VolumeSize:
  36. Description: Validator and Build instance volume size in GB
  37. Type: Number
  38. Default: 120
  39. RPCVolumeSize:
  40. Description: RPC Instance volume size in GB
  41. Type: Number
  42. Default: 120
  43. Conditions:
  44. HasAMIId: !Not [!Equals [!Ref EC2AMI, '']]
  45. Resources:
  46. SecurityGroup:
  47. Type: AWS::EC2::SecurityGroup
  48. Properties:
  49. GroupDescription: !Sub 'Internal Security group for validator nodes ${AWS::StackName}'
  50. SecurityGroupIngress:
  51. - IpProtocol: tcp
  52. FromPort: 30333
  53. ToPort: 30333
  54. CidrIp: 0.0.0.0/0
  55. - IpProtocol: tcp
  56. FromPort: 22
  57. ToPort: 22
  58. CidrIp: 0.0.0.0/0
  59. Tags:
  60. - Key: Name
  61. Value: !Sub '${AWS::StackName}_validator'
  62. RPCSecurityGroup:
  63. Type: AWS::EC2::SecurityGroup
  64. Properties:
  65. GroupDescription: !Sub 'Internal Security group for RPC nodes ${AWS::StackName}'
  66. SecurityGroupIngress:
  67. - IpProtocol: tcp
  68. FromPort: 9933
  69. ToPort: 9933
  70. CidrIp: 0.0.0.0/0
  71. - IpProtocol: tcp
  72. FromPort: 9944
  73. ToPort: 9944
  74. CidrIp: 0.0.0.0/0
  75. - IpProtocol: tcp
  76. FromPort: 30333
  77. ToPort: 30333
  78. CidrIp: 0.0.0.0/0
  79. - IpProtocol: tcp
  80. FromPort: 443
  81. ToPort: 443
  82. CidrIp: 0.0.0.0/0
  83. - IpProtocol: tcp
  84. FromPort: 80
  85. ToPort: 80
  86. CidrIp: 0.0.0.0/0
  87. - IpProtocol: tcp
  88. FromPort: 22
  89. ToPort: 22
  90. CidrIp: 0.0.0.0/0
  91. Tags:
  92. - Key: Name
  93. Value: !Sub '${AWS::StackName}_rpc'
  94. InstanceLaunchTemplate:
  95. Type: AWS::EC2::LaunchTemplate
  96. Metadata:
  97. AWS::CloudFormation::Init:
  98. config:
  99. packages:
  100. apt:
  101. wget: []
  102. unzip: []
  103. Properties:
  104. LaunchTemplateName: !Sub 'LaunchTemplate_${AWS::StackName}'
  105. LaunchTemplateData:
  106. ImageId: !If [HasAMIId, !Ref EC2AMI, !Ref DefaultAMI]
  107. InstanceType: !Ref EC2InstanceType
  108. KeyName: !Ref KeyName
  109. SecurityGroupIds:
  110. - !GetAtt SecurityGroup.GroupId
  111. BlockDeviceMappings:
  112. - DeviceName: /dev/sda1
  113. Ebs:
  114. VolumeSize: !Ref VolumeSize
  115. UserData:
  116. Fn::Base64: !Sub |
  117. #!/bin/bash -xe
  118. # send script output to /tmp so we can debug boot failures
  119. exec > /tmp/userdata.log 2>&1
  120. # Update all packages
  121. apt-get update -y
  122. # Prevent interactive prompts that would interrupt the installation
  123. export DEBIAN_FRONTEND=noninteractive
  124. # Install the updates
  125. apt-get upgrade -y
  126. apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
  127. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  128. 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
  129. apt-get update -y
  130. apt-get install -y docker-ce docker-ce-cli containerd.io
  131. usermod -aG docker ubuntu
  132. # Update docker-compose to 1.28+
  133. curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  134. chmod +x /usr/local/bin/docker-compose
  135. ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
  136. # Get latest cfn scripts and install them;
  137. apt-get install -y python3-setuptools
  138. mkdir -p /opt/aws/bin
  139. wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
  140. python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
  141. /opt/aws/bin/cfn-signal -e $? -r "Instance Created" '${WaitHandle}'
  142. AutoScalingGroup:
  143. Type: AWS::AutoScaling::AutoScalingGroup
  144. Properties:
  145. MinSize: '0'
  146. MaxSize: '10'
  147. DesiredCapacity: !Ref NumberOfValidators
  148. AvailabilityZones:
  149. Fn::GetAZs:
  150. Ref: 'AWS::Region'
  151. MixedInstancesPolicy:
  152. LaunchTemplate:
  153. LaunchTemplateSpecification:
  154. LaunchTemplateId: !Ref InstanceLaunchTemplate
  155. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  156. Overrides:
  157. - InstanceType: !Ref ValidatorEC2InstanceType
  158. Tags:
  159. - Key: Name
  160. Value: !Sub '${AWS::StackName}'
  161. PropagateAtLaunch: 'true'
  162. RPCInstance:
  163. Type: AWS::EC2::Instance
  164. Properties:
  165. SecurityGroupIds:
  166. - !GetAtt RPCSecurityGroup.GroupId
  167. InstanceType: !Ref RPCEC2InstanceType
  168. LaunchTemplate:
  169. LaunchTemplateId: !Ref InstanceLaunchTemplate
  170. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  171. BlockDeviceMappings:
  172. - DeviceName: /dev/sda1
  173. Ebs:
  174. VolumeSize: !Ref RPCVolumeSize
  175. Tags:
  176. - Key: Name
  177. Value: !Sub '${AWS::StackName}_rpc'
  178. BuildInstance:
  179. Type: AWS::EC2::Instance
  180. Properties:
  181. InstanceType: !Ref BuildEC2InstanceType
  182. LaunchTemplate:
  183. LaunchTemplateId: !Ref InstanceLaunchTemplate
  184. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  185. Tags:
  186. - Key: Name
  187. Value: !Sub '${AWS::StackName}_build'
  188. WaitHandle:
  189. Type: AWS::CloudFormation::WaitConditionHandle
  190. WaitCondition:
  191. Type: AWS::CloudFormation::WaitCondition
  192. Properties:
  193. Handle: !Ref 'WaitHandle'
  194. Timeout: '600'
  195. Count: !Ref NumberOfValidators
  196. Outputs:
  197. AutoScalingId:
  198. Description: The Auto Scaling ID
  199. Value: !Ref AutoScalingGroup
  200. Export:
  201. Name: !Sub '${AWS::StackName}AutoScalingGroup'
  202. RPCPublicIp:
  203. Description: The DNS name for the created instance
  204. Value: !Sub '${RPCInstance.PublicIp}'
  205. Export:
  206. Name: !Sub '${AWS::StackName}RPCPublicIp'
  207. BuildPublicIp:
  208. Description: The DNS name for the created instance
  209. Value: !Sub '${BuildInstance.PublicIp}'
  210. Export:
  211. Name: !Sub '${AWS::StackName}BuildPublicIp'
  212. BuildInstanceId:
  213. Description: Build instance ID
  214. Value: !Ref BuildInstance
  215. Export:
  216. Name: !Sub '${AWS::StackName}BuildInstanceId'