【问题标题】:How to add multiple AWS ClientVPN Routes using Terraform如何使用 Terraform 添加多个 AWS ClientVPN 路由
【发布时间】:2021-11-09 10:57:29
【问题描述】:

我有 AWS clientVPN,它是从 AWS 控制台手动创建的,它有大约 20 多个路由表条目。 现在,我想对其进行 terraform,以便我们可以使用 terraform 添加任何新路线。

我已经使用 terraform import 导入了 ClientVPN 信息。要导入所有现有路由,我可以一次导入一个路由,也可以为每个路由导入我需要在 main.tf 中添加资源条目,如下所示:

Command used to import the route table entry:
$ terraform import aws_ec2_client_vpn_route.example cvpn-endpoint-0e3e121d2,subnet-08acf2,<CIDR>
This command updates the .tfstate file and when I run terraform plan it gives me an error because I need to add resource section for this in main.tf file. 

resource "aws_ec2_client_vpn_route" "example" {
  client_vpn_endpoint_id = var.client_vpn_endpoint_id
  destination_cidr_block = "CIDR"
  target_vpc_subnet_id   = var.target_vpc_subnet_id
}

resource "aws_ec2_client_vpn_route" "example1" {
  client_vpn_endpoint_id = var.client_vpn_endpoint_id
  destination_cidr_block = "CIDR"
  target_vpc_subnet_id   = var.target_vpc_subnet_id
}

每次导入路由,都需要在 main.tf 中添加资源。如果我有 20 个路由表条目,那么我必须在 main.tf 文件中写入 20 个资源条目吗?

我只想在main.tf中使用一个资源入口,怎么可能?

导入后,当我运行 terraform 计划时,检查输出:

% terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

aws_ec2_client_vpn_route.example: Refreshing state... [id=cvpn-endpoint,subnet-02231,0.0.0.0/16]
aws_ec2_client_vpn_endpoint.example: Refreshing state... [id=cvpn-endpoint]

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
  - destroy

Terraform will perform the following actions:

  # aws_ec2_client_vpn_route.example will be destroyed
  - resource "aws_ec2_client_vpn_route" "example" {
      - client_vpn_endpoint_id = "cvpn-endpoint" -> null
      - description            = "Default Route" -> null
      - destination_cidr_block = "0.0.0.0/16" -> null
      - id                     = "cvpn-endpoint,subnet-02231308,0.0.0.0/16" -> null
      - origin                 = "associate" -> null
      - target_vpc_subnet_id   = "subnet-022313" -> null
      - type                   = "Nat" -> null
    }

  # aws_ec2_client_vpn_route.example["Default Route"] will be created
  + resource "aws_ec2_client_vpn_route" "example" {
      + client_vpn_endpoint_id = "cvpn-endpoint"
      + description            = "Default Route"
      + destination_cidr_block = "0.0.0.0/16"
      + id                     = (known after apply)
      + origin                 = (known after apply)
      + target_vpc_subnet_id   = "subnet-022313"
      + type                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

资源名称不匹配,这就是它再次销毁和创建的原因。 但是,当我应用 terraform 时,它会失败,因为它首先创建资源并且由于相同的 CIDR 而失败。

【问题讨论】:

  • 您可以在资源上将其写为for_each,然后作为适当的索引导入。如果您是 Terraform 的新手,那么这可能会有点令人困惑。
  • 您能否编辑您的问题以准确显示您尝试过的内容以及运行命令时产生的错误或计划输出(同时显示您运行的命令)?
  • 看起来您之前已经应用/导入了一个名为 aws_ec2_client_vpn_route.example 的资源,现在删除了它的配置,因此 Terraform 想要删除它。您可以改为使用terraform state rm aws_ec2_client_vpn_route.example 将其从状态中删除,然后再次运行该计划。看起来您还没有正确导入 aws_ec2_client_vpn_route.example[\"Default Route\"] 资源,因此需要按照我上面评论中的第二个链接执行此操作。
  • 在这一点上,虽然我认为你可能有点过头了。我可能会稍作停顿,通过learn.hashicorp.com 上的一些简单的事情或课程,而不是尝试用看起来像是生产资源的东西做更高级的事情。

标签: amazon-web-services terraform devops terraform-provider-aws sre


【解决方案1】:

您可以使用for_each 方法 Terraform 提供,它基本上循环并创建您在可变资源列表中拥有的资源数量。

variable "cidr_blocks" {
  description = ""
  default     = {
    "10.0.0.1/16" = 1
    "10.0.0.2/16" = 2
    "10.0.0.3/16" = 3
  }
}

resource "aws_ec2_client_vpn_route" "example" {
  for_each = var.cidr_blocks

  client_vpn_endpoint_id = var.client_vpn_endpoint_id
  destination_cidr_block = each.key
  target_vpc_subnet_id   = var.target_vpc_subnet_id
}

在本例中,您将创建 3 个 aws_ec2_client_vpn_route 资源。

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 2021-02-25
    • 1970-01-01
    • 2019-01-21
    • 2021-10-30
    • 2021-09-07
    • 2018-04-02
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多