Terraform: Import of aws_cloudwatch_metric_alarm ignores provider alias

Created on 25 Apr 2017  ยท  13Comments  ยท  Source: hashicorp/terraform

Terraform Version

v0.9.3

Affected Resource(s)

  • aws_cloudwatch_metric_alarm

Terraform Configuration Files

provider "aws" {
  region = "us-east-1"
  alias = "billing"
}

resource "aws_cloudwatch_metric_alarm" "billing" {
  provider                  = "aws.billing"
  alarm_name                = "BillingAlarm"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = "1"
  metric_name               = "EstimatedCharges"
  namespace                 = "AWS/Billing"
  period                    = "${6*60*60}"
  statistic                 = "Maximum"
  threshold                 = "9"
  treat_missing_data        = "missing"

  dimensions {
    Currency = "USD"
  }
}

Expected Behavior

The provider alias should be respected when importing the resource.

Actual Behavior

The provider alias is ignored. The appearance of the following prompt suggests that Terraform is looking for the non-existent aws provider:

$ terraform import aws_cloudwatch_metric_alarm.billing BillingAlarm
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value:

Note that removing the alias and provider lines allows the alarm to be imported (as you would expect), while adding the aws provider in a different region causes it to think the resource is non-existent (consistent with it picking up aws instead of the alias).

Important Factoids

I need to use an alias because billing alarms have to be in us-east-1, and I don't otherwise use that region.

bug core v0.9

Most helpful comment

Hi @bchallenor, thanks for the issue!

To clarify, does using the -provider argument to the terraform import command work for you?

ex:

terraform import -provider=billing aws_cloudwatch_metric_alarm.billing BillingAlarm

All 13 comments

Hi @bchallenor, thanks for the issue!

To clarify, does using the -provider argument to the terraform import command work for you?

ex:

terraform import -provider=billing aws_cloudwatch_metric_alarm.billing BillingAlarm

Hmm, I wasn't aware of that option (though note it expects -provider=aws.billing not -provider=billing). Its behaviour is very strange. It seems to do two things in parallel: it successfully imports the resource (correctly setting the provider in the state file), but it also launches the region prompt mentioned above. The output interleaves these two actions. I have to type something in the prompt to cause Terraform to exit, and it has to be a valid region name, but what region I pick is irrelevant because the import is already done by then.

$ terraform import -provider=aws.billing aws_cloudwatch_metric_alarm.billing BillingAlarm                 provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.                                                                                                              
  Default: us-east-1                                                                                                                            Enter a value: aws_cloudwatch_metric_alarm.billing: Importing from ID "BillingAlarm"...
aws_cloudwatch_metric_alarm.billing: Import complete!                                                                                           Imported aws_cloudwatch_metric_alarm (ID: BillingAlarm)
aws_cloudwatch_metric_alarm.billing: Refreshing state... (ID: BillingAlarm)
eu-west-2


Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate                                                                               configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

By the way, I can get the prompt to disappear if I define an aws provider in the file that is otherwise unused:

provider "aws" {
  region = "eu-west-1"
}

It's possible that these are two separate issues:

  1. The prompt appears if and only if there is no aws provider in the file.
  2. The import itself ignores aws.billing as specified in the resource in the file, but does use it when forced by the command line.

Hey @bchallenor, I'm unable to reproduce the issue you're describing above with aws_instances. Lemme try it with a cloudwatch metric alarm and see if that's the issue.
image

But just to clarify, both of the aws providers that you have in your configuration are set, and aliased correctly?

Also was unable to reproduce the issue with the aws_cloudwatch_metric_alarm resource. Can you verify the full list of providers that you're using in the same directory as the import command that you're running? Thanks!

I can confirm the behaviour you see above with your input files. The problem is if you have defined the resource already.

Resource in file, aws.billing in file, aws not in file, aws.billing specified on command line

Resource imports but useless prompt is shown.

