|
| 1 | + |
| 2 | +variable "api_name" { |
| 3 | + description = "name of the api" |
| 4 | + default = "api" |
| 5 | +} |
| 6 | + |
| 7 | +variable "api_path" { |
| 8 | + description = "path of the api" |
| 9 | + default = "api" |
| 10 | +} |
| 11 | + |
| 12 | +variable "lambda_arn" { |
| 13 | + description = "arn of the associated lambda function" |
| 14 | +} |
| 15 | + |
| 16 | +variable "region" { |
| 17 | + description = "region" |
| 18 | +} |
| 19 | +variable "account_id" { |
| 20 | + description = "account id" |
| 21 | +} |
| 22 | + |
| 23 | +variable "deploy_stage" { |
| 24 | + description = "stage name for deployment" |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +# API Gateway |
| 29 | +resource "aws_api_gateway_rest_api" "api" { |
| 30 | + name = "${var.api_name}" |
| 31 | +} |
| 32 | + |
| 33 | +resource "aws_api_gateway_resource" "resource" { |
| 34 | + path_part = "${var.api_path}" |
| 35 | + parent_id = "${aws_api_gateway_rest_api.api.root_resource_id}" |
| 36 | + rest_api_id = "${aws_api_gateway_rest_api.api.id}" |
| 37 | +} |
| 38 | + |
| 39 | +resource "aws_api_gateway_method" "method" { |
| 40 | + rest_api_id = "${aws_api_gateway_rest_api.api.id}" |
| 41 | + resource_id = "${aws_api_gateway_resource.resource.id}" |
| 42 | + http_method = "ANY" |
| 43 | + authorization = "NONE" |
| 44 | +} |
| 45 | + |
| 46 | +resource "aws_api_gateway_integration" "integration" { |
| 47 | + rest_api_id = "${aws_api_gateway_rest_api.api.id}" |
| 48 | + resource_id = "${aws_api_gateway_resource.resource.id}" |
| 49 | + http_method = "${aws_api_gateway_method.method.http_method}" |
| 50 | + uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/${var.lambda_arn}/invocations" |
| 51 | + # lambda can be invoked by POST only |
| 52 | + integration_http_method = "POST" |
| 53 | + # type is AWS_PROXY as we are going to integrate with an express lambda function. |
| 54 | + type = "AWS_PROXY" |
| 55 | +} |
| 56 | +# https://docs.aws.amazon.com/apigateway/latest/developerguide/stage-variables.html?icmpid=docs_apigateway_console |
| 57 | +resource "aws_api_gateway_deployment" "deployment" { |
| 58 | + depends_on = ["aws_api_gateway_method.method","aws_api_gateway_integration.integration"] |
| 59 | + rest_api_id = "${aws_api_gateway_rest_api.api.id}" |
| 60 | + stage_name = "${var.deploy_stage}" |
| 61 | +} |
| 62 | +output "url" { |
| 63 | + value = "${aws_api_gateway_deployment.deployment.invoke_url}" |
| 64 | +} |
| 65 | + |
| 66 | +# Lambda |
| 67 | +resource "aws_lambda_permission" "apigw_lambda" { |
| 68 | + statement_id = "AllowExecutionFromAPIGateway" |
| 69 | + action = "lambda:InvokeFunction" |
| 70 | + function_name = "${var.lambda_arn}" |
| 71 | + principal = "apigateway.amazonaws.com" |
| 72 | + # More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html |
| 73 | + # source_arn = "arn:aws:execute-api:${var.region}:${var.account_id}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method.http_method}${aws_api_gateway_resource.resource.path}" |
| 74 | + # arn:aws:execute-api:region:account-id:api-id/stage/METHOD_HTTP_VERB/Resource-path |
| 75 | + source_arn = "arn:aws:execute-api:${var.region}:${var.account_id}:${aws_api_gateway_rest_api.api.id}/*/*/*" |
| 76 | +} |
0 commit comments