PEM File
Introduction
I am using IDE: PyCharm 2023.3.5 (Professional Edition). This takes care of the Python installation and virtual environment. In this article, we will create a Ansible playbook to create a PEM file on your local machine.
Creating the PEM File
- Create a folder for the experiments. Go into that folder
- Create hosts file with:
- Create a file called create_key_pair.yml with the following content:
- Install the Ansible collection for AWS.
- Run the playbook:
Explanation of the Playbook
The playbook runs on the local machine. It creates a new PEM file using ec2_key Ansible module. The result of the operation is stored in the variable ec2_key_result. The private key is stored in the file rails-server.pem.
The copy module saves the private key in the local machine as rails-server.pem. The mode is set to 0600. This means that the file is only readable and writable by the owner. This is a security best practice for SSH private keys. This task is only executed if the key pair was successfully created. This is the when: ec2_key_result.changed
condition.
The hosts: local
means it will execute on the local machine where ansible playbook is run. The connection: local
means the task will run on the local machine. The gather_facts: no
means it will not collect any facts. This will speed up execution. The tasks:
section contains the tasks that will be executed.
Verify the Result
Verify the PEM file on your local machine:
Associate the PEM file to the EC2 instance. The boto3 Python SDK code example creates a key pair and associates the key pair to the EC2 instance it creates.
If you don’t see the PEM file in the AWS secrets manager, make sure you are looking at the right AWS account:
If you have multiple AWS accounts, you can check which account has the secrets.
Troubleshooting Issues
SSL Problem
To fix this issue, run the following commands:
Next Action Items
- Store the generated PEM file in AWS Secrets Manager. Modify the hive project code.