Skip to content

Commit

Permalink
Fixing issue with webapp_ha_aurora playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Feb 6, 2024
1 parent f89c274 commit dbb3c18
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 139 deletions.
2 changes: 2 additions & 0 deletions playbooks/webapp/tasks/add_route53_records.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
failover: "PRIMARY"
health_check: "{{ healthchk_primary_result.health_check.id }}"
alias_hosted_zone_id: "{{ primary_lb.elb.hosted_zone_id }}"
overwrite: true
register: alias_record_primary_result

- name: Add an alias record that points to an aws ELB in the replica region
Expand All @@ -57,6 +58,7 @@
failover: "SECONDARY"
health_check: "{{ healthchk_replica_result.health_check.id }}"
alias_hosted_zone_id: "{{ replica_lb.elb.hosted_zone_id }}"
overwrite: true
register: alias_record_replica_result

- name: Pause for 30 secs for the alias records to be active
Expand Down
1 change: 1 addition & 0 deletions playbooks/webapp/tasks/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
- name: Set variable for SSH private key file path
ansible.builtin.set_fact:
deploy_flask_app_bastion_ssh_private_key_path: "~/.{{ resource_prefix }}_id_rsa"
when: deploy_flask_app_bastion_ssh_private_key_path is undefined

- name: Create key pair to connect to the workers
amazon.aws.ec2_key:
Expand Down
12 changes: 0 additions & 12 deletions playbooks/webapp/tasks/create_aurora_db_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@
create_rds_global_cluster_replica_cluster_vpc_security_group_ids:
- "{{ rds_replica_sg.security_groups[0].group_id }}"

- name: Get primary instance info
amazon.aws.rds_instance_info:
db_instance_identifier: "{{ rds_primary_cluster_instance_name }}"
region: "{{ rds_primary_cluster_region }}"
register: primary_instance_info_result

- name: Get primary cluster info
amazon.aws.rds_cluster_info:
db_cluster_identifier: "{{ rds_primary_cluster_name }}"
Expand All @@ -62,12 +56,6 @@
region: "{{ rds_replica_cluster_region }}"
register: replica_cluster_info_result

- name: Get replica instance info
amazon.aws.rds_instance_info:
db_instance_identifier: "{{ rds_replica_cluster_instance_name }}"
region: "{{ rds_replica_cluster_region }}"
register: replica_instance_info_result

- name: Get global db info
amazon.aws.rds_global_cluster_info:
global_cluster_identifier: "{{ rds_global_cluster_name }}"
Expand Down
43 changes: 43 additions & 0 deletions playbooks/webapp/tasks/deploy_app_into_region.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
- name: Deploy application into regions
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key | default(omit) }}"
aws_secret_key: "{{ aws_secret_key | default(omit) }}"
security_token: "{{ security_token | default(omit) }}"
region: "{{ region }}"
block:
- name: Get VPC info
amazon.aws.ec2_vpc_net_info:
filters:
"tag:Name": "{{ vpc_name }}"
register: vpc_result

- name: Get Private Subnet for Workers
amazon.aws.ec2_vpc_subnet_info:
filters:
vpc-id: "{{ vpc_result.vpcs[0].id }}"
cidr: "{{ subnet_cidr[1] }}"
register: _subnets

- name: Get VM info
amazon.aws.ec2_instance_info:
filters:
"tag:Name": "{{ deploy_flask_app_bastion_host_name }}"
instance-state-name: ["running"]
register: vm_result

- name: Get RDS instance info
amazon.aws.rds_instance_info:
db_instance_identifier: "{{ rds_cluster_name }}"
register: rds_result

- name: Deploy app into region
ansible.builtin.include_role:
name: cloud.aws_ops.deploy_flask_app
vars:
deploy_flask_app_private_subnet_id: "{{ _subnets.subnets[0].id }}"
deploy_flask_app_vpc_id: "{{ vpc_result.vpcs[0].id }}"
deploy_flask_app_bastion_instance_id: "{{ vm_result.instances.0.instance_id }}"
deploy_flask_app_rds_host: "{{ rds_result.instances.0.endpoint.address }}"
deploy_flask_app_rds_dbname: "{{ rds_result.instances.0.db_name }}"
2 changes: 1 addition & 1 deletion playbooks/webapp/vars/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ rds_replica_cluster_region: us-east-2
rds_replica_cluster_instance_name: "{{ resource_prefix }}-replica-instance"

