Creating and using an SSH key pair to use on AWS
Now we have our AWS provider configured in Terraform, let's add a SSH key pair to use on a default account of the virtual machines we intend to launch soon.
Getting ready
To step through this recipe, you will need the following:
- A working Terraform installation
- An AWS provider configured in Terraform
- Generate a pair of SSH keys somewhere you remember, for example, in the
keys
folder at the root of your repo:$ mkdir keys $ ssh-keygen -q -f keys/aws_terraform -C aws_terraform_ssh_key -N ''
- An Internet connection
How to do it…
The resource we want for this is named aws_key_pair
. Let's use it inside a keys.tf
file, and paste the public key content:
resource "aws_key_pair" "admin_key" { key_name = "admin_key" public_key = "ssh-rsa AAAAB3[…]" }
This will simply upload your public key to your AWS account under the name admin_key
:
$ terraform apply aws_key_pair.admin_key: Creating...