$ cat main.tf 
provider "aws" {
  region = "us-east-1"
  alias = "billing"
}

resource "aws_cloudwatch_metric_alarm" "billing" {
  provider                  = "aws.billing"
  alarm_name                = "BillingAlarm"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = "1"
  metric_name               = "EstimatedCharges"
  namespace                 = "AWS/Billing"
  period                    = "${6*60*60}"
  statistic                 = "Maximum"
  threshold                 = "9"
  treat_missing_data        = "missing"

  dimensions {
    Currency = "USD"
  }
}

$ terraform show
No state.

$ terraform import -provider=aws.billing aws_cloudwatch_metric_alarm.billing BillingAlarm
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: aws_cloudwatch_metric_alarm.billing: Importing from ID "BillingAlarm"...
aws_cloudwatch_metric_alarm.billing: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarm)
aws_cloudwatch_metric_alarm.billing: Refreshing state... (ID: BillingAlarm)
eu-west-1


Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

Resource in file, aws.billing in file, aws in file, aws.billing specified on command line

Resource imports and useless prompt is not shown (but I think this is a bug; the prompt seems to not show only because aws happens to be defined in the file).

$ cat main.tf 
provider "aws" {
  region = "eu-west-1"
}

provider "aws" {
  region = "us-east-1"
  alias = "billing"
}

resource "aws_cloudwatch_metric_alarm" "billing" {
  provider                  = "aws.billing"
  alarm_name                = "BillingAlarm"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = "1"
  metric_name               = "EstimatedCharges"
  namespace                 = "AWS/Billing"
  period                    = "${6*60*60}"
  statistic                 = "Maximum"
  threshold                 = "9"
  treat_missing_data        = "missing"

  dimensions {
    Currency = "USD"
  }
}

$ terraform show
No state.

$ terraform import -provider=aws.billing aws_cloudwatch_metric_alarm.billing BillingAlarm
aws_cloudwatch_metric_alarm.billing: Importing from ID "BillingAlarm"...
aws_cloudwatch_metric_alarm.billing: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarm)
aws_cloudwatch_metric_alarm.billing: Refreshing state... (ID: BillingAlarm)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

Resource in file, aws.billing in file, aws in file, aws.billing not specified on command line

Resource fails to import and useless prompt is not shown (this is consistent with it incorrectly using the aws provider).

$ cat main.tf 
provider "aws" {
  region = "eu-west-1"
}

provider "aws" {
  region = "us-east-1"
  alias = "billing"
}

resource "aws_cloudwatch_metric_alarm" "billing" {
  provider                  = "aws.billing"
  alarm_name                = "BillingAlarm"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = "1"
  metric_name               = "EstimatedCharges"
  namespace                 = "AWS/Billing"
  period                    = "${6*60*60}"
  statistic                 = "Maximum"
  threshold                 = "9"
  treat_missing_data        = "missing"

  dimensions {
    Currency = "USD"
  }
}

$ terraform show
No state.

$ terraform import aws_cloudwatch_metric_alarm.billing BillingAlarm
aws_cloudwatch_metric_alarm.billing: Importing from ID "BillingAlarm"...
aws_cloudwatch_metric_alarm.billing: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarm)
aws_cloudwatch_metric_alarm.billing: Refreshing state... (ID: BillingAlarm)
Error importing: 1 error(s) occurred:

* aws_cloudwatch_metric_alarm.billing (import id: BillingAlarm): 1 error(s) occurred:

* import aws_cloudwatch_metric_alarm.billing result: BillingAlarm: import aws_cloudwatch_metric_alarm.billing (id: BillingAlarm): Terraform detected a resource with this ID doesn't
exist. Please verify the ID is correct. You cannot import non-existent
resources using Terraform import.

Resource in file, aws.billing in file, aws not in file, aws.billing not specified on command line

Prompt is shown; whether the import succeeds depends on what is typed in the prompt. Again it seems that the lack of aws in the file is triggering the prompt, and it is not respecting the aws.billing specified on the resource.

