Terraform cloud : Import existing resource

svb*_*ask 5 terraform terraform-provider-aws terraform-cloud

I am using terraform cloud to manage the state of the infrastructure provisioned in AWS.

I am trying to use terraform import to import an existing resource that is currently not managed by terraform.

I understand terraform import is a local only command. I have set up a workspace reference as follows:

terraform {
  required_version = "~> 0.12.0"

  backend "remote" {
    hostname = "app.terraform.io"
    organization = "foo"

    workspaces {
      name = "bar"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

The AWS credentials are configured in the remote cloud workspace but terraform does not appear to be referencing the AWS credentials from the workspace but instead falls back trying to using the local credentials which points to a different AWS account. I would like Terraform to use the credentials by referencing the variables in the workspace when I run terraform import.

When I comment out the locally configured credentials, I get the error:

Error: No valid credential sources found for AWS Provider.
Run Code Online (Sandbox Code Playgroud)

I would have expected terraform to use the credentials configured in the workspace.

Note that terraform is able to use the credentials correctly, when I run the plan/apply command directly from the cloud console.

小智 0

Use the data provider, for Example:-

data "terraform_remote_state" "test" {
  backend = "s3"
  config = {
    bucket = "BUCKET_NAME"
    key    = "BUCKET_KEY WHERE YOUR TERRAFORM.TFSTATE FILE IS PRESENT"
    region = "CLOUD REGION"
  }
}
Run Code Online (Sandbox Code Playgroud)

Now you can call your provisioned resources Example :-

For getting the VPC ID:-

data.terraform_remote_state.test.*.outputs.vpc_id
Run Code Online (Sandbox Code Playgroud)

Just make the cloud resource property you want to refer should be in exported as output and stored in terraform.tfstate file

  • 我正在尝试导入不受 terraform 管理的现有资源。terraform_remote_state 在这种情况下无济于事。 (2认同)