Commit 0c73bdff authored by Bertrand PINEL's avatar Bertrand PINEL
Browse files

Update terraform script to include S3 lambda code bucket and to correct some strage behaviour...

parent 3d7522d3
...@@ -26,7 +26,48 @@ resource "aws_api_gateway_method" "typePathGet" { ...@@ -26,7 +26,48 @@ resource "aws_api_gateway_method" "typePathGet" {
http_method = "GET" http_method = "GET"
authorization = "NONE" authorization = "NONE"
} }
# OPTIONS on path /{type} for supporting CORS
resource "aws_api_gateway_method" "typePathOptions" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typePath.id}"
http_method = "OPTIONS"
authorization = "NONE"
}
resource "aws_api_gateway_method_response" "typeOptions200" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typePath.id}"
http_method = "${aws_api_gateway_method.typePathOptions.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" "typeOptionsIntegration" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typePath.id}"
http_method = "${aws_api_gateway_method.typePathOptions.http_method}"
type = "MOCK"
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "{\"statusCode\": 200}"
}
}
resource "aws_api_gateway_integration_response" "typeOptionsIntegrationResponse" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typePath.id}"
http_method = "${aws_api_gateway_method.typePathOptions.http_method}"
status_code = "${aws_api_gateway_method_response.typeOptions200.status_code}"
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,Access-Control-Allow-Origin,X-Amz-Date,Authorization,X-Requested-With,X-Requested-By,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST'",
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
}
# POST on path /{type} # POST on path /{type}
resource "aws_api_gateway_method" "typePathPost" { resource "aws_api_gateway_method" "typePathPost" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -42,7 +83,48 @@ resource "aws_api_gateway_method" "typeIdPathGet" { ...@@ -42,7 +83,48 @@ resource "aws_api_gateway_method" "typeIdPathGet" {
http_method = "GET" http_method = "GET"
authorization = "NONE" authorization = "NONE"
} }
# OPTIONS on path /{type}/{id}
resource "aws_api_gateway_method" "typeIdPathOptions" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typeIdPath.id}"
http_method = "OPTIONS"
authorization = "NONE"
}
resource "aws_api_gateway_method_response" "typeIdOptions200" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typeIdPath.id}"
http_method = "${aws_api_gateway_method.typeIdPathOptions.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" "typeIdOptionsIntegration" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typeIdPath.id}"
http_method = "${aws_api_gateway_method.typeIdPathOptions.http_method}"
type = "MOCK"
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "{\"statusCode\": 200}"
}
}
resource "aws_api_gateway_integration_response" "typeIdOptionsIntegrationResponse" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typeIdPath.id}"
http_method = "${aws_api_gateway_method.typeIdPathOptions.http_method}"
status_code = "${aws_api_gateway_method_response.typeIdOptions200.status_code}"
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,Access-Control-Allow-Origin,X-Amz-Date,Authorization,X-Requested-With,X-Requested-By,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,DELETE,PATCH'",
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
}
# DELETE on path /{type}/{id} # DELETE on path /{type}/{id}
resource "aws_api_gateway_method" "typeIdPathDelete" { resource "aws_api_gateway_method" "typeIdPathDelete" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -59,6 +141,8 @@ resource "aws_api_gateway_method" "typeIdPathPatch" { ...@@ -59,6 +141,8 @@ resource "aws_api_gateway_method" "typeIdPathPatch" {
authorization = "NONE" authorization = "NONE"
} }
##############################################
# GET on {type}
# Setup Integration Request for GET on {type} # Setup Integration Request for GET on {type}
resource "aws_api_gateway_integration" "lambdaJsonTypeGet" { resource "aws_api_gateway_integration" "lambdaJsonTypeGet" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -74,77 +158,15 @@ resource "aws_api_gateway_integration" "lambdaJsonTypeGet" { ...@@ -74,77 +158,15 @@ resource "aws_api_gateway_integration" "lambdaJsonTypeGet" {
"application/json" = "${file("template.vm")}" "application/json" = "${file("template.vm")}"
} }
} }
# Setup Integration Request for POST on {type}
resource "aws_api_gateway_integration" "lambdaJsonTypePost" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typePathPost.resource_id}"
http_method = "${aws_api_gateway_method.typePathPost.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Request for GET on {type}/{id}
resource "aws_api_gateway_integration" "lambdaJsonTypeIdGet" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typeIdPathGet.resource_id}"
http_method = "${aws_api_gateway_method.typeIdPathGet.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Request for DELETE on {type}/{id}
resource "aws_api_gateway_integration" "lambdaJsonTypeIdDelete" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typeIdPathDelete.resource_id}"
http_method = "${aws_api_gateway_method.typeIdPathDelete.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Request for PATCH on {type}/{id}
resource "aws_api_gateway_integration" "lambdaJsonTypeIdPatch" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typeIdPathPatch.resource_id}"
http_method = "${aws_api_gateway_method.typeIdPathPatch.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Response for status code 200 and GET on {type} # Setup Integration Response for status code 200 and GET on {type}
resource "aws_api_gateway_method_response" "200TypeGet" { resource "aws_api_gateway_method_response" "200TypeGet" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typePathGet.resource_id}" resource_id = "${aws_api_gateway_method.typePathGet.resource_id}"
http_method = "${aws_api_gateway_method.typePathGet.http_method}" http_method = "${aws_api_gateway_method.typePathGet.http_method}"
status_code = "200" status_code = "200"
response_parameters {
"method.response.header.Access-Control-Allow-Origin" = true
}
} }
resource "aws_api_gateway_integration_response" "200TypeGetIntegrationResponse" { resource "aws_api_gateway_integration_response" "200TypeGetIntegrationResponse" {
...@@ -155,8 +177,28 @@ resource "aws_api_gateway_integration_response" "200TypeGetIntegrationResponse" ...@@ -155,8 +177,28 @@ resource "aws_api_gateway_integration_response" "200TypeGetIntegrationResponse"
response_templates = { response_templates = {
"application/json" = "" "application/json" = ""
} }
response_parameters {
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
depends_on = ["aws_api_gateway_integration.lambdaJsonTypeGet"]
} }
##############################################
# POST on {type}
# Setup Integration Request for POST on {type}
resource "aws_api_gateway_integration" "lambdaJsonTypePost" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typePathPost.resource_id}"
http_method = "${aws_api_gateway_method.typePathPost.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Response for status code 200 and POST on {type} # Setup Integration Response for status code 200 and POST on {type}
resource "aws_api_gateway_method_response" "200TypePost" { resource "aws_api_gateway_method_response" "200TypePost" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -173,8 +215,25 @@ resource "aws_api_gateway_integration_response" "200TypePostIntegrationResponse" ...@@ -173,8 +215,25 @@ resource "aws_api_gateway_integration_response" "200TypePostIntegrationResponse"
response_templates = { response_templates = {
"application/json" = "" "application/json" = ""
} }
depends_on = ["aws_api_gateway_integration.lambdaJsonTypePost"]
} }
##############################################
# GET on {type}/{id}
# Setup Integration Request for GET on {type}/{id}
resource "aws_api_gateway_integration" "lambdaJsonTypeIdGet" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typeIdPathGet.resource_id}"
http_method = "${aws_api_gateway_method.typeIdPathGet.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Response for status code 200 and GET on {type}/{id} # Setup Integration Response for status code 200 and GET on {type}/{id}
resource "aws_api_gateway_method_response" "200TypeIdGet" { resource "aws_api_gateway_method_response" "200TypeIdGet" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -190,9 +249,26 @@ resource "aws_api_gateway_integration_response" "200TypeIdGetIntegrationResponse ...@@ -190,9 +249,26 @@ resource "aws_api_gateway_integration_response" "200TypeIdGetIntegrationResponse
status_code = "${aws_api_gateway_method_response.200TypeIdGet.status_code}" status_code = "${aws_api_gateway_method_response.200TypeIdGet.status_code}"
response_templates = { response_templates = {
"application/json" = "" "application/json" = ""
} }
depends_on=["aws_api_gateway_integration.lambdaJsonTypeIdGet"]
} }
##############################################
# DELETE on {type}/{id}
# Setup Integration Request for DELETE on {type}/{id}
resource "aws_api_gateway_integration" "lambdaJsonTypeIdDelete" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typeIdPathDelete.resource_id}"
http_method = "${aws_api_gateway_method.typeIdPathDelete.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Response for status code 200 and DELETE on {type}/{id} # Setup Integration Response for status code 200 and DELETE on {type}/{id}
resource "aws_api_gateway_method_response" "200TypeIdDelete" { resource "aws_api_gateway_method_response" "200TypeIdDelete" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -209,8 +285,25 @@ resource "aws_api_gateway_integration_response" "200TypeIdDeleteIntegrationRespo ...@@ -209,8 +285,25 @@ resource "aws_api_gateway_integration_response" "200TypeIdDeleteIntegrationRespo
response_templates = { response_templates = {
"application/json" = "" "application/json" = ""
} }
depends_on= ["aws_api_gateway_integration.lambdaJsonTypeIdDelete"]
} }
##############################################
# PATCH on {type}/{id}
# Setup Integration Request for PATCH on {type}/{id}
resource "aws_api_gateway_integration" "lambdaJsonTypeIdPatch" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_method.typeIdPathPatch.resource_id}"
http_method = "${aws_api_gateway_method.typeIdPathPatch.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda-jsonapi.invoke_arn}"
# Transforms the incoming XML request to JSON
passthrough_behavior = "WHEN_NO_TEMPLATES"
request_templates {
"application/json" = "${file("template.vm")}"
}
}
# Setup Integration Response for status code 200 and PATCH on {type}/{id} # Setup Integration Response for status code 200 and PATCH on {type}/{id}
resource "aws_api_gateway_method_response" "200TypeIdPatch" { resource "aws_api_gateway_method_response" "200TypeIdPatch" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
...@@ -218,7 +311,6 @@ resource "aws_api_gateway_method_response" "200TypeIdPatch" { ...@@ -218,7 +311,6 @@ resource "aws_api_gateway_method_response" "200TypeIdPatch" {
http_method = "${aws_api_gateway_method.typeIdPathPatch.http_method}" http_method = "${aws_api_gateway_method.typeIdPathPatch.http_method}"
status_code = "200" status_code = "200"
} }
resource "aws_api_gateway_integration_response" "200TypeIdPatchIntegrationResponse" { resource "aws_api_gateway_integration_response" "200TypeIdPatchIntegrationResponse" {
rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}" rest_api_id = "${aws_api_gateway_rest_api.jsonapi.id}"
resource_id = "${aws_api_gateway_resource.typeIdPath.id}" resource_id = "${aws_api_gateway_resource.typeIdPath.id}"
...@@ -227,12 +319,15 @@ resource "aws_api_gateway_integration_response" "200TypeIdPatchIntegrationRespon ...@@ -227,12 +319,15 @@ resource "aws_api_gateway_integration_response" "200TypeIdPatchIntegrationRespon
response_templates = { response_templates = {
"application/json" = "" "application/json" = ""
} }
depends_on = ["aws_api_gateway_integration.lambdaJsonTypeIdPatch"]
} }
resource "aws_api_gateway_deployment" "jsonapiDeployment" { resource "aws_api_gateway_deployment" "jsonapiDeployment" {
depends_on = [ depends_on = [
"aws_api_gateway_integration.typeOptionsIntegration",
"aws_api_gateway_integration.lambdaJsonTypeGet", "aws_api_gateway_integration.lambdaJsonTypeGet",
"aws_api_gateway_integration.lambdaJsonTypePost", "aws_api_gateway_integration.lambdaJsonTypePost",
"aws_api_gateway_integration.typeIdOptionsIntegration",
"aws_api_gateway_integration.lambdaJsonTypeIdGet", "aws_api_gateway_integration.lambdaJsonTypeIdGet",
"aws_api_gateway_integration.lambdaJsonTypeIdPatch", "aws_api_gateway_integration.lambdaJsonTypeIdPatch",
"aws_api_gateway_integration.lambdaJsonTypeIdDelete" "aws_api_gateway_integration.lambdaJsonTypeIdDelete"
......
...@@ -7,6 +7,17 @@ variable "bucket_name_staging" { ...@@ -7,6 +7,17 @@ variable "bucket_name_staging" {
default = "ember-aws-ehipster-staging" default = "ember-aws-ehipster-staging"
} }
resource "aws_s3_bucket" "lambda-bucket" {
bucket = "lambda-jsonapi-code-bucket"
acl = "public-read"
}
resource "aws_s3_bucket_object" "lambda-bucket-code" {
bucket = "${aws_s3_bucket.lambda-bucket.bucket}"
key = "v1.0.0/lambda-jsonapi.zip"
source = "../lambda/lambda-jsonapi.zip"
etag = "${md5(file("../lambda/lambda-jsonapi.zip"))}"
}
resource "aws_s3_bucket" "production" { resource "aws_s3_bucket" "production" {
bucket = "${var.bucket_name_production}" bucket = "${var.bucket_name_production}"
acl = "public-read" acl = "public-read"
......
...@@ -10,8 +10,8 @@ resource "aws_lambda_function" "lambda-jsonapi" { ...@@ -10,8 +10,8 @@ resource "aws_lambda_function" "lambda-jsonapi" {
function_name = "lambda-jsonapi" function_name = "lambda-jsonapi"
# The bucket name as created before running terraform scripts with "aws s3api create-bucket" # The bucket name as created before running terraform scripts with "aws s3api create-bucket"
s3_bucket = "lambda-jsonapi-code-bucket" s3_bucket = "${aws_s3_bucket.lambda-bucket.bucket}"
s3_key = "v1.0.0/lambda-jsonapi.zip" s3_key = "${aws_s3_bucket_object.lambda-bucket-code.key}"
# "main" is the filename within the zip file (main.js) and "handler" # "main" is the filename within the zip file (main.js) and "handler"
# is the name of the property under which the handler function was # is the name of the property under which the handler function was
...@@ -20,6 +20,8 @@ resource "aws_lambda_function" "lambda-jsonapi" { ...@@ -20,6 +20,8 @@ resource "aws_lambda_function" "lambda-jsonapi" {
runtime = "nodejs8.10" runtime = "nodejs8.10"
role = "${aws_iam_role.lambda_jsonapi.arn}" role = "${aws_iam_role.lambda_jsonapi.arn}"
depends_on = ["aws_s3_bucket.lambda-bucket"]
} }
...@@ -33,4 +35,6 @@ resource "aws_lambda_permission" "apigw" { ...@@ -33,4 +35,6 @@ resource "aws_lambda_permission" "apigw" {
# The /*/* portion grants access from any method on any resource # The /*/* portion grants access from any method on any resource
# within the API Gateway "REST API". # within the API Gateway "REST API".
source_arn = "${aws_api_gateway_deployment.jsonapiDeployment.execution_arn}/*/*" source_arn = "${aws_api_gateway_deployment.jsonapiDeployment.execution_arn}/*/*"
depends_on = ["aws_iam_role.lambda_jsonapi"]
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment