Created
April 29, 2019 16:14
-
-
Save hervekhg/43d6ea921c27b5ce6dc9efb8978d9582 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# -------------------------------------------------------------- | |
# Step 1 : Create APIGateway | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_rest_api" "apigateway" { | |
name = "${data.consul_keys.ck.var.project_name}-${data.consul_keys.ck.var.env}-${var.install_name}-backend" | |
description = "Serverless application for SLM" | |
} | |
# -------------------------------------------------------------- | |
# Step 2 : Create APIGateway Ressource | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_resource" "proxy_resource" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
parent_id = "${aws_api_gateway_rest_api.apigateway.root_resource_id}" | |
path_part = "{proxy+}" | |
} | |
# -------------------------------------------------------------- | |
# Step 3 : Create Method Request | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_method" "proxy_method" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_resource.proxy_resource.id}" | |
http_method = "ANY" | |
authorization = "NONE" | |
} | |
# -------------------------------------------------------------- | |
# Step 4 : Activation du CORS (Method Response) | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_method_response" "cors_method_response_200" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_resource.proxy_resource.id}" | |
http_method = "${aws_api_gateway_method.proxy_method.http_method}" | |
status_code = "200" | |
response_models { | |
"application/json" = "Empty" | |
} | |
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 | |
} | |
depends_on = ["aws_api_gateway_method.proxy_root"] | |
} | |
# -------------------------------------------------------------- | |
# Step 5: Route API Gateway Request to Lambda (Create Integration Request) | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_integration" "sendlambda" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_method.proxy_method.resource_id}" | |
http_method = "${aws_api_gateway_method.proxy_method.http_method}" | |
type = "AWS_PROXY" | |
integration_http_method = "ANY" | |
uri = "${aws_lambda_function.backend_lambda.invoke_arn}" | |
} | |
# -------------------------------------------------------------- | |
# Step 6 : Activation du CORS (Integration Response) | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_integration_response" "cors_integ_response" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_resource.proxy_resource.id}" | |
http_method = "${aws_api_gateway_method.proxy_method.http_method}" | |
status_code = "${aws_api_gateway_method_response.cors_method_response_200.status_code}" | |
response_parameters = { | |
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'", | |
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'", | |
"method.response.header.Access-Control-Allow-Origin" = "'*'" | |
} | |
} | |
#---------------------------------------------------------------- | |
# -------------------------------------------------------------- | |
# Step 1-6 For for root path (/) | |
# --------------------------------------------------------------- | |
#---------------------------------------------------------------- | |
resource "aws_api_gateway_method" "proxy_root" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_rest_api.apigateway.root_resource_id}" | |
http_method = "ANY" | |
authorization = "NONE" | |
} | |
# Enable CORS | |
resource "aws_api_gateway_method_response" "cors_root_method_200" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_rest_api.apigateway.root_resource_id}" | |
http_method = "${aws_api_gateway_method.proxy_root.http_method}" | |
status_code = "200" | |
response_models { | |
"application/json" = "Empty" | |
} | |
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 | |
} | |
} | |
resource "aws_api_gateway_integration" "sendlambda_root" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_method.proxy_root.resource_id}" | |
http_method = "${aws_api_gateway_method.proxy_root.http_method}" | |
integration_http_method = "ANY" | |
type = "AWS" | |
uri = "${aws_lambda_function.backend_lambda.invoke_arn}" | |
} | |
# Enable CORS | |
resource "aws_api_gateway_integration_response" "cors_root_response" { | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
resource_id = "${aws_api_gateway_rest_api.apigateway.root_resource_id}" | |
http_method = "${aws_api_gateway_method.proxy_method.http_method}" | |
status_code = "${aws_api_gateway_method_response.cors_method_response_200.status_code}" | |
response_parameters = { | |
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'", | |
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'", | |
"method.response.header.Access-Control-Allow-Origin" = "'*'" | |
} | |
} | |
# -------------------------------------------------------------- | |
# Step 7 : Deploy and Activate Configuration | |
# --------------------------------------------------------------- | |
resource "aws_api_gateway_deployment" "deployApI" { | |
depends_on = [ | |
"aws_api_gateway_integration.sendlambda", | |
"aws_api_gateway_integration.sendlambda_root" | |
] | |
rest_api_id = "${aws_api_gateway_rest_api.apigateway.id}" | |
stage_name = "${data.consul_keys.ck.var.project_name}-${data.consul_keys.ck.var.env}-${var.install_name}-api" | |
} | |
# -------------------------------------------------------------- | |
# Step 8 : Allow API Gateway to Access Lambda | |
# --------------------------------------------------------------- | |
resource "aws_lambda_permission" "apigw_access" { | |
statement_id = "AllowAPIGatewayInvoke" | |
action = "lambda:InvokeFunction" | |
function_name = "${aws_lambda_function.backend_lambda.arn}" | |
principal = "apigateway.amazonaws.com" | |
source_arn = "${aws_api_gateway_deployment.deployApI.execution_arn}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment