Skip to content

Commit

Permalink
Deploy app in 2 regions
Browse files Browse the repository at this point in the history
  • Loading branch information
GomathiselviS committed Sep 29, 2023
1 parent 45cc0d3 commit 86841c9
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 126 deletions.
3 changes: 3 additions & 0 deletions playbooks/webapp/files/run_app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
ansible.builtin.yum:
name:
- podman
sslverify: false
validate_certs: false
update_cache: true
state: present

- name: Pull image from private registry
Expand Down
31 changes: 15 additions & 16 deletions playbooks/webapp/tasks/create_aurora_db_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
- name: Create rds global database

Check failure on line 16 in playbooks/webapp/tasks/create_aurora_db_cluster.yaml

View workflow job for this annotation

GitHub Actions / ansible-lint

syntax-check[specific]

couldn't resolve module/action 'amazon.cloud.rds_global_cluster'. This often indicates a misspelling, missing collection, or incorrect module path.
amazon.cloud.rds_global_cluster:
global_cluster_identifier: "{{ test_global_cluster_name }}"
engine: "aurora-postgresql"
engine_version: "15.2"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
region: "{{ test_primary_cluster_region }}"
state: present
register: create_global_result
Expand All @@ -26,11 +26,13 @@
amazon.aws.rds_cluster:
global_cluster_identifier: "{{ test_global_cluster_name }}"
db_cluster_identifier: "{{ test_primary_cluster_name }}"
db_name: "{{ rds_instance_name }}"
region: "{{ test_primary_cluster_region }}"
engine: "aurora-postgresql"
engine_version: "15.2"
username: "{{ deploy_flask_app_rds_master_username }}"
password: "{{ deploy_flask_app_rds_master_password }}"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
#enable_global_write_forwarding: True
master_user_password: "{{ deploy_flask_app_rds_master_password }}"
master_username: "{{ deploy_flask_app_rds_master_username }}"
db_subnet_group_name: "{{ rds_subnet_group_name }}"
vpc_security_group_ids:
- "{{ rds_primary_sg.security_groups[0].group_id }}"
Expand All @@ -40,11 +42,9 @@
amazon.aws.rds_instance:
db_cluster_identifier: "{{ test_primary_cluster_name }}"
db_instance_identifier: "{{ test_primary_cluster_name }}-instance"
db_name: "{{ rds_instance_name }}"
region: "{{ test_primary_cluster_region }}"
engine: "aurora-postgresql"
db_instance_class: "{{ test_instance_class }}"
monitoring_interval: 0
engine: "{{ rds_engine }}"
db_instance_class: "db.r6g.large"
skip_final_snapshot: true

- name: Get primary instance info
Expand Down Expand Up @@ -84,9 +84,10 @@
amazon.aws.rds_cluster:
global_cluster_identifier: "{{ test_global_cluster_name }}"
db_cluster_identifier: "{{ test_replica_cluster_name }}"
engine: "aurora-postgresql"
engine_version: "{{ global_cluster_info.GlobalClusters[0].EngineVersion }}" # replica cluster engine version needs to be exact same as global db engine version
db_subnet_group_name: "{{ rds_subnet_group_name }}"
engine: "{{ rds_engine }}"
engine_version: "{{ global_cluster_info.GlobalClusters[0].EngineVersion }}" # replica cluster engine version needs to be exact same as global db engine version
#enable_global_write_forwarding: True
vpc_security_group_ids:
- "{{ rds_replica_sg.security_groups[0].group_id }}"
region: "{{ test_replica_cluster_region }}"
Expand All @@ -96,11 +97,9 @@
amazon.aws.rds_instance:
db_cluster_identifier: "{{ test_replica_cluster_name }}"
db_instance_identifier: "{{ test_replica_cluster_name }}-instance"
db_name: "{{ rds_instance_name }}"
region: "{{ test_replica_cluster_region }}"
engine: "aurora-postgresql"
db_instance_class: "{{ test_instance_class }}"
monitoring_interval: 0
engine: "{{ rds_engine }}"
db_instance_class: "db.r6g.large"
skip_final_snapshot: true

