3

我想将现有的 aws 资源 iam-role 'DEVOPS' 导入到我的 terraform 管理中。

虽然存在资源,但我收到以下错误 -

错误:无法导入不存在的远程对象

在尝试将现有对象导入 aws_iam_role.okta_devops_role 时,提供程序检测到不存在具有给定 ID 的对象。只能导入预先存在的对象;检查 id 是否正确以及它是否与提供者配置的区域或端点相关联,或者使用“terraform apply”为此资源创建一个新的远程对象。

我在 main.tf 中创建了空资源 -> aws_iam_role.devops_role

4

3 回答 3

4

您应该能够通过执行以下操作来导入现有 IAM 角色资源:

  1. 像这样为资源创建存根main.tf
resource "aws_iam_role" "DEVOPS" {
  # stub
}
  1. 运行导入命令:
terraform import aws_iam_role.DEVOPS DEVOPS
  1. 完成后,显示资源并更新您在步骤 1 中创建的资源存根:
terraform show

这是文档的链接

于 2020-11-20T18:13:08.827 回答
0

只是作为一种补充解决方案。如果你已经定义了你的aws_iam_role内部模块,你可能需要在terraform import命令中添加两个前缀。从模块中找到正确资源名称的一种方法是使用terraform plan命令。

例如,aws_iam_role模块内的这个资源

resource "aws_iam_role" "reports_role" {
  name = "${var.environment}_reports_role"

  inline_policy {
    name = "${var.environment}_s3_access_policy"
    policy = templatefile("${path.module}/templates/s3_access_policy.json", {
      bucket_name = var.bucket_name
    })
  }
}

尝试为dev覆盖(环境)部署它时出现以下错误:

╷│错误:创建IAM角色时出错(prod_reports_role):EntityAlreadyExists:名称为prod_reports_role的角色已经存在。│ 状态码:409,请求 id:************************ │ │ with module.aws_role.aws_iam_role.reports_role,│ on ../../modules/authorization /roles/role.tf 第 1 行,在资源“aws_iam_role”“reports_role”中:│1:资源“aws_iam_role”“reports_role”{│╵</p>

使用该terraform plan命令后,我可以看到它的名称并导入它。如您所见,我必须module.aws_roleaws_iam_role.reports_role.

terraform import module.aws_role.aws_iam_role.reports_role dev_reports_role
于 2021-11-24T11:49:11.010 回答
-1

无法导入未通过 terraform 配置的现有资源。

由于 terraform 确实通过 terraform 状态文件引用资源并检测配置漂移

不过,您可以尝试:-

https://github.com/GoogleCloudPlatform/terraformer#use-with-aws
于 2020-05-12T05:05:35.590 回答