terraform 将现有的 AWS 资源导入模块

时间:2021-01-12 05:27:31

标签: amazon-web-services import terraform terraform-modules

我有一个已经准备好 ECR 和 IAM 的 AWS 账户。我现在正在使用 terraform 模块创建一个新环境。但是我找不到将现有 IAM 和 ECR 资源导入我的模块的方法。当我运行命令 terraform import aws_ecr_repository.c2m_an c2m_an 时,我收到一个错误

Error: resource address "aws_ecr_repository.c2m_cs" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "aws_ecr_repository" "c2m_cs" {
  # (resource arguments)
}

我的ECR模块定义如下:

resource "aws_ecr_repository" "c2m_cs" {
  name = var.c2m_cs#"c2m_cs"
}

output "c2m_cs" {
  value = "terraform import aws_ecr_repository.c2m_cs c2m_cs"
}

在我的环境文件夹中的 main.tf 文件中,我的模块定义如下:

module "ecr" {
  source = "../modules/ecr"
  c2m_cs = module.ecr.c2m_cs
}

1 个答案:

答案 0 :(得分:3)

将资源导入模块的正确方法以the docs为例:

terraform import module.foo.aws_instance.bar i-abcd1234

因此,在您的情况下,它将是:

terraform import module.aws_ecr_repository.c2m_an c2m_an 
相关问题