如何将现有资源导入terraform

时间:2019-09-23 00:12:16

标签: amazon-web-services terraform

我正在使用Terraform v0.12.6来管理AWS基础设施。将配置部署到AWS时出现此错误。我知道我需要导入现有资源,但是我不知道我应该使用的参数ID。

Error: Error creating Security Group: InvalidGroup.Duplicate: The security group 'SecuritySearchElasticSearchencr' already exists for VPC 'vpc-0cce833ea304b0215'
    status code: 400, request id: c3bef103-023a-4d6f-888b-bcb8d024eff8

  on deploy/infra/modules/elasticsearch/security-groups.tf line 1, in resource "aws_security_group" "es":
   1: resource "aws_security_group" "es" {

运行terraform import aws_security_group.es vpc-0cce833ea304b0215时出现此错误:

Error: resource address "aws_security_group.es" does not exist in the configuration.

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

resource "aws_security_group" "es" {
  # (resource arguments)
}

我也尝试导入安全组ID,但仍然存在相同的错误:

#terraform import aws_security_group.es sg-0227291ba8162542d

Error: resource address "aws_security_group.es" does not exist in the configuration.

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

resource "aws_security_group" "es" {
  # (resource arguments)
}

当我运行terraform plan时,我得到了:

 + resource "aws_security_group" "es" {
      + arn                    = (known after apply)
      + description            = "Allow HTTPS inbound traffic"
      + egress                 = [
          + {
              + cidr_blocks      = [
                  + "0.0.0.0/0",
                ]
              + description      = ""
              + from_port        = 0
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "-1"
              + security_groups  = []
              + self             = false
              + to_port          = 0
            },
        ]
      + id                     = (known after apply)
      + ingress                = [
          + {
              + cidr_blocks      = []
              + description      = ""
              + from_port        = 443
              + ipv6_cidr_blocks = []
              + prefix_list_ids  = []
              + protocol         = "tcp"
              + security_groups  = [
                  + "sg-08e48ea67d2f8ebd6",
                  + "sg-0bdc3d7ce660183d8",
                ]
              + self             = false
              + to_port          = 443
            },
        ]
      + name                   = "SecuritySearchElasticSearchencr"
      + owner_id               = (known after apply)
      + revoke_rules_on_delete = false
      + tags                   = {
          + "Application"            = "Security Search"
          + "Client"                 = "IRESS"
          + "DataClassification"     = "NoData"
          + "Name"                   = "Security Search ElasticSearch"
          + "Owner"                  = "platform"
          + "Product"                = "SharedServices"
          + "Schedule"               = "False"
          + "Service"                = "Elastic Search Cluster"
          + "TaggingStandardVersion" = "3"
        }
      + vpc_id                 = "vpc-0cce833ea304b0215"
    }

我想知道应该使用什么命令来导入现有资源?

1 个答案:

答案 0 :(得分:0)

可以使用安全组ID导入安全组,而使用VPC ID导入。

如果您获取SecuritySearchElasticSearchencr的ID并将其传递给terraform import命令,它应该可以工作。

 terraform import aws_security_group.es sg-xxxxxx