【问题标题】:Terraform: how to import AWS cross-account resource?Terraform:如何导入 AWS 跨账户资源?
【发布时间】:2020-02-27 21:54:15
【问题描述】:

如何将现有 AWS 资源导入 Terraform 状态,该资源存在于不同账户中?

terraform import module.mymodule.aws_iam_policy.policy arn:aws:iam::123456789012:policy/mypolicy

给出以下错误:

Error: Cannot import non-existent remote object

While attempting to import an existing object to aws_iam_policy.policy, the
provider detected that no object exists with the given id. Only pre-existing
objects can be imported; check that the id is correct and that it is
associated with the provider's configured region or endpoint, or use
"terraform apply" to create a new remote object for this resource.

资源是在一个帐户中使用在名为mymodule 的模块中定义的不同配置器创建的:

module "mymodule" {
    // ... define variables for the module
}

// within the module
provider "aws" {
  alias = "cross-account"
  region = "eu-west-2"
  assume_role {
    role_arn = var.provider_role_arn
  }
}

resource "aws_iam_policy" "policy" {
  provider = "aws.cross-account"
  name        = var.policy-name
  path        = var.policy-path
  description = var.policy-description

  policy = var.policy-document
}

如何导入跨账户资源?

更新:使用-provider 标志,我得到一个不同的错误:

Error: Provider configuration not present

To work with module.mymodule.aws_iam_policy.policy (import
id "arn:aws:iam::123456789012:policy/somepolicytoimport") its original provider
configuration at provider.aws.cross-account is required, but it has been
removed. This occurs when a provider configuration is removed while objects
created by that provider still exist in the state. Re-add the provider
configuration to destroy
module.mymodule.aws_iam_policy.policy (import id
"arn:aws:iam::123456789012:policy/somepolicytoimport"), after which you can remove
the provider configuration again.

【问题讨论】:

  • 您使用的是什么版本的 Terraform?
  • 最新版本(0.12.12)
  • 自 0.12.10 起,您不需要使用 -provider=aws.cross-account,但可能值得尝试看看这是否有帮助。
  • @ydaetskcoR 给出不同的错误,请参阅上面的编辑。

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

我认为您必须承担第二个帐户的角色,如下所示。

provider "aws" {
  assume_role {
    role_arn     = "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME"
    session_name = "SESSION_NAME"
    external_id  = "EXTERNAL_ID"
  }
}

[1]:https://www.terraform.io/docs/providers/aws/index.html

【讨论】:

  • 如果您阅读相同的链接,您会发现这些链接是可选的,所以这不是问题所在。
  • 是的,它是可选的,但您可以应用其他帐户的假设角色。这就是重点。
  • 他们已经设置了承担角色位。您刚刚添加了 2 个可选字段,用于担任您根本不需要的角色,仅用于识别 CloudTrail 中的会话。
【解决方案2】:

我在尝试导入 AWS acm 证书时遇到了同样的错误。

作为第一步,在导入资源之前,您需要在根模块(或其他相关模块)中创建其配置:

resource "aws_acm_certificate" "cert" {
  # (resource arguments)
}

否则你会得到以下错误:

错误:资源地址“aws_acm_certificate.cert”不存在 配置。

然后您可以通过提供相关的 arn 来导入资源:

$ terraform import aws_acm_certificate.cert <certificate-arn>

就像 cmets 中提到的 @ydaetskcoR - 如果您使用 v0.12.10+,则无需承担第二个帐户的角色。

但 Terraform 确实需要访问第二个帐户的凭据 - 因此请确保您提供相关帐户的凭据(而不是源帐户凭据),否则您将无法使用
Error: Cannot import non-existent remote object
像我一样几个小时(:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2021-12-21
    • 2021-04-17
    • 2023-01-02
    • 2020-01-23
    • 2019-03-16
    相关资源
    最近更新 更多