# vars for route53 records
route53_subdomain: "flaskapp"
route53_subdomain: "ansiblecloud.xyz"

# A bucket to save RSA key into
bucket_name: "bucket-rsa-{{ resource_prefix }}"
211 changes: 98 additions & 113 deletions playbooks/webapp/webapp_ha_aurora.yaml
Original file line number Diff line number Diff line change
@@ -1,111 +1,86 @@
---
- name: Webapp HA
- name: Configure inventory for High availability Aurora cluster
hosts: localhost
gather_facts: false

vars_files:
- vars/main.yaml

module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key | default(omit) }}"
aws_secret_key: "{{ aws_secret_key | default(omit) }}"
security_token: "{{ security_token | default(omit) }}"
region: "{{ region | default(aws_region) }}"
tasks:
- name: Add different hosts
ansible.builtin.add_host:
groups:
- aurora
name: "aurora_{{ item.region }}"
ansible_connection: local
region: "{{ item.region }}"
ansible_python_interpreter: "{{ ansible_python_interpreter }}"
deploy_flask_app_bastion_ssh_private_key_path: "~/.{{ resource_prefix }}{{ item.region }}_id_rsa"
rds_cluster_name: "{{ item.rds_cluster_name }}"
with_items:
- region: "{{ rds_primary_cluster_region }}"
rds_cluster_name: "{{ rds_primary_cluster_instance_name }}"
- region: "{{ rds_replica_cluster_region }}"
rds_cluster_name: "{{ rds_replica_cluster_instance_name }}"

- name: Webapp HA
hosts: aurora
gather_facts: false
strategy: free

vars_files:
- vars/main.yaml

tasks:
- name: Create resources and Deploy App
- name: Create resources in region
ansible.builtin.include_tasks: tasks/create.yaml
when: operation == "create"
block:
- name: Create resources in primary region
ansible.builtin.include_tasks: tasks/create.yaml
vars:
region: "{{ creation_region }}"
rds_instance_class: db.r5.large
rds_engine: aurora-postgresql
loop:
- "{{ rds_primary_cluster_region }}"
- "{{ rds_replica_cluster_region }}"
loop_control:
loop_var: creation_region

- name: Create Aurora db cluster
ansible.builtin.import_tasks: tasks/create_aurora_db_cluster.yaml
vars:
rds_instance_class: db.r5.large
rds_engine: aurora-postgresql
vars:
rds_instance_class: db.r5.large
rds_engine: aurora-postgresql

# ================= Deploy App in the primary region =================
- name: Create Aurora db cluster
hosts: localhost

vars_files:
- vars/main.yaml

- name: Get VPC info from primary region
amazon.aws.ec2_vpc_net_info:
filters:
"tag:Name": "{{ vpc_name }}"
region: "{{ rds_primary_cluster_region }}"
register: primary_vpc
tasks:
- name: Create Aurora db cluster
ansible.builtin.import_tasks: tasks/create_aurora_db_cluster.yaml
when: operation == "create"
vars:
rds_instance_class: db.r5.large
rds_engine: aurora-postgresql

- name: Get primary private subnet for workers
amazon.aws.ec2_vpc_subnet_info:
filters:
vpc-id: "{{ primary_vpc.vpcs[0].id }}"
cidr: "{{ subnet_cidr[1] }}"
region: "{{ rds_primary_cluster_region }}"
register: primary_private_subnet
- name: Deploy application into regions
hosts: localhost
gather_facts: false

- name: Get VM info in the primary region
amazon.aws.ec2_instance_info:
filters:
"tag:Name": "{{ deploy_flask_app_bastion_host_name }}"
instance-state-name: [ "running"]
region: "{{ rds_primary_cluster_region }}"
register: primary_vm_result
vars_files:
- vars/main.yaml

