Skip to content

Instantly share code, notes, and snippets.

@gungoren
Created January 23, 2023 15:00
Show Gist options
  • Save gungoren/9179e3a1e26757dbb9be42ec829a4ad9 to your computer and use it in GitHub Desktop.
Save gungoren/9179e3a1e26757dbb9be42ec829a4ad9 to your computer and use it in GitHub Desktop.
resource "aws_api_gateway_rest_api" "api" {
name = "recognition-api"
}
resource "aws_api_gateway_resource" "search" {
path_part = "search"
parent_id = aws_api_gateway_rest_api.api.root_resource_id
rest_api_id = aws_api_gateway_rest_api.api.id
}
resource "aws_api_gateway_method" "method" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.search.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.search.id
http_method = aws_api_gateway_method.method.http_method
integration_http_method = "POST"
type = "AWS"
uri = aws_lambda_function.search_function.invoke_arn
content_handling = "CONVERT_TO_TEXT"
}
resource "aws_api_gateway_method_response" "response_200" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.search.id
http_method = aws_api_gateway_method.method.http_method
status_code = "200"
response_models = {}
response_parameters = {}
}
resource "aws_lambda_permission" "search_function" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.search_function.function_name
principal = "apigateway.amazonaws.com"
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
source_arn = "arn:aws:execute-api:${var.region}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method.http_method}${aws_api_gateway_resource.search.path}"
}
resource "aws_api_gateway_deployment" "apig_deployment" {
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = var.api_stage_name
lifecycle {
create_before_destroy = true
}
depends_on = [
aws_api_gateway_resource.search,
aws_api_gateway_method.method,
aws_api_gateway_integration.integration
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment