Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nivleshc/5d18c6f3b0792d5c82125b3bbe3ce024 to your computer and use it in GitHub Desktop.
Save nivleshc/5d18c6f3b0792d5c82125b3bbe3ce024 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.
# Create CORS for fetch-object using OPTIONS method
resource "aws_api_gateway_method" "fetch_object_options" {
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id
resource_id = aws_api_gateway_resource.fetch_object.id
http_method = "OPTIONS"
authorization = "NONE"
}
# Set up a MOCK integration for the OPTIONS method.
resource "aws_api_gateway_integration" "fetch_object_options_integration" {
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id
resource_id = aws_api_gateway_resource.fetch_object.id
http_method = aws_api_gateway_method.fetch_object_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" "fetch_object_options_method_response" {
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id
resource_id = aws_api_gateway_resource.fetch_object.id
http_method = aws_api_gateway_method.fetch_object_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" "fetch_object_options_integration_response" {
rest_api_id = aws_api_gateway_rest_api.photo_location_map.id
resource_id = aws_api_gateway_resource.fetch_object.id
http_method = aws_api_gateway_method.fetch_object_options.http_method
status_code = aws_api_gateway_method_response.fetch_object_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