Reputation: 107
I was trying one POC in the below scenario using was terraform api_gateway.
path= /demo/user(GET) -> invoke lamda function (hello).
path= /demo/user/{id)(put) -> invoke lamda function (test).
so here i have create the below resource
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"
}
in terraform apply it is creating resource under /demo
but here how do I achieve the path?
path= /demo/user(GET) -> invoke lamda function (hello).
path= /demo/user/{id)(PUT) -> invoke lamda function (test).
Any comment will be highly appreciated.
Upvotes: 2
Views: 2493
Reputation: 676
For /demo/user (GET), you need to create resource 'user' under 'demo' and add integration for 'user' resource. For /demo/user/{id} (PUT), you need to create another resource 'userId' under 'user' and add integration for 'userId' resource.
Http methods and Lambda integrations must be added for both of them using corresponding Lambda functions.
Updated code would be something like below.
# 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"
}
References
Upvotes: 6