【发布时间】: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