如何使用 Terraform 在 API Gateway 中创建默认部署阶段?

How can I create a default deployment stage in API Gateway using Terraform?

我有一个使用 Terraform 的 API 网关设置。我需要能够访问基本路径上的 API 网关,即没有附加到基本 URL.

的阶段名称

https://{api_id}.execute-api.{region}.amazonaws.com/ <- 可接受

https://{api_id}.execute-api.{region}.amazonaws.com/{StageName} <- 不可接受

我会通过创建 default deployment stage like here.

从控制台执行此操作

我查看了 terraform 文档,但没有找到任何内容 here for stages

我希望能够通过创建默认阶段来完成此操作,而不是使用 aws_api_gateway_domain_name 资源

aws_api_gateway_domain_name 用于 REST API which does not have a default stage. To create such a stage you have to use HTTP API which in terraform is provided by the family of apigatewayv2 方法。

来自AWS documentation

You can create a $default stage that is served from the base of your API's URL—for example, https://{api_id}.execute-api.{region}.amazonaws.com/. You use this URL to invoke an API stage.

Terraform 文档没有提及这一点,但您可以创建一个阶段名称为 $default 的阶段。然后调用基本路径应该使用那个阶段。

resource "aws_apigatewayv2_stage" "example" {
  api_id = aws_apigatewayv2_api.example.id
  name   = "$default"
}