In this blog, we will be Provisioning EC2 key pairs with terraform.
There are two ways to Provisioning EC2 key pairs with terraform
- Use Existing Key
- Create New Key
Use Existing Key
You can create one key using AWS console and use the existing one which is already present as shown below
Now in your terraform code, you have to just use this key name in your configuration like this
resource “aws_instance” “bastion” {
count = “1”
connection { user = “ubuntu” // private_key = “${base64decode(var.ssh_private_key)}” }
instance_type = “${var.instance_type}”
ami = “${var.aws_amis}”
key_name = “${local.ssh_key_name}”
Create New Key
You can create a new key, you have to pass the public key as shown, you can use the path or hardcoded the public key .
resource "aws_key_pair" "deploy" { key_name = "Terraform-test" public_key = "ssh-rsa "
}