terraform 将现有 AWS 资源导入模块

All*_*nth 6 import amazon-web-services 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)
}
Run Code Online (Sandbox Code Playgroud)

我的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"
}
Run Code Online (Sandbox Code Playgroud)

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

module "ecr" {
  source = "../modules/ecr"
  c2m_cs = module.ecr.c2m_cs
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*cin 8

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

terraform import module.foo.aws_instance.bar i-abcd1234
Run Code Online (Sandbox Code Playgroud)

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

terraform import module.aws_ecr_repository.c2m_an c2m_an 
Run Code Online (Sandbox Code Playgroud)