-
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.
Merge pull request #613 from qld-gov-au/QOLDEV-833-autoscaling-ami
QOLDEV-833 Run autoscaling groups on custom AMIs
- Loading branch information
Showing
12 changed files
with
184 additions
and
59 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,105 @@ | ||
--- | ||
- name: Create Amazon Machine Images | ||
hosts: local | ||
connection: local | ||
vars_files: | ||
- vars/AMI-template-instances.var.yml | ||
|
||
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 | ||
|
||
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.ansible_facts.cloudformation[stack_name].stack_outputs[layer + 'TemplateInstanceId'] }}" | ||
InstanceName: "{{ Environment }}-{{ service_name_lower }}-{{ layer }}-image" | ||
|
||
- name: Wait for instance startup | ||
shell: | | ||
STATUS=$(aws ec2 describe-instances --region {{ region }} --instance-ids {{ InstanceId }} --query "Reservations[].Instances[].State.Name" --output text) || return 1 | ||
echo "Instance {{ InstanceId }} status: $STATUS" >&2 | ||
for retry in `seq 1 6`; do | ||
if [ "$STATUS" = "pending" ]; then | ||
sleep 10 | ||
$(aws ec2 describe-instances --region {{ region }} --instance-ids {{ InstanceId }} --query "Reservations[].Instances[].State.Name" --output text) || return 1 | ||
echo "Instance {{ InstanceId }} status: $STATUS" >&2 | ||
else | ||
break | ||
fi | ||
done | ||
if [ "$STATUS" != "running" ]; then | ||
echo "Failed to start {{ InstanceId }}, status $STATUS - aborting" >&2 | ||
exit 2 | ||
fi | ||
- 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) || exit 1 | ||
echo "Deployment $DEPLOYMENT_ID: $STATUS" >&2 | ||
for retry in `seq 1 180`; do | ||
if [ "$STATUS" = "Pending" ] || [ "$STATUS" = "InProgress" ] || [ "$STATUS" = "None" ]; 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) || exit 1 | ||
echo "Instance {{ InstanceId }} deployment status: $STATUS" >&2 | ||
else | ||
break | ||
fi | ||
done | ||
if [ "$STATUS" != "Success" ]; then | ||
echo "Failed to deploy $DEPLOYMENT_ID, status $STATUS - aborting" >&2 | ||
exit 2 | ||
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 | replace(':', '-') }}" | ||
description: "Base image for {{ Environment }} {{ service_name }} {{ layer }} layer" | ||
tags: | ||
Name: "{{ InstanceName }}-{{ timestamp | replace(':', '-') }}" | ||
Environment: "{{ Environment }}" | ||
Service: "{{ service_name }}" | ||
Division: "{{ Division }}" | ||
Owner: "{{ Owner }}" | ||
Version: "1.0" | ||
register: new_image | ||
|
||
- name: Record AMI ID | ||
shell: | | ||
aws ssm put-parameter --type String --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" | ||
# ami-0146fc9ad419e2cfd: Amazon Linux 2023 AMI 2023.6.20241121.0 x86_64 HVM kernel-6.1 | ||
localhost region=ap-southeast-2 Owner="Development and Delivery" Division="Qld Online" base_ami="ami-0146fc9ad419e2cfd" |
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
Oops, something went wrong.