$ cat main.tf 
provider "aws" {
  region = "us-east-1"
  alias = "billing"
}

resource "aws_cloudwatch_metric_alarm" "billing" {
  provider                  = "aws.billing"
  alarm_name                = "BillingAlarm"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = "1"
  metric_name               = "EstimatedCharges"
  namespace                 = "AWS/Billing"
  period                    = "${6*60*60}"
  statistic                 = "Maximum"
  threshold                 = "9"
  treat_missing_data        = "missing"

  dimensions {
    Currency = "USD"
  }
}

$ terraform show
No state.

$ terraform import aws_cloudwatch_metric_alarm.billing BillingAlarm
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: eu-west-1

aws_cloudwatch_metric_alarm.billing: Importing from ID "BillingAlarm"...
aws_cloudwatch_metric_alarm.billing: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarm)
aws_cloudwatch_metric_alarm.billing: Refreshing state... (ID: BillingAlarm)
Error importing: 1 error(s) occurred:

* aws_cloudwatch_metric_alarm.billing (import id: BillingAlarm): 1 error(s) occurred:

* import aws_cloudwatch_metric_alarm.billing result: BillingAlarm: import aws_cloudwatch_metric_alarm.billing (id: BillingAlarm): Terraform detected a resource with this ID doesn't
exist. Please verify the ID is correct. You cannot import non-existent
resources using Terraform import.

$ terraform import aws_cloudwatch_metric_alarm.billing BillingAlarm
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: us-east-1

aws_cloudwatch_metric_alarm.billing: Importing from ID "BillingAlarm"...
aws_cloudwatch_metric_alarm.billing: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarm)
aws_cloudwatch_metric_alarm.billing: Refreshing state... (ID: BillingAlarm)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

I think those four cases cover the different behaviours I've seen. Let me know if you want anything else.

@bchallenor okay I was able to reproduce this issue with only one provider in the config, and the resource added to the config prior to importing.

$ tf show
No state.

$ cat main.tf 
provider "aws" {
  region = "us-east-1"
  alias = "bar"
}

resource "aws_cloudwatch_metric_alarm" "bar" {
  provider = "aws.bar"
  alarm_name                = "BillingAlarmBar"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = "1"
  metric_name               = "EstimatedCharges"
  namespace                 = "AWS/Billing"
  period                    = "${6*60*60}"
  statistic                 = "Maximum"
  threshold                 = "9"
  treat_missing_data        = "missing"

  dimensions {
    Currency = "USD"
  }
}

$ tf import -provider=aws.bar aws_cloudwatch_metric_alarm.bar BillingAlarmBar
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: aws_cloudwatch_metric_alarm.bar: Importing from ID "BillingAlarmBar"...
aws_cloudwatch_metric_alarm.bar: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarmBar)
aws_cloudwatch_metric_alarm.bar: Refreshing state... (ID: BillingAlarmBar)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

However, I cannot reproduce the issue if the resource is not defined in the configuration prior to the import:

$ tf show
No state.

$ cat main.tf 
provider "aws" {
  region = "us-east-1"
  alias = "bar"
}

$ tf import -provider=aws.bar aws_cloudwatch_metric_alarm.bar BillingAlarmBar
aws_cloudwatch_metric_alarm.bar: Importing from ID "BillingAlarmBar"...
aws_cloudwatch_metric_alarm.bar: Import complete!
  Imported aws_cloudwatch_metric_alarm (ID: BillingAlarmBar)
aws_cloudwatch_metric_alarm.bar: Refreshing state... (ID: BillingAlarmBar)

Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

However, this is definitely a bug! Going to flag this as core as such, and assign to @jbardin.

I am going to close this issue due to inactivity. We've made many changes to import since this was opened. If you are still experiencing this issue with Terraform v0.13, please open a new issue and fill out the template.
Thanks!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings