首页 > 解决方案 > terraform 将现有 AWS 资源导入模块

问题描述

我有一个已经准备好 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
}

标签: amazon-web-servicesimportterraformterraform-modules

解决方案


文档中举例说明了将资源导入模块的正确方法:

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

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

terraform import module.aws_ecr_repository.c2m_an c2m_an 

推荐阅读