-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcloudformation.yaml
More file actions
111 lines (93 loc) · 3.86 KB
/
cloudformation.yaml
File metadata and controls
111 lines (93 loc) · 3.86 KB
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
---
- name: provision stack
hosts: all
connection: local
vars:
working_dirs:
- json
- tmp
- lambda
tasks:
- name: include vars
include_vars: vars.yaml
- name: Create the temporary directories
file: dest={{ item }} state=directory
with_items: working_dirs
- name: Generate CloudFormation templates from troposphere
local_action: >
shell python {{ item }} > {{ playbook_dir }}/json/{{ item | basename | replace('.py', '.json') }}
with_fileglob:
- "{{ playbook_dir }}/templates/*.py"
- name: Validate the cloudformation descriptions
local_action: >
shell aws cloudformation validate-template --region {{ region }} --template-body file://{{ item }} --output text
with_fileglob:
- "{{ playbook_dir }}/json/*.json"
- name: set fact for time_token
set_fact: time_token="{{ ansible_date_time.iso8601 }}"
- name: Create Lambda Role
cloudformation: >
stack_name="{{ stack_prefix }}-role"
state=present
region="{{region}}"
template={{ playbook_dir }}/json/iam-assets.json
args:
template_parameters:
RdsInstance: "{{ rds_identifier }}"
register: iam
- name: Create Lambda Bucket
cloudformation: >
stack_name="{{ stack_prefix }}-bucket"
state=present
region="{{region}}"
template={{ playbook_dir }}/json/s3.json
register: s3
- name: Create Alarms
cloudformation: >
stack_name="{{ stack_prefix }}-alarms"
state=present
region="{{region}}"
template={{ playbook_dir }}/json/alarms.json
args:
template_parameters:
RdsInstance: "{{ rds_identifier }}"
UpThreshold: "{{ scale_up['threshold'] }}"
UpEvaluations: "{{ scale_up['alarm_duration'] }}"
DownThreshold: "{{ scale_down['threshold'] }}"
DownEvaluations: "{{ scale_down['alarm_duration'] }}"
CreditThreshold: "{{ credits['threshold'] }}"
CreditEvaluations: "{{ credits['alarm_duration'] }}"
register: alarms
- name: Write output of the alarm names to vars
copy: content="---\nalarm_high{{ ':' }} {{ alarms['stack_outputs']['UpAlarm'] }}\nalarm_low{{ ':' }} {{ alarms['stack_outputs']['DownAlarm'] }}\nalarm_credits{{ ':' }} {{ alarms['stack_outputs']['CreditLowAlarm'] }}\n..." dest="{{ playbook_dir }}/tmp/alarms.yaml"
- name: Unzip deps
unarchive: src={{ playbook_dir }}/reds/deps.zip dest={{ playbook_dir }}/lambda
- name: Copy script into working folder
local_action: >
shell cp {{ playbook_dir }}/reds/reds.py {{ playbook_dir }}/lambda/reds.py
- name: Create local zip
local_action: >
shell cd {{ playbook_dir }}/lambda && zip -r {{ playbook_dir }}/tmp/reds-{{ time_token }}.zip ./* && zip -j {{ playbook_dir }}/tmp/reds-{{ time_token }}.zip {{ playbook_dir }}/vars.yaml {{ playbook_dir }}/tmp/alarms.yaml
- name: Upload python files to S3 bucket
local_action: >
shell aws s3 cp {{ playbook_dir }}/tmp/reds-{{ time_token }}.zip s3://{{ s3['stack_outputs']['BucketName'] }}
- name: Create Lambda Function
cloudformation: >
stack_name="{{ stack_prefix }}-lambda"
state=present
region="{{region}}"
template={{ playbook_dir }}/json/lambda-function.json
args:
template_parameters:
TimeToken: "{{ time_token }}"
BucketName: "{{ s3['stack_outputs']['BucketName'] }}"
RdsInstance: "{{ rds_identifier }}"
LambdaRole: "{{ iam['stack_outputs']['LambdaRole'] }}"
register: lambda
- name: Clean up after ourselves
file: path="{{ playbook_dir }}/{{ item }}" state=absent
with_items: working_dirs
- name: "Add reminder to manually set the recurring "
debug: msg="~~~ Reminder!! - Go set the recurring lambda call manually - https://github.com/mediatemple/ReDS ~~~"
with_sequence: start=0 end=3
...