- name: Deploy app in primary region
ansible.builtin.include_role:
name: cloud.aws_ops.deploy_flask_app
tasks:
- name: Deplou application and add Route53 records
when: operation == "create"
block:
- name: Deploy application into primary region
ansible.builtin.import_tasks: tasks/deploy_app_into_region.yaml
vars:
deploy_flask_app_private_subnet_id: "{{ primary_private_subnet.subnets[0].id }}"
deploy_flask_app_vpc_id: "{{ primary_vpc.vpcs[0].id }}"
deploy_flask_app_vm_info: "{{ primary_vm_result }}"
deploy_flask_app_rds_info: "{{ primary_instance_info_result }}"
region: "{{ rds_primary_cluster_region }}"
rds_cluster_name: "{{ rds_primary_cluster_instance_name }}"
deploy_flask_app_bastion_ssh_private_key_path: "~/.{{ resource_prefix }}{{ rds_primary_cluster_region }}_id_rsa"

- name: Get load balancer name from the primary region
ansible.builtin.set_fact:
primary_lb: "{{ deploy_flask_app_lb_result }}"

# ================= Deploy App in the replica region =================

- name: Get VPC info from replica region
amazon.aws.ec2_vpc_net_info:
filters:
"tag:Name": "{{ vpc_name }}"
region: "{{ rds_replica_cluster_region }}"
register: replica_vpc

- name: Get VM info in the replica region
amazon.aws.ec2_instance_info:
filters:
"tag:Name": "{{ deploy_flask_app_bastion_host_name }}"
instance-state-name: [ "running"]
region: "{{ rds_replica_cluster_region }}"
register: replica_vm_result

- name: Get replica private subnet for workers
amazon.aws.ec2_vpc_subnet_info:
filters:
vpc-id: "{{ replica_vpc.vpcs[0].id }}"
cidr: "{{ subnet_cidr[1] }}"
region: "{{ rds_replica_cluster_region }}"
register: replica_private_subnet

- name: Deploy app in replica region
ansible.builtin.include_role:
name: cloud.aws_ops.deploy_flask_app
- name: Deploy application into replica region
ansible.builtin.import_tasks: tasks/deploy_app_into_region.yaml
vars:
deploy_flask_app_private_subnet_id: "{{ replica_private_subnet.subnets[0].id }}"
deploy_flask_app_vpc_id: "{{ replica_vpc.vpcs[0].id }}"
deploy_flask_app_vm_info: "{{ replica_vm_result }}"
deploy_flask_app_rds_info: "{{ replica_instance_info_result }}"
region: "{{ rds_replica_cluster_region }}"
rds_cluster_name: "{{ rds_replica_cluster_instance_name }}"
deploy_flask_app_bastion_ssh_private_key_path: "~/.{{ resource_prefix }}{{ rds_replica_cluster_region }}_id_rsa"

- name: Get load balancer name from the replica region
ansible.builtin.set_fact:
Expand All @@ -114,34 +89,44 @@
- name: Add Route53 configurations
ansible.builtin.include_tasks: tasks/add_route53_records.yaml

# ================================================================================
# ================================================================================

- name: Delete Route53 records and Aurora cluster
hosts: localhost
gather_facts: false

vars_files:
- vars/main.yaml

- name: Delete resources
tasks:
- name: Delete Route 53 records and health checks
ansible.builtin.import_tasks: tasks/delete_route53_records.yaml
when: operation == "delete"
block:

- name: Delete Route 53 records and health checks
ansible.builtin.import_tasks: tasks/delete_route53_records.yaml
- name: Delete Aurora DB
ansible.builtin.include_role:
name: cloud.aws_ops.create_rds_global_cluster
vars:
create_rds_global_cluster_operation: delete
create_rds_global_cluster_global_cluster_name: "{{ rds_global_cluster_name }}"
create_rds_global_cluster_primary_cluster_name: "{{ rds_primary_cluster_name }}"
create_rds_global_cluster_primary_cluster_region: "{{ rds_primary_cluster_region }}"
create_rds_global_cluster_primary_cluster_instance_name: "{{ rds_primary_cluster_instance_name }}"
create_rds_global_cluster_replica_cluster_name: "{{ rds_replica_cluster_name }}"
create_rds_global_cluster_replica_cluster_region: "{{ rds_replica_cluster_region }}"
create_rds_global_cluster_replica_cluster_instance_name: "{{ rds_replica_cluster_instance_name }}"

