Files
dc-deployments-automation/roles/ec2-instance/tasks/main.yml
2020-10-21 10:48:42 +11:00

50 lines
1.4 KiB
YAML

---
- name: Fetch AMI list for region
ec2_ami_facts:
region: "{{ aws_region }}"
owner: "{{ ec2_ami_owner }}"
filters:
description: "{{ ec2_ami_description }}"
register: amis
- name: Find the latest version of the AMI
set_fact:
latest_ami: "{{ amis.images | selectattr('name', 'defined') | sort(attribute='creation_date') | last }}"
- name: Provision EC2 host
ec2:
exact_count: 1
count_tag:
Name: "{{instance_name}}"
keypair: "{{ ec2_keypair }}"
region: "{{ aws_region }}"
instance_type: "{{ ec2_type }}"
image: "{{ latest_ami.image_id }}"
group_id: "{{ sg.group_id }}"
vpc_subnet_id: "{{ subnet.subnet.id }}"
assign_public_ip: True
termination_protection: "{{ ec2_termination_protection }}"
volumes:
- device_name: /dev/xvda
volume_type: gp2
volume_size: "{{ ec2_disk_size }}"
wait: true
instance_tags:
Name: "{{ instance_name }}"
name: "{{ instance_name }}"
business_unit: "{{ business_unit }}"
service_name: "{{ instance_name}}"
resource_owner: "{{ resource_owner }}"
register: ec2_instance
- name: Add DNS entry for EC2 instance
route53:
command: create
zone: "{{ dns_domain }}"
type: CNAME
record: "{{ instance_hostname }}.{{ dns_domain }}"
value: "{{ ec2_instance.tagged_instances[0].dns_name }}"
ttl: 60
overwrite: true