-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy.yml
130 lines (115 loc) · 4.86 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
- hosts: 127.0.0.1
environment:
AWS_SECURITY_TOKEN: "{{ (ansible_env|default({})).AWS_SESSION_TOKEN|default('') }}"
connection: local
# Check for prerequisites
tasks:
- name: make sure we have java 1.8 installed.
shell: "java -version 2>&1 |grep 1.8"
register: javatest_result
failed_when: "javatest_result.rc != 0"
- name: make sure jq is installed
shell: "which jq"
register: jq_result
failed_when: "jq_result.rc != 0"
- name: make sure we have connection to AWS.
shell: "aws s3 ls"
register: aws_result
failed_when: "aws_result.rc != 0"
# Download requirements
- name: Get the api gateway importer
get_url:
url: https://circle-artifacts.com/gh/ryandub/aws-apigateway-importer/2/artifacts/0/home/ubuntu/aws-apigateway-importer/target/aws-apigateway-importer.jar
dest: ./aws-apigateway-importer.jar
# Zip and Upload Lambda Function
- name: Create S3 Bucket
s3: bucket={{lambda_s3_bucket}} mode=create
register: s3bucket
# !!! From here on out, we assume that if the bucket did not exist, this
# is a new deployment. If the bucket did exist, this is an update to
# an existing deployment. !!!
- name: Zip Lambda module
shell: cp -ar {{ module_name }} .tmp_lambda_dir
- shell: pip install -r requirements.txt -t ".tmp_lambda_dir/"
- shell: cd ".tmp_lambda_dir/"; zip -r ../{{ s3_key }} *; cd -
- name: Upload Lambda zip file to S3
s3:
bucket: "{{lambda_s3_bucket}}"
mode: put
object: "{{ s3_key }}"
src: "{{ s3_key }}"
overwrite: different
when:
s3bucket.changed
# Import API Gateway
- name: Update Swagger File
template: src=../swagger.yml dest=./swagger.ansible.yml
when:
s3bucket.changed
- name: Create the API from the swagger file
shell: java -jar aws-apigateway-importer.jar --region {{ region }} --create swagger.ansible.yml
when:
s3bucket.changed
- name: Get API Gateway ID
shell: aws apigateway get-rest-apis |jq -r '.items[] | select(.name | contains("{{ title }}")) | .id'
register: apigwid
# Create AWS Resources
- name: Create the cloudformation stack if does not exist.
cloudformation:
stack_name: "{{ stack_name }}"
state: "present"
region: "{{ region }}"
disable_rollback: true
template: "template.json"
template_parameters:
S3Bucket: "{{ lambda_s3_bucket }}"
S3Key: "{{ s3_key }}"
DynamoDBTable: "{{ dynamodb_table_name}}"
ApiId: "{{ apigwid.stdout }}"
register: stack
- name: Set swagger variables from cf output
set_fact:
LambdaArn: "{{ stack.stack_outputs['LambdaArn'] }}"
LambdaName: "{{ stack.stack_outputs['LambdaName'] }}"
LambdaRole: "{{ stack.stack_outputs['LambdaRole'] }}"
APIGatewayRole: "{{ stack.stack_outputs['APIGatewayRole'] }}"
# Add Integrations to API Gateway
- name: Update Swagger File
template: src=../swagger.yml dest=./swagger.ansible.yml
register: swagger
- name: Update the API Gateway
shell: java -jar aws-apigateway-importer.jar --region {{ region }} --update {{ apigwid.stdout }} swagger.ansible.yml
when: swagger.changed
# Deploy API Gateway to Stage
- name: Update Swagger File
template: src=../swagger.yml dest=./swagger.ansible.yml
- name: Deploy API Gateway to {{ api_gw_stage }}
shell: "aws apigateway create-deployment --rest-api-id {{ apigwid.stdout }} --stage-name {{ api_gw_stage }}"
when: swagger.changed
# Set Stage Variables
- name: Update Swagger File
template: src=../swagger.yml dest=./swagger.ansible.yml
- name: Update stage variables
shell: aws apigateway update-stage --rest-api-id {{ apigwid.stdout }} --stage-name {{ api_gw_stage }} --patch-operations op=replace,path=/variables/DBTable,value={{ dynamodb_table_name }}
# Download Generated Swagger File. It is recommended to use the
# Swagger file that gets generated here for import into other tools
# such as Runscope, Flex, Postman, or Doc generator.
- name: Export Swagger file from AWS.
shell: aws apigateway get-export --rest-api-id {{ apigwid.stdout }} --stage-name {{ api_gw_stage }} --export-type swagger ./swagger.awsexport.json
when: swagger.changed
- name: Cleanup temporary files
file: path=.tmp_lambda_dir state=absent
- file: path=swagger.ansible.yml state=absent
- file: path={{ s3_key }} state=absent
# Output useful variables
- shell: jq -r '.host' swagger.awsexport.json
register: awshost
- shell: jq -r '.basePath' swagger.awsexport.json
register: awsbasepath
- shell: jq -r '.paths | keys[0]' swagger.awsexport.json
register: awsfirstpath
- shell: echo {{ LambdaArn }} | awk -F ":" '{print $NF}'
register: lambda_function_name
- debug: msg="URL of API is https://{{ awshost.stdout }}{{ awsbasepath.stdout }}{{ awsfirstpath.stdout }}"
- debug: msg="Log Group is /aws/lambda/{{ lambda_function_name.stdout }}"