- name: Delete EC2 resources
hosts: aurora
gather_facts: false
strategy: free

- name: Delete Aurora DB
ansible.builtin.include_role:
name: cloud.aws_ops.create_rds_global_cluster
vars:
create_rds_global_cluster_operation: delete
create_rds_global_cluster_global_cluster_name: "{{ rds_global_cluster_name }}"
create_rds_global_cluster_primary_cluster_name: "{{ rds_primary_cluster_name }}"
create_rds_global_cluster_primary_cluster_region: "{{ rds_primary_cluster_region }}"
create_rds_global_cluster_primary_cluster_instance_name: "{{ rds_primary_cluster_instance_name }}"
create_rds_global_cluster_replica_cluster_name: "{{ rds_replica_cluster_name }}"
create_rds_global_cluster_replica_cluster_region: "{{ rds_replica_cluster_region }}"
create_rds_global_cluster_replica_cluster_instance_name: "{{ rds_replica_cluster_instance_name }}"

- name: Delete all resources
ansible.builtin.include_tasks: tasks/delete.yaml
vars:
region: "{{ deletion_region }}"
loop:
- "{{ rds_primary_cluster_region }}"
- "{{ rds_replica_cluster_region }}"
loop_control:
loop_var: deletion_region
vars_files:
- vars/main.yaml

tasks:
- name: Delete all resources
ansible.builtin.include_tasks: tasks/delete.yaml
when: operation == "delete"
vars:
rds_identifier: "{{ rds_cluster_name }}"
6 changes: 3 additions & 3 deletions roles/create_rds_global_cluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Role Variables
- **create_rds_global_cluster_global_cluster_name** - Name of the Amazon Aurora global cluster. **required**
- **create_rds_global_cluster_engine** - Engine of the Amazon Aurora global and rds clusters. Default is aurora-postgresql.
- **create_rds_global_cluster_engine_version** - Engine version of the Amazon Aurora global and rds clusters.
- **create_rds_global_cluster_instance_class** - Instance class of instance in primary and replica cluster. **required**
- **create_rds_global_cluster_master_username** - Username of the rds clusters master user. **required**
- **create_rds_global_cluster_master_user_password** - Password of the rds clusters master user. **required**
- **create_rds_global_cluster_instance_class** - Instance class of instance in primary and replica cluster. **Required** when __create_rds_global_cluster_operation__ is set to __create__.
- **create_rds_global_cluster_master_username** - Username of the rds clusters master user. **Required** when __create_rds_global_cluster_operation__ is set to __create__.
- **create_rds_global_cluster_master_user_password** - Password of the rds clusters master user. **Required** when __create_rds_global_cluster_operation__ is set to __create__.

**Primary cluster variables**
- **create_rds_global_cluster_primary_cluster_name** - Name of the primary cluster. Default is $create_rds_global_cluster_global_cluster_name.
Expand Down
6 changes: 3 additions & 3 deletions roles/create_rds_global_cluster/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ argument_specs:
create_rds_global_cluster_instance_class:
description:
- Instance class of instance in primary and replica cluster.
required: true
- Required when I(create_rds_global_cluster_operation=create).
create_rds_global_cluster_master_username:
description:
- Username of the rds clusters master user.
required: true
- Required when I(create_rds_global_cluster_operation=create).
create_rds_global_cluster_master_user_password:
description:
- Password of the rds clusters master user.
required: true
- Required when I(create_rds_global_cluster_operation=create).
create_rds_global_cluster_primary_cluster_name:
description:
- Name of the primary cluster.
Expand Down
Loading

0 comments on commit dbb3c18

Please sign in to comment.