|
1 | | -resource "aws_api_gateway_rest_api" "example" { |
2 | | - name = "ServerlessExample" |
3 | | - description = "Terraform Serverless Application Example" |
| 1 | +resource "aws_api_gateway_rest_api" "lambda-api" { |
| 2 | + name = "deckdeckgo-handler-rest-api" |
4 | 3 | } |
5 | 4 |
|
6 | 5 | resource "aws_api_gateway_resource" "proxy" { |
7 | | - rest_api_id = "${aws_api_gateway_rest_api.example.id}" |
8 | | - parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}" |
| 6 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
| 7 | + parent_id = "${aws_api_gateway_rest_api.lambda-api.root_resource_id}" |
9 | 8 | path_part = "{proxy+}" |
10 | 9 | } |
11 | 10 |
|
12 | 11 | resource "aws_api_gateway_method" "proxy" { |
13 | | - rest_api_id = "${aws_api_gateway_rest_api.example.id}" |
| 12 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
14 | 13 | resource_id = "${aws_api_gateway_resource.proxy.id}" |
15 | 14 | http_method = "ANY" |
16 | 15 | authorization = "NONE" |
17 | 16 | } |
18 | 17 |
|
19 | | -resource "aws_api_gateway_integration" "lambda" { |
20 | | - rest_api_id = "${aws_api_gateway_rest_api.example.id}" |
| 18 | +resource "aws_api_gateway_integration" "lambda-api" { |
| 19 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
21 | 20 | resource_id = "${aws_api_gateway_method.proxy.resource_id}" |
22 | 21 | http_method = "${aws_api_gateway_method.proxy.http_method}" |
23 | 22 |
|
24 | 23 | integration_http_method = "POST" |
25 | 24 | type = "AWS_PROXY" |
26 | | - uri = "${aws_lambda_function.example.invoke_arn}" |
| 25 | + uri = "${aws_lambda_function.api.invoke_arn}" |
27 | 26 | } |
28 | 27 |
|
29 | | -resource "aws_api_gateway_method" "proxy_root" { |
30 | | - rest_api_id = "${aws_api_gateway_rest_api.example.id}" |
31 | | - resource_id = "${aws_api_gateway_rest_api.example.root_resource_id}" |
32 | | - http_method = "ANY" |
| 28 | +resource "aws_api_gateway_deployment" "lambda-api" { |
| 29 | + depends_on = [ |
| 30 | + "aws_api_gateway_integration.lambda-api", |
| 31 | + ] |
| 32 | + |
| 33 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
| 34 | + stage_name = "beta" |
| 35 | +} |
| 36 | + |
| 37 | +resource "aws_lambda_permission" "lambda_permission" { |
| 38 | + action = "lambda:InvokeFunction" |
| 39 | + function_name = "${aws_lambda_function.api.function_name}" |
| 40 | + principal = "apigateway.amazonaws.com" |
| 41 | + source_arn = "${aws_api_gateway_rest_api.lambda-api.execution_arn}/*/*/*" |
| 42 | + |
| 43 | + depends_on = [ |
| 44 | + "aws_lambda_function.api", |
| 45 | + ] |
| 46 | +} |
| 47 | + |
| 48 | +############### |
| 49 | +# Enable CORS # |
| 50 | +############### |
| 51 | +# https://medium.com/@MrPonath/terraform-and-aws-api-gateway-a137ee48a8ac |
| 52 | +resource "aws_api_gateway_method" "options_method" { |
| 53 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
| 54 | + resource_id = "${aws_api_gateway_resource.proxy.id}" |
| 55 | + http_method = "OPTIONS" |
33 | 56 | authorization = "NONE" |
34 | 57 | } |
35 | 58 |
|
36 | | -resource "aws_api_gateway_integration" "lambda_root" { |
37 | | - rest_api_id = "${aws_api_gateway_rest_api.example.id}" |
38 | | - resource_id = "${aws_api_gateway_method.proxy_root.resource_id}" |
39 | | - http_method = "${aws_api_gateway_method.proxy_root.http_method}" |
| 59 | +resource "aws_api_gateway_method_response" "options_200" { |
| 60 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
| 61 | + resource_id = "${aws_api_gateway_resource.proxy.id}" |
| 62 | + http_method = "${aws_api_gateway_method.options_method.http_method}" |
| 63 | + status_code = 200 |
40 | 64 |
|
41 | | - integration_http_method = "POST" |
42 | | - type = "AWS_PROXY" |
43 | | - uri = "${aws_lambda_function.example.invoke_arn}" |
| 65 | + response_models { |
| 66 | + "application/json" = "Empty" |
| 67 | + } |
| 68 | + |
| 69 | + response_parameters { |
| 70 | + "method.response.header.Access-Control-Allow-Headers" = true |
| 71 | + "method.response.header.Access-Control-Allow-Methods" = true |
| 72 | + "method.response.header.Access-Control-Allow-Origin" = true |
| 73 | + } |
44 | 74 | } |
45 | 75 |
|
46 | | -resource "aws_api_gateway_deployment" "example" { |
47 | | - depends_on = [ |
48 | | - "aws_api_gateway_integration.lambda", |
49 | | - "aws_api_gateway_integration.lambda_root", |
50 | | - ] |
| 76 | +resource "aws_api_gateway_integration" "options_integration" { |
| 77 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
| 78 | + resource_id = "${aws_api_gateway_resource.proxy.id}" |
| 79 | + http_method = "${aws_api_gateway_method.options_method.http_method}" |
| 80 | + type = "MOCK" |
51 | 81 |
|
52 | | - rest_api_id = "${aws_api_gateway_rest_api.example.id}" |
53 | | - stage_name = "test" |
| 82 | + # https://stackoverflow.com/questions/43990464/api-gateway-mock-integration-fails-with-500/44013347#44013347 |
| 83 | + request_templates { |
| 84 | + "application/json" = <<EOF |
| 85 | +{ |
| 86 | + "statusCode": 200 |
54 | 87 | } |
| 88 | +EOF |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +resource "aws_api_gateway_integration_response" "options_integration_response" { |
| 93 | + rest_api_id = "${aws_api_gateway_rest_api.lambda-api.id}" |
| 94 | + resource_id = "${aws_api_gateway_resource.proxy.id}" |
| 95 | + http_method = "${aws_api_gateway_method.options_method.http_method}" |
| 96 | + status_code = "${aws_api_gateway_method_response.options_200.status_code}" |
55 | 97 |
|
| 98 | + response_parameters = { |
| 99 | + "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" |
| 100 | + "method.response.header.Access-Control-Allow-Methods" = "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'" |
| 101 | + "method.response.header.Access-Control-Allow-Origin" = "'*'" |
| 102 | + } |
| 103 | +} |
56 | 104 |
|
57 | 105 | output "base_url" { |
58 | | - value = "${aws_api_gateway_deployment.example.invoke_url}" |
| 106 | + |
| 107 | + value = "${aws_api_gateway_deployment.lambda-api.invoke_url}" |
| 108 | + |
59 | 109 | } |
0 commit comments