- name: Get replica instance info
Expand Down
2 changes: 1 addition & 1 deletion playbooks/webapp/tasks/create_aurora_setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@

- name: Set 'sshkey_file' variable
ansible.builtin.set_fact:
sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }}
sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region }}

- name: Create key pair to connect to the VM
amazon.aws.ec2_key:
Expand Down
16 changes: 13 additions & 3 deletions playbooks/webapp/tasks/delete.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
---
- name: Delete resources playbook
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: Set 'region' variable
ansible.builtin.set_fact:
region: "{{ region | default(aws_region) }}"

- name: Get vpc information
amazon.aws.ec2_vpc_net_info:
Expand All @@ -25,6 +28,7 @@
instance-type: "{{ bastion_host_type }}"
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
vpc-id: "{{ vpc_id }}"
instance-state-name: running
register: bastion

- name: Delete EC2 instances with dependant Resources
Expand Down Expand Up @@ -81,6 +85,7 @@
amazon.aws.rds_subnet_group:
name: "{{ rds_subnet_group_name }}"
state: absent
ignore_errors: true

- name: List Security group from VPC
amazon.aws.ec2_security_group_info:
Expand All @@ -94,6 +99,7 @@
state: absent
group_id: "{{ item }}"
with_items: "{{ secgroups.security_groups | map(attribute='group_id') | list }}"
ignore_errors: true

- name: List routes table from VPC
amazon.aws.ec2_vpc_route_table_info:
Expand All @@ -109,6 +115,7 @@
lookup: id
state: absent
with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"
ignore_errors: true

- name: Get NAT gateway
amazon.aws.ec2_vpc_nat_gateway_info:
Expand All @@ -122,18 +129,21 @@
state: absent
wait: true
with_items: "{{ nat_gw.result | map(attribute='nat_gateway_id') | list }}"
ignore_errors: true

- name: Delete internet gateway
amazon.aws.ec2_vpc_igw:
vpc_id: "{{ vpc_id }}"
state: absent
ignore_errors: true

- name: Delete subnets
amazon.aws.ec2_vpc_subnet:
cidr: "{{ item }}"
state: absent
vpc_id: "{{ vpc_id }}"
with_items: "{{ subnet_cidr }}"
ignore_errors: true

# As ec2_vpc_route_table can't delete route table, the vpc still has dependencies and cannot be deleted.
# You need to do it delete it manually using either the console or the cli.
Expand Down
21 changes: 12 additions & 9 deletions playbooks/webapp/tasks/delete_aurora_db_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
amazon.aws.rds_instance:
db_cluster_identifier: "{{ test_replica_cluster_name }}"
db_instance_identifier: "{{ test_replica_cluster_name }}-instance"
engine: "aurora-postgresql"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
db_instance_class: "{{ test_instance_class }}"
skip_final_snapshot: true
region: "{{ test_replica_cluster_region }}"
wait: True
state: absent

- name: Delete replica cluster
amazon.aws.rds_cluster:
db_cluster_identifier: "{{ test_replica_cluster_name }}"
global_cluster_identifier: "{{ test_global_cluster_name }}"
engine: "aurora-postgresql"
engine_version: "15.2"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
skip_final_snapshot: true
remove_from_global_db: true
region: "{{ test_replica_cluster_region }}"
Expand All @@ -31,7 +33,8 @@
amazon.aws.rds_instance:
db_cluster_identifier: "{{ test_primary_cluster_name }}"
db_instance_identifier: "{{ test_primary_cluster_name }}-instance"
engine: "aurora-postgresql"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
db_instance_class: "{{ test_instance_class }}"
skip_final_snapshot: true
region: "{{ test_primary_cluster_region }}"
Expand All @@ -41,19 +44,19 @@
amazon.aws.rds_cluster:
db_cluster_identifier: "{{ test_primary_cluster_name }}"
global_cluster_identifier: "{{ test_global_cluster_name }}"
engine: "aurora-postgresql"
engine_version: "15.2"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
username: "{{ deploy_flask_app_rds_master_username }}"
password: "{{ deploy_flask_app_rds_master_password }}"
skip_final_snapshot: true
region: "{{ test_replica_cluster_region }}"
region: "{{ test_primary_cluster_region }}"
state: absent

