You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
7.8 KiB

  1. AWSTemplateFormatVersion: 2010-09-09
  2. Description: AWS CloudFormation Template for doccano.
  3. Resources:
  4. AppSG:
  5. Type: 'AWS::EC2::SecurityGroup'
  6. Properties:
  7. GroupDescription: 'for the app nodes that allow ssh, http and docker ports'
  8. SecurityGroupIngress:
  9. - IpProtocol: tcp
  10. FromPort: '80'
  11. ToPort: '80'
  12. CidrIp: 0.0.0.0/0
  13. - IpProtocol: tcp
  14. FromPort: '443'
  15. ToPort: '443'
  16. CidrIp: 0.0.0.0/0
  17. - IpProtocol: tcp
  18. FromPort: '22'
  19. ToPort: '22'
  20. CidrIp: 0.0.0.0/0
  21. Metadata:
  22. 'AWS::CloudFormation::Designer':
  23. id: 116a7f7b-14c5-489a-a3c8-faf276be7ab0
  24. App:
  25. Type: 'AWS::EC2::Instance'
  26. Properties:
  27. InstanceType: !Ref InstanceType
  28. ImageId: !Ref Ubuntu16Ami
  29. KeyName: !Ref KeyName
  30. SecurityGroups:
  31. - !Ref AppSG
  32. UserData: !Base64
  33. 'Fn::Join':
  34. - ''
  35. - - |
  36. #!/usr/bin/env bash
  37. - |
  38. sudo apt update
  39. - >
  40. sudo apt install -y apt-transport-https ca-certificates curl
  41. software-properties-common
  42. - >
  43. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo
  44. apt-key add -
  45. - >
  46. sudo add-apt-repository "deb [arch=amd64]
  47. https://download.docker.com/linux/ubuntu bionic stable"
  48. - |
  49. sudo apt update
  50. - |
  51. apt-cache policy docker-ce
  52. - |
  53. sudo apt install -y docker-ce
  54. - |
  55. sudo usermod -aG docker ${USER}
  56. - |
  57. touch /env.list
  58. - sudo tee -a /env.list <<< ADMIN=
  59. - !Ref AdminUserName
  60. - |+
  61. - sudo tee -a /env.list <<< EMAIL=
  62. - !Ref AdminEmail
  63. - |+
  64. - sudo tee -a /env.list <<< PASSWORD=
  65. - !Ref AdminPassword
  66. - |+
  67. - sudo tee -a /env.list <<< DEBUG=
  68. - !Ref Debug
  69. - |+
  70. - sudo tee -a /env.list <<< SECRET_KEY=
  71. - !Ref SecretKey
  72. - |+
  73. - sudo tee -a /env.list <<< EMAIL_USE_TLS=
  74. - !Ref EMailUseTSL
  75. - |+
  76. - sudo tee -a /env.list <<< EMAIL_HOST=
  77. - !Ref EMailHost
  78. - |+
  79. - sudo tee -a /env.list <<< EMAIL_PORT=
  80. - !Ref EMailHostPort
  81. - |+
  82. - sudo tee -a /env.list <<< EMAIL_HOST_USER=
  83. - !Ref EMailHostUser
  84. - |+
  85. - sudo tee -a /env.list <<< EMAIL_HOST_PASSWORD=
  86. - !Ref EMailHostPassword
  87. - |+
  88. - |
  89. sudo docker pull doccano/doccano:latest
  90. - |
  91. set -a
  92. - |
  93. source /env.list
  94. - |
  95. set +a
  96. - >
  97. sudo docker run -d --name doccano --env-file /env.list -p 80:8000
  98. doccano/doccano:latest
  99. - >
  100. sudo docker exec doccano tools/create-admin.sh ${ADMIN} ${EMAIL}
  101. ${PASSWORD}
  102. Metadata:
  103. 'AWS::CloudFormation::Designer':
  104. id: 3547c02e-5393-4b26-a9af-6f00dc2cbcdb
  105. DescribeImagesRole:
  106. Type: AWS::IAM::Role
  107. Properties:
  108. AssumeRolePolicyDocument:
  109. Version: '2012-10-17'
  110. Statement:
  111. - Action: sts:AssumeRole
  112. Effect: Allow
  113. Principal:
  114. Service: lambda.amazonaws.com
  115. ManagedPolicyArns:
  116. - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
  117. Policies:
  118. - PolicyName: DescribeImages
  119. PolicyDocument:
  120. Version: '2012-10-17'
  121. Statement:
  122. - Action: ec2:DescribeImages
  123. Effect: Allow
  124. Resource: "*"
  125. GetLatestAMI:
  126. Type: AWS::Lambda::Function
  127. Properties:
  128. Runtime: python3.6
  129. Handler: index.handler
  130. Role: !Sub ${DescribeImagesRole.Arn}
  131. Timeout: 60
  132. Code:
  133. ZipFile: |
  134. import boto3
  135. import cfnresponse
  136. import json
  137. import traceback
  138. def handler(event, context):
  139. try:
  140. response = boto3.client('ec2').describe_images(
  141. Owners=[event['ResourceProperties']['Owner']],
  142. Filters=[
  143. {'Name': 'name', 'Values': [event['ResourceProperties']['Name']]},
  144. {'Name': 'architecture', 'Values': [event['ResourceProperties']['Architecture']]},
  145. {'Name': 'root-device-type', 'Values': ['ebs']},
  146. ],
  147. )
  148. amis = sorted(response['Images'],
  149. key=lambda x: x['CreationDate'],
  150. reverse=True)
  151. id = amis[0]['ImageId']
  152. cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, id)
  153. except:
  154. traceback.print_last()
  155. cfnresponse.send(event, context, cfnresponse.FAIL, {}, "ok")
  156. Ubuntu16Ami:
  157. Type: Custom::FindAMI
  158. Properties:
  159. ServiceToken: !Sub ${GetLatestAMI.Arn}
  160. Owner: "099720109477"
  161. Name: "ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20190913"
  162. Architecture: "x86_64"
  163. Metadata:
  164. 'AWS::CloudFormation::Designer':
  165. 116a7f7b-14c5-489a-a3c8-faf276be7ab0:
  166. size:
  167. width: 60
  168. height: 60
  169. position:
  170. x: 60
  171. 'y': 210
  172. z: 1
  173. embeds: []
  174. 3547c02e-5393-4b26-a9af-6f00dc2cbcdb:
  175. size:
  176. width: 60
  177. height: 60
  178. position:
  179. x: 180
  180. 'y': 210
  181. z: 1
  182. embeds: []
  183. isassociatedwith:
  184. - 116a7f7b-14c5-489a-a3c8-faf276be7ab0
  185. Parameters:
  186. InstanceType:
  187. Description: Server EC2 instance type
  188. Type: String
  189. Default: t2.micro
  190. AllowedValues:
  191. - t1.micro
  192. - t2.micro
  193. - t2.small
  194. - t2.medium
  195. - t3.micro
  196. - t3.small
  197. - t3.medium
  198. ConstraintDescription: must be a valid EC2 instance type.
  199. KeyName:
  200. Description: Name of an EC2 KeyPair to enable SSH access to the instance.
  201. Type: 'AWS::EC2::KeyPair::KeyName'
  202. ConstraintDescription: must be the name of an existing EC2 KeyPair.
  203. AdminUserName:
  204. Description: The admin account user name
  205. Default: 'admin'
  206. Type: String
  207. MinLength: 1
  208. MaxLength: 16
  209. AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
  210. AdminEmail:
  211. Default: admin@gmail.com
  212. Description: The admin account user email
  213. Type: String
  214. AllowedPattern: '^[\x20-\x45]?[\w-\+]+(\.[\w]+)*@[\w-]+(\.[\w]+)*(\.[a-z]{2,})$'
  215. AdminPassword:
  216. Description: The admin account password
  217. Type: String
  218. NoEcho: true
  219. MinLength: 1
  220. MaxLength: 16
  221. AllowedPattern: '^[a-zA-Z0-9]*$'
  222. Debug:
  223. Default: 'False'
  224. AllowedValues:
  225. - 'False'
  226. - 'True'
  227. Description: Debug mode or not
  228. Type: String
  229. AllowedPattern: '^[a-zA-Z0-9]*$'
  230. SecretKey:
  231. Default: zICc59rlKXmlFRpG
  232. Description: Secret key for Django
  233. Type: String
  234. AllowedPattern: '^[a-zA-Z0-9]*$'
  235. EMailUseTSL:
  236. Default: 'False'
  237. AllowedValues:
  238. - 'False'
  239. - 'True'
  240. Description: SMTP will use TSL
  241. Type: String
  242. AllowedPattern: '^[a-zA-Z0-9]*$'
  243. EMailHost:
  244. Description: SMTP Host
  245. Type: String
  246. AllowedPattern: '^[\w-]+(\.[\w-]+)*(\.[a-z]{2,})$'
  247. EMailHostUser:
  248. Description: SMTP Host User
  249. Type: String
  250. MinLength: 1
  251. MaxLength: 25
  252. AllowedPattern: '^[a-zA-Z0-9][a-zA-Z0-9]{1,25}$'
  253. EMailHostPassword:
  254. Description: SMTP Host User password
  255. Type: String
  256. NoEcho: true
  257. MinLength: 0
  258. MaxLength: 60
  259. AllowedPattern: '^\p{Graph}+$'
  260. EMailHostPort:
  261. Description: SMTP Port
  262. Type: String
  263. MinLength: 1
  264. MaxLength: 5
  265. AllowedPattern: '^\d+$'
  266. Outputs:
  267. PublicDNS:
  268. Value: !GetAtt
  269. - App
  270. - PublicDnsName
  271. Description: Newly created server DNS address