terraform aws apigateway path_part

terraform aws apigateway path_part

我在下面的场景中尝试了一个 POC,使用的是 terraform api_gateway。

path= /demo/user(GET) -> 调用 lamda 函数(你好)。
path= /demo/user/{id)(put) -> 调用 lamda 函数(测试)。
所以在这里我创建了以下资源

resource "aws_api_gateway_rest_api" "MyDemoAPI" {
  name        = "MyDemoAPI"
  description = "This is my API for demonstration purposes"
}

resource "aws_api_gateway_resource" "MyDemoResource" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  parent_id   = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
  path_part   = "demo"
}
resource "aws_api_gateway_integration" "MyDemoIntegration" {
  rest_api_id          = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id          = aws_api_gateway_resource.MyDemoResource.id
  http_method          = aws_api_gateway_method.MyDemoMethod.http_method
  type                 = "AWS_PROXY"
  uri                  = "<lambda function arn>/invocation"
}

在 terraform apply 中,它正在 /demo 下创建资源
但是这里我该如何实现路径?
path= /demo/user(GET) -> 调用 lamda 函数 (你好).
path= /demo/user/{id)(PUT) -> 调用 lamda 函数(测试)。

任何意见将不胜感激。

对于 /demo/user (GET),您需要在 'demo' 下创建资源 'user' 并为 'user' 资源添加集成。对于 /demo/user/{id} (PUT),您需要在 'user' 下创建另一个资源 'userId' 并为 'userId' 资源添加集成。

必须使用相应的 Lambda 函数为它们添加 Http 方法和 Lambda 集成。

更新后的代码如下所示。

# root resource
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
  name        = "MyDemoAPI"
  description = "This is my API for demonstration purposes"
}

# demo resource (corresponding to path /demo)
resource "aws_api_gateway_resource" "MyDemoResource" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  parent_id   = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
  path_part   = "demo"
}

# user resource (corresponding to path /demo/user)
resource "aws_api_gateway_resource" "MyDemoUserResource" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  parent_id   = aws_api_gateway_resource.MyDemoResource.id
  path_part   = "user"
}

# adding GET method for path /demo/user
resource "aws_api_gateway_method" "MyDemoUserGetMethod" {
  rest_api_id   = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id   = aws_api_gateway_resource.MyDemoUserResource.id
  http_method   = "GET"
  authorization = "NONE"
}

# userId resource (corresponding to path /demo/user/{id})
resource "aws_api_gateway_resource" "MyDemoUserIdResource" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  parent_id   = aws_api_gateway_resource.MyDemoUserResource.id
  path_part   = "{id}"
}

# adding PUT method for path /demo/user/{id}
resource "aws_api_gateway_method" "MyDemoUserIdPutMethod" {
  rest_api_id   = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id   = aws_api_gateway_resource.MyDemoUserIdResource.id
  http_method   = "PUT"
  authorization = "NONE"
}

# adding Lambda integration for GET at /demo/user
resource "aws_api_gateway_integration" "MyDemoUserIntegration" {
  rest_api_id          = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id          = aws_api_gateway_resource.MyDemoUserResource.id
  http_method          = aws_api_gateway_method.MyDemoUserGetMethod.http_method
  type                 = "AWS_PROXY"
  uri                  = "<lambda function arn>/invocation"
}

# adding Lambda integration for PUT at /demo/user/{id}
resource "aws_api_gateway_integration" "MyDemoUserIdIntegration" {
  rest_api_id          = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id          = aws_api_gateway_resource.MyDemoUserIdResource.id
  http_method          = aws_api_gateway_method.MyDemoUserIdPutMethod.http_method
  type                 = "AWS_PROXY"
  uri                  = "<lambda function arn>/invocation"
}

参考资料