Skip to content

Commit

Permalink
Support storage of key pair into S3 bucket - Needed for running on AW…
Browse files Browse the repository at this point in the history
…X (container)
  • Loading branch information
abikouo committed Jan 18, 2024
1 parent b760acc commit b32f8fc
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
17 changes: 2 additions & 15 deletions playbooks/webapp/tasks/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,8 @@
db_instance_identifier: "{{ rds_identifier }}"
register: rds_result

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

- name: Create key pair to connect to the VM
amazon.aws.ec2_key:
name: "{{ deploy_flask_app_sshkey_pair_name }}"
register: rsa_key

- name: Save private key into file
ansible.builtin.copy:
content: "{{ rsa_key.key.private_key }}"
dest: "{{ sshkey_file }}"
mode: 0400
when: rsa_key is changed
- name: Create key pair to connect to the virtual machine
ansible.builtin.include_tasks: manage_keypair.yaml

- name: Ensure IAM instance role exists
amazon.aws.iam_role:
Expand Down
5 changes: 5 additions & 0 deletions playbooks/webapp/tasks/delete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
region: "{{ region | default(aws_region) }}"

block:
- name: Delete S3 bucket
amazon.aws.s3_bucket:
name: "{{ bucket_name }}"
state: absent
force: true

- name: Get vpc information
amazon.aws.ec2_vpc_net_info:
Expand Down
51 changes: 51 additions & 0 deletions playbooks/webapp/tasks/manage_keypair.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
- name: Ensure S3 bucket exists
amazon.aws.s3_bucket:
name: "{{ bucket_name }}"
versioning: false
state: present

- name: Check if object exists into bucket
amazon.aws.s3_object_info:
bucket_name: "{{ bucket_name }}"
object_name: "id_rsa"
register: existing_obj
ignore_errors: true

- name: Create RSA key pair and Save Private key into S3 bucket
when: existing_obj is failed
block:
- name: Create key pair to connect to the VM
amazon.aws.ec2_key:
name: "{{ deploy_flask_app_sshkey_pair_name }}"
register: keypair

- name: Put object into bucket
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
mode: put
object: "id_rsa"
content: "{{ keypair.key.private_key }}"

- name: Download object as string

Check failure on line 30 in playbooks/webapp/tasks/manage_keypair.yaml

View workflow job for this annotation

GitHub Actions / ansible-lint

var-naming[pattern]

Variables names should match ^\[a-z_]\[a-z0-9_]*$ regex. (downloadObj) (register: downloadObj)
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
mode: getstr
object: "id_rsa"
register: downloadObj

# Download RSA Key
- name: Create temporary file for Private RSA key
ansible.builtin.tempfile:
suffix: id_rsa
register: private_key

- name: Save private key into file
ansible.builtin.copy:
content: "{{ downloadObj.contents }}"
dest: "{{ private_key.path }}"
mode: 0400

- name: Set variable for SSH private key file
ansible.builtin.set_fact:
sshkey_file: "{{ private_key.path }}"
3 changes: 3 additions & 0 deletions playbooks/webapp/vars/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ rds_replica_cluster_instance_name: "{{ resource_prefix }}-replica-instance"

# vars for route53 records
route53_subdomain: "flaskapp"

# A bucket to save RSA key into
bucket_name: "bucket-rsa-{{ resource_prefix }}"
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
vpc_security_group_ids:
- "{{ rds_sg.group_id }}"
wait: false
register: rds_result

# Create key pair to connect to the VM
- name: Create directory to generate keys in
Expand Down Expand Up @@ -212,3 +211,8 @@
subnet: private
route: nat-gateway
state: present

- name: Get RDS instance info
amazon.aws.rds_instance_info:
db_instance_identifier: "{{ rds_identifier }}"
register: rds_result
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
url: "http://{{ deploy_flask_app_lb_result.elb.dns_name }}:{{ deploy_flask_app_listening_port }}"
register: deploy_flask_app_check
until: "deploy_flask_app_check.status == 200"
retries: 5
delay: 10
retries: 200
delay: 3
ignore_errors: true

0 comments on commit b32f8fc

Please sign in to comment.