ChrisAdkin
ChrisAdkin

Reputation: 1326

provisioning an EC2 instance with terraform InvalidKeyPair.NotFound

I've created a key pair for EC2 called terraform, downloaded the pem file to the same directory where my terraform files live, I issue a terraform apply and I get:

aws_instance.windows: Creating...

Error: Error launching source instance: InvalidKeyPair.NotFound: The key pair 'terraform' does not exist
        status code: 400, request id: 1ac563d4-244a-4371-bde7-ee9bcf048830

I'm specifying the name of the key-value pair via an envrionment variable. This is the start of the block I'm using to create the Windows virtual machine:

resource "aws_instance" "windows" {
  ami                         = data.aws_ami.Windows_2019.image_id
  instance_type               = var.windows_instance_types
  key_name                    = var.key_name
  vpc_security_group_ids      = [aws_security_group.allow_rdp_winrm.id]
  associate_public_ip_address = true
  subnet_id                   = aws_subnet.subnet1.id
  get_password_data           = "true"

  user_data = file("scripts/user_data.txt")

There is obviously something I'm doing wrong, do I need to tell terraform which aws region then key pair resides in ?

Upvotes: 4

Views: 8356

Answers (3)

Surprise Associate
Surprise Associate

Reputation: 31

I'll only provide an answer that pertains to your error message directly, as this question came up first on a Bing search.

The Data Source documentation doesn't make mention of Keypair. It might be presumed from the Instances Availability Zone.

data "aws_key_pair" "example" {
  key_name = "terraform"
  filter {
    name   = "tag:Component"
    values = ["web"]
  }
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  key_name      = data.aws_key_pair.example.key_name
  instance_type = "t3.micro"

  tags = {
    Name = "HelloWorld"
  }
}

Instance resource declaration: https://registry.terraform.io/providers/hashicorp%20%20/aws/latest/docs/resources/instance

AWS Keypair Data Source: https://registry.terraform.io/providers/hashicorp%20%20/aws/latest/docs/data-sources/key_pair

Upvotes: 0

Sathyajith Bhat
Sathyajith Bhat

Reputation: 21851

The key pairs are regional, so if you created them in one region, they aren't available in the other.

Terraform will always try to find and use the key in the region that you tell it to run in and if the key is not present, AWS will complain about this error.

Terraform also doesn't like it when things are created out of band and you might run into complications. It's also much cleaner to create the keypair using terraform and you can reference it as Atul has posted in his answer.

You could also import the key into Terraform or use Terraform's data sources to search and find the key as alternatives but these are a bit advanced, especially if you're getting started with Terraform.

Upvotes: 5

Atul Sharma
Atul Sharma

Reputation: 10740

You need to create a key pair first before consuming it.

resource "aws_key_pair" "my_key_pair" {
  key_name   = var.key_name
  public_key = file("${abspath(path.cwd)}/my-key.pub")
}

Now use the key as

resource "aws_instance" "windows" {
  ami                         = data.aws_ami.Windows_2019.image_id
  instance_type               = var.windows_instance_types
  key_name                    = aws_key_pair.my_key_pair.key_name

Upvotes: 3

Related Questions