DEV Community

jonjpbm
jonjpbm

Posted on

Compound IDs When Importing Existing Infrastructure Resources With Terraform Import Command

Quick tid bit gotcha I found when I was attempting to import an existing New Relic alert condition.

The usage is like so:
Usage: terraform import [options] ADDRESS ID
Reference: https://www.terraform.io/cli/commands/import

Documentation noted above states the following concerning the "ADDRESS ID" that one has to pass when attempting to import an existing resource.

ID is dependent on the resource type being imported. For example, for AWS instances it is the instance ID (i-abcd1234) but for AWS Route53 zones it is the zone ID (Z12ABC4UGMOZ2N). Please reference the provider documentation for details on the ID format. If you're unsure, feel free to just try an ID. If the ID is invalid, you'll just receive an error message.

I knew that for a given alert in New Relic, there is an associated unique ID value which can be had from the New Relic UI. (See below for example)
Image description

So after you have added the terraform code to create the alert as if you were we were wanting to create it and it did not exist yet (see reference noted above), I attempted to simply use that ID value captured from New Relic UI, in the terraform import command:

terraform import newrelic_nrql_alert_condition.nameofexistingalert 123456789

But that failed with this:

Error: compound ID item count cannot be less than expected default ID item count: [123456789]

Here is where I googled the error and could NOT find any good documentation.

So I went to the terraform state file to look at an ID for another alert that was already in the state file and I found this for the ID in the json file:
id": "98765432:123456789"

There are two values that are used in terraform for the ID of an New Relic alert. The policy id and the alert id.

Seeing that and then looking at the documentation (again, noted above), I came up with this.

❯ terraform import newrelic_nrql_alert_condition.Streaming_Service_Disconnect 98765432:123456789:static
newrelic_nrql_alert_condition.Streaming_Service_Disconnect: Importing from ID "98765432:123456789:static"...
newrelic_nrql_alert_condition.Streaming_Service_Disconnect: Import prepared!
  Prepared newrelic_nrql_alert_condition for import
newrelic_nrql_alert_condition.Streaming_Service_Disconnect: Refreshing state... [id=98765432:123456789]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Releasing state lock. This may take a few moments...
Enter fullscreen mode Exit fullscreen mode

Top comments (0)