Created
March 23, 2025 23:34
-
-
Save nivleshc/5b6c63487bd1ed4444549854df66f886 to your computer and use it in GitHub Desktop.
This gist contains code from the file api_gateway.tf which is part of the blog-photo-location-map repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Allow API Gateway to invoke the regenerate-map lambda function | |
resource "aws_lambda_permission" "apigw_invoke_regenerate_map_lambda" { | |
statement_id = "AllowAPIGatewayInvoke" | |
action = "lambda:InvokeFunction" | |
function_name = aws_lambda_function.regenerate_map_lambda.function_name | |
principal = "apigateway.amazonaws.com" | |
source_arn = "${aws_api_gateway_rest_api.photo_location_map.execution_arn}/*/*" | |
} | |
# create the resource for regenerating map | |
resource "aws_api_gateway_resource" "regenerate_map" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
parent_id = aws_api_gateway_rest_api.photo_location_map.root_resource_id | |
path_part = local.api_gateway_regenerate_map_path | |
} | |
# Create the method on the regenerate map resource that uses POST method | |
resource "aws_api_gateway_method" "regenerate_map_post" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
resource_id = aws_api_gateway_resource.regenerate_map.id | |
http_method = "POST" | |
authorization = "NONE" | |
} | |
# Set up the integration between API Gateway and the regenerate map Lambda function | |
resource "aws_api_gateway_integration" "regenerate_map" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
resource_id = aws_api_gateway_resource.regenerate_map.id | |
http_method = aws_api_gateway_method.regenerate_map_post.http_method | |
integration_http_method = "POST" | |
type = "AWS_PROXY" | |
uri = aws_lambda_function.regenerate_map_lambda.invoke_arn | |
} | |
# Create CORS for regenerate-map using OPTIONS method | |
resource "aws_api_gateway_method" "regenerate_map_options" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
resource_id = aws_api_gateway_resource.regenerate_map.id | |
http_method = "OPTIONS" | |
authorization = "NONE" | |
} | |
# Set up a MOCK integration for the OPTIONS method. | |
resource "aws_api_gateway_integration" "regenerate_map_options_integration" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
resource_id = aws_api_gateway_resource.regenerate_map.id | |
http_method = aws_api_gateway_method.regenerate_map_options.http_method | |
type = "MOCK" | |
request_templates = { | |
"application/json" = "{\"statusCode\": 200}" | |
} | |
} | |
# Define the method response for OPTIONS, including CORS headers. | |
resource "aws_api_gateway_method_response" "regenerate_map_options_method_response" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
resource_id = aws_api_gateway_resource.regenerate_map.id | |
http_method = aws_api_gateway_method.regenerate_map_options.http_method | |
status_code = "200" | |
response_parameters = { | |
"method.response.header.Access-Control-Allow-Headers" = true | |
"method.response.header.Access-Control-Allow-Methods" = true | |
"method.response.header.Access-Control-Allow-Origin" = true | |
} | |
} | |
# Configure the integration response for OPTIONS to set the CORS headers. | |
resource "aws_api_gateway_integration_response" "regenerate_map_options_integration_response" { | |
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id | |
resource_id = aws_api_gateway_resource.regenerate_map.id | |
http_method = aws_api_gateway_method.regenerate_map_options.http_method | |
status_code = aws_api_gateway_method_response.regenerate_map_options_method_response.status_code | |
response_parameters = { | |
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'" | |
"method.response.header.Access-Control-Allow-Methods" = "'GET,POST,OPTIONS,PUT,PATCH,DELETE'" | |
"method.response.header.Access-Control-Allow-Origin" = "'*'" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment