Brent Bradburn
Brent Bradburn

Reputation: 54899

What remedies exist when `terraform validate` returns false positives?

I'm importing a lot of existing infrastructure to Terraform. On multiple occasions (and with various resource types), I've seen issues like the following...

After a successful import of a resource (and manually copying the associated state into my configuration), running terraform validate returns an error due to Terraform argument validation rules that are more restrictive than the provider's actual rules.

Example imported configuration:

resource "aws_athena_database" "example" {
    name       = "mydatabase-dev"
    properties = {}
}

Example validation error:

$ terraform validate       
╷
│ Error: invalid value for name (must be lowercase letters, numbers, or underscore ('_'))
│ 
│   with aws_athena_database.example,
│   on main.tf line 121, in resource "aws_athena_database" "example":
│   11:     name       = "mydatabase-dev"
│ 

Error caused by this provider code:

"name": {
    Type:         schema.TypeString,
    Required:     true,
    ForceNew:     true,
    ValidateFunc: validation.StringMatch(regexp.MustCompile("^[_a-z0-9]+$"), "must be lowercase letters, numbers, or underscore ('_')"),
},

Since terraform validate is failing, terraform plan and terraform apply will also fail. Short of renaming the preexisting resources (which could be disruptive), is there an easy way around this?

Upvotes: 0

Views: 784

Answers (1)

spookysaito
spookysaito

Reputation: 11

To fix this issue, you can use the ignore_changes attribute in your Terraform configuration to tell Terraform to ignore any changes to the name attribute of the aws_athena_database resource. This will allow you to keep the existing name of the resource, even if it does not match the validation rules specified in the provider code. After adding the ignore changes attribute, you can run terraform validate again to verify that the error has been resolved. You can then run terraform plan and terraform apply as usual to apply the changes to your infrastructure.

Upvotes: 1

Related Questions