export AWS_ACCESS_KEY_ID='' export AWS_SECRET_ACCESS_KEY=''
---
# This playbook deploys the whole AWS Network, Bastion, Web, and RDS Instances. Web Configuration is currently manual
# - Must have credentials exported for AWS IAM
# - RDS may take up to 30 minutes to deploy the instance
- name: Deploy RDS Infrastructure
hosts: localhost
connection: local
gather_facts: false
vars:
# Global AWS Variables
region: us-west-2
aws_network_name: dev
# VPC Variables
vpc_cidr: 10.4.0.0/16
public_subnet_cidr: 10.4.0.0/24
public_subnet_az: us-west-2a
private_subnet_1_cidr: 10.4.1.0/24
private_subnet_1_az: us-west-2b
private_subnet_2_cidr: 10.4.2.0/24
private_subnet_2_az: us-west-2c
# Bastion Variables
ec2_id: "ami-4836a428"
key_pair: "pythian-markwardt-west"
# RDS Variables
rds_user: root
rds_pass:
rds_instance_type: db.m1.small
rds_size_gb: 15
rds_parameter_engine: mysql5.6
rds_instance_engine: 5.6.34
rds_parameters:
- { param: 'binlog_format', value: 'ROW' }
- { param: 'general_log', value: '1' }
roles:
- aws-network
- aws-bastion
- aws-rds
--- - name: Build VPC ec2_vpc_net: name: "-vpc" state: present cidr_block: "" region: "" - name: Get VPC ID ec2_vpc_net_facts: region: "" filters: "tag:Name": "-vpc" register: vpc_facts - name: VPC ID debug: var: vpc_facts['vpcs'][0]['id'] - name: Create Private Subnet 1 ec2_vpc_subnet: state: present vpc_id: "" cidr: "" region: "" az: "" resource_tags: Name: "-private1" register: private_subnet_1 - name: subnet MySQL database servers ID 1 debug: var: private_subnet_1['subnet']['id'] - name: Create Private Subnet 2 ec2_vpc_subnet: state: present vpc_id: "" cidr: "" region: "" az: "" resource_tags: Name: "-private2" register: private_subnet_2 - name: subnet MySQL database servers ID 2 debug: var: private_subnet_2['subnet']['id'] - name: Create public subnet ec2_vpc_subnet: state: present vpc_id: "" cidr: "" region: "" az: "" resource_tags: Name: "-public" register: public_subnet - name: Subnet public ID debug: var: public_subnet['subnet']['id'] - name: Create internet gateway ec2_vpc_igw: vpc_id: "" state: present region: "" register: igw - name: Internet gateway ID debug: var: igw['gateway_id'] - name: Set up public subnet route table ec2_vpc_route_table: vpc_id: "" region: "" state: present tags: Name: "-public" subnets: - "" - "" - "" routes: - dest: 0.0.0.0/0 gateway_id: "" register: public_route_table - name: Bastion Security group ec2_group: name: "-bastion" state: present description: Security group for SSH Bastion to get into the servers vpc_id: "" region: "" rules: - proto: tcp from_port: 22 to_port: 22 cidr_ip: 0.0.0.0/0 register: bastion_sg - name: Bastion Security group ID debug: var: bastion_sg['group_id'] - name: Web Security group ec2_group: name: "-web" state: present description: Security group for SSH Bastion to get into the servers vpc_id: "" region: "" rules: - proto: tcp from_port: 80 to_port: 80 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 22 to_port: 22 group_id: "" register: web_sg - name: Web Security group ID debug: var: web_sg['group_id'] - name: MySQL Security group ec2_group: name: "-private" state: present description: Security group for private access vpc_id: "" region: "" rules: - proto: tcp from_port: 80 to_port: 80 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 22 to_port: 22 group_id: "" - proto: tcp from_port: 3306 to_port: 3306 group_id: "" register: mysql_sg - name: MySQL Security group ID debug: var: mysql_sg['group_id']
--- - name: "Get Public Subnet ID" ec2_vpc_subnet_facts: region: "" filters: "tag:Name": "-public" register: public_subnet - name: Subnet ID debug: var: public_subnet['subnets'][0]['id'] - name: Get bastion SG ID ec2_group_facts: region: "" filters: group-name: "-bastion" register: bastion_sg - name: Bastion SG ID debug: var: bastion_sg['security_groups'][0]['group_id'] - name: Create Bastion ec2: region: "" key_name: "" group_id: "" instance_type: t2.micro image: "" wait: yes wait_timeout: 500 exact_count: 1 instance_tags: Name: "-bastion" Environment: Dev count_tag: Name: "-bastion" Environment: Dev vpc_subnet_id: "" assign_public_ip: yes register: bastion_facts - debug: "var=bastion_facts" - name: Capture Bastion public IP set_fact: bastion_public_ip: "" when: bastion_facts['instances'] | length > 0 - name: Capture Bastion public IP set_fact: bastion_public_ip: "" when: bastion_facts['tagged_instances'] | length > 0 - name: Display Public IP debug: var: bastion_public_ip
---
# tasks file for aws-rds
- name: "Get Subnet ID for markwardt-private1"
ec2_vpc_subnet_facts:
region: ""
filters:
"tag:Name": "-private1"
register: subnet_private1
- name: "Get Subnet ID for -private2"
ec2_vpc_subnet_facts:
region: ""
filters:
"tag:Name": "-private2"
register: subnet_private2
- name: Get MySQL SG ID
ec2_group_facts:
region: ""
filters:
group-name: "-private"
register: private_sg
- name: Build RDS Subnet Group
rds_subnet_group:
region: ""
state: present
name: "-subnetgroup"
description: Subnet Group for
subnets:
- ""
- ""
register: subnet_group_results
- name: Subnet group results
debug:
var: subnet_group_results
- name: Build MySQL Parameters
rds_param_group:
state: present
name: "-parameters"
description: " Parameters"
engine: ""
immediate: no
region: ""
params: "{{item.param}}={{item.value}}"
with_items: ""
- name: Build RDS Instance
rds:
command: create
instance_name: "-rds"
db_engine: MySQL
size: ""
instance_type: ""
username: ""
password: ""
region: ""
subnet: "-subnetgroup"
parameter_group: "-parameters"
engine_version: ""
vpc_security_groups: ""
wait: yes
wait_timeout: 1800
multi_zone: yes
tags:
Environment: ""
register: rds_results
- name: RDS rds results
debug:
var: rds_results
- hosts: localhost connection: local vars: region: "us-west-2" aws_network_name: dev tasks: - name: Remove RDS Instance rds: region: "" command: delete instance_name: "-rds" wait: yes wait_timeout: 1800 - name: Remove RDS Subnet Group rds_subnet_group: region: "" state: absent name: "-subnetgroup" - name: Build MySQL Parameters rds_param_group: region: "" state: absent name: "-parameters"AWS Bastion Cleanup Tasks
- hosts: localhost
connection: local
vars:
region: "us-west-2"
aws_network_name: dev
tasks:
- name: Get Bastion EC2 ID
ec2_remote_facts:
region: ""
filters:
"tag:Name": "-bastion"
register: ec2_facts
- name: Destroy Bastion
ec2:
instance_ids: "{{item.id}}"
state: absent
region: ""
with_items: ""
AWS Network Cleanup Tasks
- hosts: localhost connection: local vars: region: "us-west-2" aws_network_name: dev vpc_cidr: 10.4.0.0/16 public_subnet_cidr: 10.4.0.0/24 private_subnet_1_cidr: 10.4.1.0/24 private_subnet_2_cidr: 10.4.2.0/24 gather_facts: False tasks: - name: Get VPC ID ec2_vpc_net_facts: region: "" filters: "tag:Name": "-vpc" register: vpc_facts - name: Destroy SG MySQL ec2_group: name: "-private" state: absent region: "" - name: Destroy SG Web ec2_group: name: "-web" state: absent region: "" - name: Destroy SG Bastion ec2_group: name: "-bastion" state: absent region: "" - name: Remove subnet route table ec2_vpc_route_table: region: "" state: absent tags: Name: "-public" vpc_id: "" - name: Remove Public Subnet ec2_vpc_subnet: state: absent cidr: "" region: "" vpc_id: "" - name: Remove Private Subnet 1 ec2_vpc_subnet: state: absent cidr: "" region: "" vpc_id: "" - name: Remove Private Subnet 2 ec2_vpc_subnet: state: absent cidr: "" region: "" vpc_id: "" - name: Create internet gateway ec2_vpc_igw: vpc_id: "" state: absent region: "" - name: Remove VPC ec2_vpc_net: name: "-vpc" state: absent cidr_block: "" region: ""
Ansible was very enjoyable and easy to work with. The same goes for using Ansible to manage AWS. Even though there are other tools that can be used such as Terraform (https://www.terraform.io/), Ansible was pretty straightforward and intuitive for creating each of the components, discovering them, and then cleaning them up. It made it nice and simple to search for objects by tag, so it was able to tag the AWS pieces appropriately to allow for easy management of those objects dynamically. In coordination with using Ansible to integrate with AWS to build the network and instances, Ansible can then be used to manage and configure the EC2 operating systems as needed. The Ansible AWS modules make it almost a total package for building and managing an infrastructure in AWS.
Ready to optimize your Managed Services for the future?