如何将现有的虚拟专用网络导入到此模块中

huangapple go评论75阅读模式
英文:

How to import existing vpc to this module

问题

以下是您要翻译的代码部分:

Hello I'm trying to import the existing resources using terraform module `terraform-aws-modules/vpc/aws`

this is my module definition

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"

  name = "eks-dev"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1b", "us-east-1a"]
  public_subnets = ["10.0.2.0/24", "10.0.1.0/24"]

  enable_nat_gateway = false
  enable_vpn_gateway = false

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

当我运行计划时

 # module.vpc.module.vpc.aws_vpc.this[0] will be created
  + resource "aws_vpc" "this" {
      + arn                                  = (known after apply)
      + cidr_block                           = "10.0.0.0/16"
      + default_network_acl_id               = (known after apply)
      + default_route_table_id               = (known after apply)
      + default_security_group_id            = (known after apply)
      + dhcp_options_id                      = (known after apply)
      + enable_dns_hostnames                 = true
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + id                                   = (known after apply)
      + instance_tenancy                     = "default"
      + ipv6_association_id                  = (known after apply)
      + ipv6_cidr_block                      = (known after apply)
      + ipv6_cidr_block_network_border_group = (known after apply)
      + main_route_table_id                  = (known after apply)
      + owner_id                             = (known after apply)
      + tags                                 = {
          + "Environment" = "dev"
          + "Name"        = "eks-dev"
          + "Terraform"   = "true"
        }
      + tags_all                             = {
          + "Environment" = "dev"
          + "Name"        = "eks-dev"
          + "Terraform"   = "true"
        }
    }

这向我显示了足够的信息来导入,但是当我尝试导入这些内容时

❯ terraform import module.vpc.aws_vpc.this vpc-0d8bf51e96d3bc819
Error: resource address "module.vpc.aws_vpc.this" does not exist in the configuration.

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

resource "aws_vpc" "this" {
  # (resource arguments)
}

对于公共子网也是相同的情况

❯ terraform import "module.vpc.aws_subnet.public" "subnet-0d958d42eec08c493"

Error: resource address "module.vpc.aws_subnet.public" does not exist in the configuration.

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

resource "aws_subnet" "public" {
  # (resource arguments)
}

如何解决这个问题,语法是否正确?

根据Marko E的评论,这解决了问题

❯ terraform import 'module.vpc.module.vpc.aws_vpc.this[0]' vpc-0d8bf51e96d3bc819
module.vpc.module.vpc.aws_vpc.this[0]: Importing from ID "vpc-0d8bf51e96d3bc819"...
module.vpc.module.vpc.aws_vpc.this[0]: Import prepared!
  Prepared aws_vpc for import
module.vpc.module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-0d8bf51e96d3bc819]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

英文:

Hello I'm trying to import the existing resources using terraform module terraform-aws-modules/vpc/aws

this is my module defintion

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"

  name = "eks-dev"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1b", "us-east-1a"]
  public_subnets = ["10.0.2.0/24", "10.0.1.0/24"]

  enable_nat_gateway = false
  enable_vpn_gateway = false

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

When i run the plan

 # module.vpc.module.vpc.aws_vpc.this[0] will be created
  + resource "aws_vpc" "this" {
      + arn                                  = (known after apply)
      + cidr_block                           = "10.0.0.0/16"
      + default_network_acl_id               = (known after apply)
      + default_route_table_id               = (known after apply)
      + default_security_group_id            = (known after apply)
      + dhcp_options_id                      = (known after apply)
      + enable_dns_hostnames                 = true
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + id                                   = (known after apply)
      + instance_tenancy                     = "default"
      + ipv6_association_id                  = (known after apply)
      + ipv6_cidr_block                      = (known after apply)
      + ipv6_cidr_block_network_border_group = (known after apply)
      + main_route_table_id                  = (known after apply)
      + owner_id                             = (known after apply)
      + tags                                 = {
          + "Environment" = "dev"
          + "Name"        = "eks-dev"
          + "Terraform"   = "true"
        }
      + tags_all                             = {
          + "Environment" = "dev"
          + "Name"        = "eks-dev"
          + "Terraform"   = "true"
        }
    }

This shows me enough information on how to import however when I try importing the stuff

❯ terraform import module.vpc.aws_vpc.this vpc-0d8bf51e96d3bc819
Error: resource address "module.vpc.aws_vpc.this" does not exist in the configuration.

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

resource "aws_vpc" "this" {
  # (resource arguments)
}

same for public subnet

❯ terraform import "module.vpc.aws_subnet.public" "subnet-0d958d42eec08c493"

Error: resource address "module.vpc.aws_subnet.public" does not exist in the configuration.

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

resource "aws_subnet" "public" {
  # (resource arguments)
}

How to resolve this is the syntax is correct ?

based on Marko E commen's this solve the problem

❯ terraform import 'module.vpc.module.vpc.aws_vpc.this[0]' vpc-0d8bf51e96d3bc819
module.vpc.module.vpc.aws_vpc.this[0]: Importing from ID "vpc-0d8bf51e96d3bc819"...
module.vpc.module.vpc.aws_vpc.this[0]: Import prepared!
  Prepared aws_vpc for import
module.vpc.module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-0d8bf51e96d3bc819]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

答案1

得分: 3

由于看起来您正在使用嵌套模块,即模块内部的模块,仅使用问题中的命令是不足够的:

terraform import module.vpc.aws_vpc.this vpc-0d8bf51e96d3bc819

从计划输出中可以看出,模块实际上将尝试创建:

# module.vpc.module.vpc.aws_vpc.this[0] 将被创建

这也意味着为了使导入工作,您必须使用以下命令:

terraform import 'module.vpc.module.vpc.aws_vpc.this[0]' vpc-0d8bf51e96d3bc819

对于属于嵌套模块的所有资源,应应用相同的逻辑。

英文:

Since it seems you are using nested modules, i.e., modules within modules, it is not enough to use only the command from the question:

terraform import module.vpc.aws_vpc.this vpc-0d8bf51e96d3bc819

As can be seen from the plan output, the module will actually try to create:

# module.vpc.module.vpc.aws_vpc.this[0] will be created

Which also means that for the import to work, you have to use the following command:

terraform import 'module.vpc.module.vpc.aws_vpc.this[0]' vpc-0d8bf51e96d3bc819

The same logic should be applied for all the resources that belong to nested modules.

huangapple
  • 本文由 发表于 2023年7月20日 17:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定