infrastructure.yml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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: 22
  74. ToPort: 22
  75. CidrIp: 0.0.0.0/0
  76. Tags:
  77. - Key: Name
  78. Value: !Sub '${AWS::StackName}_rpc'
  79. InstanceLaunchTemplate:
  80. Type: AWS::EC2::LaunchTemplate
  81. Metadata:
  82. AWS::CloudFormation::Init:
  83. config:
  84. packages:
  85. apt:
  86. wget: []
  87. unzip: []
  88. Properties:
  89. LaunchTemplateName: !Sub 'LaunchTemplate_${AWS::StackName}'
  90. LaunchTemplateData:
  91. ImageId: !If [HasAMIId, !Ref EC2AMI, !Ref DefaultAMI]
  92. InstanceType: !Ref EC2InstanceType
  93. KeyName: !Ref KeyName
  94. SecurityGroupIds:
  95. - !GetAtt SecurityGroup.GroupId
  96. BlockDeviceMappings:
  97. - DeviceName: /dev/sda1
  98. Ebs:
  99. VolumeSize: '40'
  100. UserData:
  101. Fn::Base64: !Sub |
  102. #!/bin/bash -xe
  103. # send script output to /tmp so we can debug boot failures
  104. exec > /tmp/userdata.log 2>&1
  105. # Update all packages
  106. apt-get update -y
  107. # Install the updates
  108. apt-get upgrade -y
  109. # Get latest cfn scripts and install them;
  110. apt-get install -y python3-setuptools
  111. mkdir -p /opt/aws/bin
  112. wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
  113. python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
  114. /opt/aws/bin/cfn-signal -e $? -r "Instance Created" '${WaitHandle}'
  115. AutoScalingGroup:
  116. Type: AWS::AutoScaling::AutoScalingGroup
  117. Properties:
  118. MinSize: '0'
  119. MaxSize: '10'
  120. DesiredCapacity: !Ref NumberOfValidators
  121. AvailabilityZones:
  122. Fn::GetAZs:
  123. Ref: "AWS::Region"
  124. MixedInstancesPolicy:
  125. LaunchTemplate:
  126. LaunchTemplateSpecification:
  127. LaunchTemplateId: !Ref InstanceLaunchTemplate
  128. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  129. Overrides:
  130. - InstanceType: !Ref ValidatorEC2InstanceType
  131. Tags:
  132. - Key: Name
  133. Value: !Sub '${AWS::StackName}'
  134. PropagateAtLaunch: "true"
  135. RPCInstance:
  136. Type: AWS::EC2::Instance
  137. Properties:
  138. SecurityGroupIds:
  139. - !GetAtt RPCSecurityGroup.GroupId
  140. InstanceType: !Ref RPCEC2InstanceType
  141. LaunchTemplate:
  142. LaunchTemplateId: !Ref InstanceLaunchTemplate
  143. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  144. Tags:
  145. - Key: Name
  146. Value: !Sub '${AWS::StackName}_rpc'
  147. BuildInstance:
  148. Type: AWS::EC2::Instance
  149. Properties:
  150. InstanceType: !Ref BuildEC2InstanceType
  151. LaunchTemplate:
  152. LaunchTemplateId: !Ref InstanceLaunchTemplate
  153. Version: !GetAtt InstanceLaunchTemplate.LatestVersionNumber
  154. Tags:
  155. - Key: Name
  156. Value: !Sub '${AWS::StackName}_build'
  157. WaitHandle:
  158. Type: AWS::CloudFormation::WaitConditionHandle
  159. WaitCondition:
  160. Type: AWS::CloudFormation::WaitCondition
  161. Properties:
  162. Handle: !Ref 'WaitHandle'
  163. Timeout: '600'
  164. Count: !Ref NumberOfValidators
  165. S3Bucket:
  166. Type: AWS::S3::Bucket
  167. Properties:
  168. AccessControl: PublicRead
  169. WebsiteConfiguration:
  170. IndexDocument: index.html
  171. BucketPolicy:
  172. Type: AWS::S3::BucketPolicy
  173. Properties:
  174. PolicyDocument:
  175. Id: PublicPolicy
  176. Version: 2012-10-17
  177. Statement:
  178. - Sid: PublicReadForGetBucketObjects
  179. Effect: Allow
  180. Principal: '*'
  181. Action: 's3:GetObject'
  182. Resource: !Sub "arn:aws:s3:::${S3Bucket}/*"
  183. Bucket: !Ref S3Bucket
  184. CloudFrontDistribution:
  185. Type: AWS::CloudFront::Distribution
  186. Properties:
  187. DistributionConfig:
  188. Origins:
  189. - DomainName: !Select [1, !Split ["//", !GetAtt S3Bucket.WebsiteURL]]
  190. Id: pioneer-origin-s3
  191. CustomOriginConfig:
  192. OriginProtocolPolicy: http-only
  193. DefaultCacheBehavior:
  194. TargetOriginId: pioneer-origin-s3
  195. ViewerProtocolPolicy: redirect-to-https
  196. ForwardedValues:
  197. QueryString: true
  198. Enabled: true
  199. HttpVersion: http2
  200. Outputs:
  201. AutoScalingId:
  202. Description: The Auto Scaling ID
  203. Value: !Ref AutoScalingGroup
  204. Export:
  205. Name: !Sub "${AWS::StackName}AutoScalingGroup"
  206. RPCPublicIp:
  207. Description: The DNS name for the created instance
  208. Value: !Sub "${RPCInstance.PublicIp}"
  209. Export:
  210. Name: !Sub "${AWS::StackName}RPCPublicIp"
  211. BuildPublicIp:
  212. Description: The DNS name for the created instance
  213. Value: !Sub "${BuildInstance.PublicIp}"
  214. Export:
  215. Name: !Sub "${AWS::StackName}BuildPublicIp"
  216. S3BucketName:
  217. Value: !Ref S3Bucket
  218. Description: Name of S3 bucket to hold website content
  219. Export:
  220. Name: !Sub "${AWS::StackName}S3BucketName"
  221. DomainName:
  222. Description: CloudFront Domain Name
  223. Value: !Sub "${CloudFrontDistribution.DomainName}"
  224. Export:
  225. Name: !Sub "${AWS::StackName}DomainName"