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.

313 lines
8.6 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. - sudo tee -a /env.list <<< WORKERS=
  89. - !Ref WorkersCount
  90. - |+
  91. - sudo tee -a /env.list <<< DEFAULT_FROM_EMAIL=
  92. - !Ref FromEmail
  93. - |+
  94. - sudo tee -a /env.list <<< DOCKER_IMAGE=
  95. - !Ref DockerImageName
  96. - |+
  97. - |
  98. set -a
  99. - |
  100. source /env.list
  101. - |
  102. set +a
  103. - |
  104. sudo docker pull ${DOCKER_IMAGE}
  105. - >
  106. sudo docker run -d --name doccano --env-file /env.list -p 80:8000
  107. ${DOCKER_IMAGE}
  108. - >
  109. sleep 3 && sudo docker exec doccano tools/create-admin.sh ${ADMIN} ${EMAIL}
  110. ${PASSWORD}
  111. Metadata:
  112. 'AWS::CloudFormation::Designer':
  113. id: 3547c02e-5393-4b26-a9af-6f00dc2cbcdb
  114. DescribeImagesRole:
  115. Type: AWS::IAM::Role
  116. Properties:
  117. AssumeRolePolicyDocument:
  118. Version: '2012-10-17'
  119. Statement:
  120. - Action: sts:AssumeRole
  121. Effect: Allow
  122. Principal:
  123. Service: lambda.amazonaws.com
  124. ManagedPolicyArns:
  125. - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
  126. Policies:
  127. - PolicyName: DescribeImages
  128. PolicyDocument:
  129. Version: '2012-10-17'
  130. Statement:
  131. - Action: ec2:DescribeImages
  132. Effect: Allow
  133. Resource: "*"
  134. GetLatestAMI:
  135. Type: AWS::Lambda::Function
  136. Properties:
  137. Runtime: python3.6
  138. Handler: index.handler
  139. Role: !Sub ${DescribeImagesRole.Arn}
  140. Timeout: 60
  141. Code:
  142. ZipFile: |
  143. import boto3
  144. import cfnresponse
  145. import json
  146. import traceback
  147. def handler(event, context):
  148. try:
  149. response = boto3.client('ec2').describe_images(
  150. Owners=[event['ResourceProperties']['Owner']],
  151. Filters=[
  152. {'Name': 'name', 'Values': [event['ResourceProperties']['Name']]},
  153. {'Name': 'architecture', 'Values': [event['ResourceProperties']['Architecture']]},
  154. {'Name': 'root-device-type', 'Values': ['ebs']},
  155. ],
  156. )
  157. amis = sorted(response['Images'],
  158. key=lambda x: x['CreationDate'],
  159. reverse=True)
  160. id = amis[0]['ImageId']
  161. cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, id)
  162. except:
  163. traceback.print_last()
  164. cfnresponse.send(event, context, cfnresponse.FAIL, {}, "ok")
  165. Ubuntu16Ami:
  166. Type: Custom::FindAMI
  167. Properties:
  168. ServiceToken: !Sub ${GetLatestAMI.Arn}
  169. Owner: "099720109477"
  170. Name: "ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20190913"
  171. Architecture: "x86_64"
  172. Metadata:
  173. 'AWS::CloudFormation::Designer':
  174. 116a7f7b-14c5-489a-a3c8-faf276be7ab0:
  175. size:
  176. width: 60
  177. height: 60
  178. position:
  179. x: 60
  180. 'y': 210
  181. z: 1
  182. embeds: []
  183. 3547c02e-5393-4b26-a9af-6f00dc2cbcdb:
  184. size:
  185. width: 60
  186. height: 60
  187. position:
  188. x: 180
  189. 'y': 210
  190. z: 1
  191. embeds: []
  192. isassociatedwith:
  193. - 116a7f7b-14c5-489a-a3c8-faf276be7ab0
  194. Parameters:
  195. InstanceType:
  196. Description: Server EC2 instance type
  197. Type: String
  198. Default: t2.micro
  199. AllowedValues:
  200. - t1.micro
  201. - t2.micro
  202. - t2.small
  203. - t2.medium
  204. - t3.micro
  205. - t3.small
  206. - t3.medium
  207. ConstraintDescription: must be a valid EC2 instance type.
  208. KeyName:
  209. Description: Name of an EC2 KeyPair to enable SSH access to the instance.
  210. Type: 'AWS::EC2::KeyPair::KeyName'
  211. ConstraintDescription: must be the name of an existing EC2 KeyPair.
  212. AdminUserName:
  213. Description: The admin account user name
  214. Default: 'admin'
  215. Type: String
  216. MinLength: 1
  217. MaxLength: 16
  218. AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
  219. AdminEmail:
  220. Default: admin@gmail.com
  221. Description: The admin account user email
  222. Type: String
  223. AllowedPattern: '^[\x20-\x45]?[\w-\+]+(\.[\w]+)*@[\w-]+(\.[\w]+)*(\.[a-z]{2,})$'
  224. AdminPassword:
  225. Description: The admin account password
  226. Type: String
  227. NoEcho: true
  228. MinLength: 1
  229. MaxLength: 16
  230. AllowedPattern: '^[a-zA-Z0-9]*$'
  231. Debug:
  232. Default: 'False'
  233. AllowedValues:
  234. - 'False'
  235. - 'True'
  236. Description: Debug mode or not
  237. Type: String
  238. AllowedPattern: '^[a-zA-Z0-9]*$'
  239. SecretKey:
  240. Default: zICc59rlKXmlFRpG
  241. Description: Secret key for Django
  242. Type: String
  243. AllowedPattern: '^[a-zA-Z0-9]*$'
  244. EMailUseTSL:
  245. Default: 'False'
  246. AllowedValues:
  247. - 'False'
  248. - 'True'
  249. Description: SMTP will use TSL
  250. Type: String
  251. AllowedPattern: '^[a-zA-Z0-9]*$'
  252. EMailHost:
  253. Description: SMTP Host
  254. Type: String
  255. AllowedPattern: '^[\w-]+(\.[\w-]+)*(\.[a-z]{2,})$'
  256. EMailHostUser:
  257. Description: SMTP Host User
  258. Type: String
  259. MinLength: 1
  260. MaxLength: 25
  261. AllowedPattern: '^[a-zA-Z0-9][a-zA-Z0-9]{1,25}$'
  262. EMailHostPassword:
  263. Description: SMTP Host User password
  264. Type: String
  265. NoEcho: true
  266. MinLength: 0
  267. MaxLength: 60
  268. AllowedPattern: '^\p{Graph}+$'
  269. EMailHostPort:
  270. Description: SMTP Port
  271. Type: String
  272. MinLength: 1
  273. MaxLength: 5
  274. AllowedPattern: '^\d+$'
  275. WorkersCount:
  276. Description: Workers Count
  277. Type: String
  278. MinLength: 1
  279. MaxLength: 3
  280. AllowedPattern: '^\d+$'
  281. FromEmail:
  282. Default: webmaster@localhost
  283. Description: The email used to send from singup messages
  284. Type: String
  285. AllowedPattern: '^[\x20-\x45]?[\w-\+]+(\.[\w]+)*@[\w-]+(\.[\w]+)*(\.[a-z]{2,})?$'
  286. DockerImageName:
  287. Default: doccano/doccano:latest
  288. Description: The Docker image name
  289. Type: String
  290. AllowedPattern: '^[\w_-]+/[\w_-]+:[\w_-]+$'
  291. Outputs:
  292. PublicDNS:
  293. Value: !GetAtt
  294. - App
  295. - PublicDnsName
  296. Description: Newly created server DNS address