- name: Delete global db
amazon.cloud.rds_global_cluster:
global_cluster_identifier: "{{ test_global_cluster_name }}"
engine: "aurora-postgresql"
engine_version: "15.2"
engine: "{{ rds_engine }}"
engine_version: "{{ rds_engine_version }}"
region: "{{ test_primary_cluster_region }}"
state: absent

18 changes: 11 additions & 7 deletions playbooks/webapp/vars/main.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# Variables for create.yaml
aws_region: us-east-1
# aws_region: us-west-2
dest_region: us-east-2
resource_prefix: "ansible-test"
vpc_name: "{{ resource_prefix }}-vpc"
Expand All @@ -13,7 +13,10 @@ resource_tags:
prefix: "{{ resource_prefix }}"
operation: create

image_filter: Fedora-Cloud-Base-35-*
# image_filter: Fedora-Cloud-Base-37-*
# image_filter: RHEL-8.7.0_HVM-20221101-x86_64-0-Hourly2-GP2
# image_filter: RHEL_HA-9.0.0_HVM-20230822-x86_64-17-Hourly2-GP2 - podman 503 error
image_filter: RHEL_HA-9.2.0_HVM-20230905-x86_64-38-Hourly2-GP2
public_secgroup_name: "{{ resource_prefix }}-sg"
rds_subnet_group_name: "{{ resource_prefix }}-rds-sg"
rds_secgroup_name: "{{ resource_prefix }}-rds-sec"
Expand All @@ -30,17 +33,17 @@ rds_listening_port: 5432
# Variables for the deploy_flask_app role
deploy_flask_app_sshkey_pair_name: "{{ resource_prefix }}-key"
deploy_flask_app_bastion_host_name: "{{ resource_prefix }}-bastion"
deploy_flask_app_bastion_host_username: fedora
deploy_flask_app_bastion_host_username: ec2-user
deploy_flask_app_bastion_host_required_packages:
- python3
- python-virtualenv
- sshpass
- git
- gcc
- podman
- httpd-tools
- ansible
- ansible-core
deploy_flask_app_workers_instance_type: t3.micro
deploy_flask_app_workers_user_name: fedora
deploy_flask_app_workers_user_name: ec2-user
deploy_flask_app_number_of_workers: 2
deploy_flask_app_listening_port: 5000
deploy_flask_app_git_repository: https://github.com/abikouo/webapp_pyflask_demo.git
Expand All @@ -60,6 +63,7 @@ deploy_flask_app_rds_master_username: ansible
test_instance_class: db.r5.large
# Global cluster parameters ================================
test_global_cluster_name: "{{ resource_prefix }}-global-cluster"
aurora_listening_port: 3306

# Primary cluster parameters ================================
test_primary_cluster_name: "{{ resource_prefix }}-primary-cluster"
Expand All @@ -68,5 +72,5 @@ test_primary_cluster_instance_name: "{{ resource_prefix }}-primary-instance"

# Replica cluster parameters ================================
test_replica_cluster_name: "{{ resource_prefix }}-replica-cluster"
test_replica_cluster_region: eu-north-1
test_replica_cluster_region: us-east-1
test_replica_cluster_instance_name: "{{ resource_prefix }}-replica-instance"
Loading

0 comments on commit 86841c9

Please sign in to comment.