【问题标题】:terraform import an existing AWS resource to a moduleterraform 将现有 AWS 资源导入模块
【发布时间】:2021-04-17 01:53:36
【问题描述】:

我有一个已经准备好 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-services import terraform terraform-modules


    【解决方案1】:

    将资源导入模块的正确方法在the docs中举例说明:

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

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

    terraform import module.aws_ecr_repository.c2m_an c2m_an 
    

    【讨论】:

    • 谢谢马尔辛。也许我可以跳过 因为 ECR 导入不需要。是的,它起作用了:)
    • @AllaSasikanth 没问题。我更新了答案。如果它有帮助,我们将不胜感激。
    猜你喜欢
    • 2023-01-02
    • 2021-11-06
    • 2020-04-29
    • 1970-01-01
    • 2020-01-23
    • 2020-08-27
    • 1970-01-01
    • 2021-01-04
    • 2021-05-25
    相关资源
    最近更新 更多