The key_pairs.tf File¶
| Filename | Location | Group | Project/Repository |
|---|---|---|---|
key_pairs.tf | ./key_pairs.tf | infrastructure | terraform |
Why?¶
We'll publish a public key to AWS, an EC2 Key Pair, and use this to SSH into instances and manage them via Ansible. Without defining a Key Pair in AWs we'll be able to manage or access our instances.
Note
If you use the wrong SSH public key then you'll be locked out of your EC2 Instances. You'll have to recreate the instances if you get the key wrong and have to change it to the correct one.
Breakdown¶
1 2 3 4 | |
The contents of deployment_key.pub are what you need to provide for the above code to work. This means you have to update the public_key attribute of the aws_key_pair to be the contents of your public key, not mine.
Note
Remember when you created this key?
You can get these contents by using cat:
1 | |
Warning
Remember that the file deployment_key is the private key and should not be shared with anyone!
Grab the contents of the file and copy/paste them into the public_key attribute.
The Solution¶
1 2 3 4 | |
Terraform Documentation¶
| Type | Documentation |
|---|---|
aws_key_pair | Terraform AWS Provider |
Committing the Code¶
- Set your working directory to the
infrastructure/terraformrepository - Save the file as
key_pairs.tfand usegit add key_pairs.tfto add it to the Git staging area - Use
git commit -am 'defining the public key for ssh access to our instances'to commit the file to our repository - Push the code to GitLab.com:
git push