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

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

How to import existing vpc to this module

问题

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

  1. Hello I'm trying to import the existing resources using terraform module `terraform-aws-modules/vpc/aws`
  2. this is my module definition
  3. module "vpc" {
  4. source = "terraform-aws-modules/vpc/aws"
  5. name = "eks-dev"
  6. cidr = "10.0.0.0/16"
  7. azs = ["us-east-1b", "us-east-1a"]
  8. public_subnets = ["10.0.2.0/24", "10.0.1.0/24"]
  9. enable_nat_gateway = false
  10. enable_vpn_gateway = false
  11. tags = {
  12. Terraform = "true"
  13. Environment = "dev"
  14. }
  15. }

当我运行计划时

  1. # module.vpc.module.vpc.aws_vpc.this[0] will be created
  2. + resource "aws_vpc" "this" {
  3. + arn = (known after apply)
  4. + cidr_block = "10.0.0.0/16"
  5. + default_network_acl_id = (known after apply)
  6. + default_route_table_id = (known after apply)
  7. + default_security_group_id = (known after apply)
  8. + dhcp_options_id = (known after apply)
  9. + enable_dns_hostnames = true
  10. + enable_dns_support = true
  11. + enable_network_address_usage_metrics = (known after apply)
  12. + id = (known after apply)
  13. + instance_tenancy = "default"
  14. + ipv6_association_id = (known after apply)
  15. + ipv6_cidr_block = (known after apply)
  16. + ipv6_cidr_block_network_border_group = (known after apply)
  17. + main_route_table_id = (known after apply)
  18. + owner_id = (known after apply)
  19. + tags = {
  20. + "Environment" = "dev"
  21. + "Name" = "eks-dev"
  22. + "Terraform" = "true"
  23. }
  24. + tags_all = {
  25. + "Environment" = "dev"
  26. + "Name" = "eks-dev"
  27. + "Terraform" = "true"
  28. }
  29. }

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

  1. terraform import module.vpc.aws_vpc.this vpc-0d8bf51e96d3bc819
  2. Error: resource address "module.vpc.aws_vpc.this" does not exist in the configuration.
  3. Before importing this resource, please create its configuration in module.vpc. For example:
  4. resource "aws_vpc" "this" {
  5. # (resource arguments)
  6. }

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

  1. terraform import "module.vpc.aws_subnet.public" "subnet-0d958d42eec08c493"
  2. Error: resource address "module.vpc.aws_subnet.public" does not exist in the configuration.
  3. Before importing this resource, please create its configuration in module.vpc. For example:
  4. resource "aws_subnet" "public" {
  5. # (resource arguments)
  6. }

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

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

  1. terraform import 'module.vpc.module.vpc.aws_vpc.this[0]' vpc-0d8bf51e96d3bc819
  2. module.vpc.module.vpc.aws_vpc.this[0]: Importing from ID "vpc-0d8bf51e96d3bc819"...
  3. module.vpc.module.vpc.aws_vpc.this[0]: Import prepared!
  4. Prepared aws_vpc for import
  5. module.vpc.module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-0d8bf51e96d3bc819]
  6. Import successful!
  7. The resources that were imported are shown above. These resources are now in
  8. 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

  1. module "vpc" {
  2. source = "terraform-aws-modules/vpc/aws"
  3. name = "eks-dev"
  4. cidr = "10.0.0.0/16"
  5. azs = ["us-east-1b", "us-east-1a"]
  6. public_subnets = ["10.0.2.0/24", "10.0.1.0/24"]
  7. enable_nat_gateway = false
  8. enable_vpn_gateway = false
  9. tags = {
  10. Terraform = "true"
  11. Environment = "dev"
  12. }
  13. }

When i run the plan

  1. # module.vpc.module.vpc.aws_vpc.this[0] will be created
  2. + resource "aws_vpc" "this" {
  3. + arn = (known after apply)
  4. + cidr_block = "10.0.0.0/16"
  5. + default_network_acl_id = (known after apply)
  6. + default_route_table_id = (known after apply)
  7. + default_security_group_id = (known after apply)
  8. + dhcp_options_id = (known after apply)
  9. + enable_dns_hostnames = true
  10. + enable_dns_support = true
  11. + enable_network_address_usage_metrics = (known after apply)
  12. + id = (known after apply)
  13. + instance_tenancy = "default"
  14. + ipv6_association_id = (known after apply)
  15. + ipv6_cidr_block = (known after apply)
  16. + ipv6_cidr_block_network_border_group = (known after apply)
  17. + main_route_table_id = (known after apply)
  18. + owner_id = (known after apply)
  19. + tags = {
  20. + "Environment" = "dev"
  21. + "Name" = "eks-dev"
  22. + "Terraform" = "true"
  23. }
  24. + tags_all = {
  25. + "Environment" = "dev"
  26. + "Name" = "eks-dev"
  27. + "Terraform" = "true"
  28. }
  29. }

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

  1. terraform import module.vpc.aws_vpc.this vpc-0d8bf51e96d3bc819
  2. Error: resource address "module.vpc.aws_vpc.this" does not exist in the configuration.
  3. Before importing this resource, please create its configuration in module.vpc. For example:
  4. resource "aws_vpc" "this" {
  5. # (resource arguments)
  6. }

same for public subnet

  1. terraform import "module.vpc.aws_subnet.public" "subnet-0d958d42eec08c493"
  2. Error: resource address "module.vpc.aws_subnet.public" does not exist in the configuration.
  3. Before importing this resource, please create its configuration in module.vpc. For example:
  4. resource "aws_subnet" "public" {
  5. # (resource arguments)
  6. }

How to resolve this is the syntax is correct ?

based on Marko E commen's this solve the problem

  1. terraform import 'module.vpc.module.vpc.aws_vpc.this[0]' vpc-0d8bf51e96d3bc819
  2. module.vpc.module.vpc.aws_vpc.this[0]: Importing from ID "vpc-0d8bf51e96d3bc819"...
  3. module.vpc.module.vpc.aws_vpc.this[0]: Import prepared!
  4. Prepared aws_vpc for import
  5. module.vpc.module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-0d8bf51e96d3bc819]
  6. Import successful!
  7. The resources that were imported are shown above. These resources are now in
  8. your Terraform state and will henceforth be managed by Terraform.

答案1

得分: 3

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

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

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

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

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

  1. 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:

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

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

  1. # 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:

  1. 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:

确定