-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[QOLDEV-833] add AMI creation to build
- Start template instances after defining the stack and plugins, but before launching autoscaling instances - Stop template instances and generate AMIs from them - Provide the AMI IDs to the autoscaling instance creation
- Loading branch information
Showing
11 changed files
with
149 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
- name: Create Amazon Machine Images | ||
hosts: local | ||
connection: local | ||
|
||
pre_tasks: | ||
- name: get basic_facts | ||
set_fact: | ||
basic_fact={{ item }} | ||
when: item.Environment == Environment | ||
with_items: "{{ basic_facts }}" | ||
|
||
- name: set facts to environment from basic_fact | ||
set_fact: "{{ item.key }}={{ item.value }}" | ||
with_dict: "{{ basic_fact }}" | ||
when: basic_fact is defined | ||
|
||
- name: Set Timestamp fact | ||
set_fact: | ||
timestamp: "{{ lookup('pipe', 'date +%Y-%m-%dT%H:%M:%S%Z') }}" | ||
|
||
tasks: | ||
- name: Get stack name | ||
set_fact: | ||
stack_name: "{{ item.name }}" | ||
when: item.template_parameters.Environment == Environment | ||
with_items: "{{ cloudformation_stacks }}" | ||
|
||
- name: Get current stack facts | ||
cloudformation_facts: | ||
region: "{{ region }}" | ||
stack_name: "{{ stack_name }}" | ||
stack_resources: true | ||
register: opsworks_facts | ||
|
||
- name: Set new instance facts | ||
set_fact: | ||
InstanceId: "{{ opsworks_facts.stack_outputs[layer + 'TemplateInstanceId'] }}" | ||
InstanceName: "{{ Environment }}-{{ service_name_lower }}-{{ layer }}-image" | ||
|
||
- name: Wait for instance configuration | ||
shell: | | ||
STATUS=$(aws ssm list-commands --region {{ region }} --instance-id {{ InstanceId }} | ||
--filter "key=DocumentName,value=AWS-ApplyChefRecipes" --filter "key=InvokedAfter,value={{ timestamp }}" | ||
--query "Commands|[0].Status" --output text) || return 1 | ||
for retry in `seq 1 180`; do | ||
if [ "$STATUS" = "Pending" ] || [ "$STATUS" = "InProgress" ]; then | ||
sleep 20 | ||
STATUS=$(aws ssm list-commands --region {{ region }} --instance-id {{ InstanceId }} | ||
--filter "key=DocumentName,value=AWS-ApplyChefRecipes" --filter "key=InvokedAfter,value={{ timestamp }}" | ||
--query "Commands|[0].Status" --output text) || return 1 | ||
debug "Deployment $DEPLOYMENT_ID: $STATUS" | ||
else | ||
break | ||
fi | ||
done | ||
if [ "$STATUS" != "Success" ]; then | ||
debug "Failed to deploy $DEPLOYMENT_ID, status $STATUS - aborting" | ||
return 1 | ||
fi | ||
- name: Stop instance | ||
ec2: | ||
instance_ids: | ||
- "{{ InstanceId }}" | ||
region: "{{ Region }}" | ||
state: stopped | ||
wait: True | ||
|
||
- name: Create new ami | ||
ec2_ami: | ||
instance_id: "{{ InstanceId }}" | ||
region: "{{ Region }}" | ||
wait: yes | ||
name: "{{ InstanceName }}-{{ timestamp }}" | ||
description: "Base image for {{ Environment }} {{ service_name }} {{ layer }} layer" | ||
tags: | ||
Environment: "{{ Environment }}" | ||
Service: "{{ service_name }}" | ||
Division: "{{ Division }}" | ||
Owner: "{{ Owner }}" | ||
Version: "1.0" | ||
register: new_image | ||
|
||
- name: Record AMI ID | ||
aws_ssm_parameter: | ||
name: "/config/CKAN/{{ Environment }}/app/{{ service_name_lower }}/{{ layer }}AmiId" | ||
value: "{{ new_image.image_id }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
[local] | ||
localhost region=ap-southeast-2 Owner="Development and Delivery" Division="Qld Online" | ||
# Amazon Linux 2023: al2023-ami-2023.4.20240611.0-kernel-6.1-x86_64 | ||
localhost region=ap-southeast-2 Owner="Development and Delivery" Division="Qld Online" base_ami="ami-0e326862c8e74c0fe" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters