gpt4 book ai didi

amazon-web-services - terraform aws apigateway path_part

转载 作者:行者123 更新时间:2023-12-02 01:58:50 25 4
gpt4 key购买 nike

我在以下场景中尝试使用 terraform api_gateway 进行 POC。

path=/demo/user(GET) -> 调用 lamda 函数 (hello)。
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 函数 (hello)。
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"
}

引用文献

关于amazon-web-services - terraform aws apigateway path_part,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69271797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com