Snowcrash
Snowcrash

Reputation: 86097

Terraform > Resource > name - invalid or unknown key

According to this https://www.terraform.io/docs/configuration/resources.html you can have a resource with a name. E.g.

resource "aws_db_instance" "timeout_example" {
  allocated_storage = 10
  engine            = "mysql"
  engine_version    = "5.6.17"
  instance_class    = "db.t1.micro"
  name              = "mydb"

  # ...

  timeouts {
    create = "60m"
    delete = "2h"
  }
}

but my sample tf file:

provider "aws" {
  access_key = "<access key>"
  secret_key = "<secret key>"
  region     = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0d729a60"
  instance_type = "t2.micro"
  subnet_id = "<subnet-id>"
  name = "web"
}

gives me * aws_instance.web: : invalid or unknown key: name.

Any idea why?

Upvotes: 3

Views: 12620

Answers (2)

TJ Biddle
TJ Biddle

Reputation: 6484

This is not working because name is not a valid argument of the aws_db_instance resource type. You can find a list of all the valid arguments for this resource here.

In the documentation that you linked, this paragraph is found.

Description

The resource block creates a resource of the given TYPE (first parameter) and NAME (second parameter). The combination of the type and name must be unique.

This is referencing the second parameter of the entire resource; so when you have:

resource "aws_db_instance" "timeout_example" {
     ...
}

The "name" parameter is "timeout_example".

Upvotes: 1

Rob Lockwood-Blake
Rob Lockwood-Blake

Reputation: 5056

Just to elaborate on the comments above. You're creating an instance of the aws_db_instance resource. Checking the documentation here for that resource type, there is no mention of the name attribute, so the error seems valid and I guess it's just a bug in the Terraform documentation (not sure where you can report it).

To give your db a "name", you can use the Name AWS tag in your resource definition:

resource "aws_db_instance" "my-database" {
  ...
  tags {
    "Name" = "My-Database-Name"
  }
}

Upvotes: 1

Related Questions