├── README.md ├── main.tf └── variables.tf /README.md: -------------------------------------------------------------------------------- 1 | # Terraform API Gateway Method Module 2 | 3 | Terraform module for creating a serverless architecture in API Gateway. This module can be used to resource methods attached to your API Gateway resources to call lambda functions with a few variables exposed see [variables.tf](https://github.com/carrot//terraform-api-gateway-method-module/blob/master/variables.tf). 4 | 5 | ## Caveats 6 | 7 | This module makes a few assumptions for simplicity: 8 | 9 | 1. You are resourcing API gateway to be used to access Lambda functions as part of a serverless API. 10 | 2. Your requests/responses are all in JSON 11 | 3. You only require one error response template across your entire API Gateway instance. 12 | 13 | ## Example Useage 14 | ``` 15 | 16 | # Create a resource 17 | 18 | resource "aws_api_gateway_resource" "users" { 19 | rest_api_id = "${aws_api_gateway_rest_api..id}" 20 | parent_id = "${aws_api_gateway_rest_api..root_resource_id}" 21 | path_part = "users" 22 | } 23 | 24 | # Call the module to attach a method along with its request/response/integration templates 25 | # This one creates a user. 26 | 27 | module "UsersPost" { 28 | source = "github.com/carrot/terraform-api-gateway-method-module" 29 | rest_api_id = "${aws_api_gateway_rest_api..id}" 30 | resource_id = "${aws_api_gateway_resource.users.id}" 31 | http_method = "POST" 32 | lambda_name = "create_user_lambda_funciton" 33 | account_id = "1234567890" 34 | region = "us-east-1" 35 | integration_request_template = "#set($inputRoot = $input.path('$')){}" 36 | request_model = "Empty" 37 | integration_response_template = "#set($inputRoot = $input.path('$')){}" 38 | response_model = "Empty" 39 | integration_error_template = "#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')) {\"message\" :\"$errorMessageObj.message\"}" 40 | authorization = "AWS_IAM" 41 | } 42 | ``` 43 | 44 | Need CORS enabled? check out [https://github.com/carrot/terraform-api-gateway-cors-module](https://github.com/carrot/terraform-api-gateway-cors-module) 45 | 46 | ## More info 47 | [Terraform - API Gateway](https://www.terraform.io/docs/providers/aws/r/api_gateway_rest_api.html) 48 | 49 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | resource "aws_api_gateway_method" "ResourceMethod" { 2 | rest_api_id = "${var.rest_api_id}" 3 | resource_id = "${var.resource_id}" 4 | http_method = "${var.http_method}" 5 | authorization = "${var.authorization}" 6 | request_parameters = "${var.request_parameters}" 7 | request_models = { "application/json" = "${var.request_model}" } 8 | } 9 | 10 | resource "aws_api_gateway_integration" "ResourceMethodIntegration" { 11 | rest_api_id = "${var.rest_api_id}" 12 | resource_id = "${var.resource_id}" 13 | http_method = "${aws_api_gateway_method.ResourceMethod.http_method}" 14 | type = "AWS" 15 | uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${var.account_id}:function:${var.lambda_name}/invocations" 16 | integration_http_method = "POST" 17 | request_templates = { "application/json" = "${var.integration_request_template}" } 18 | } 19 | 20 | resource "aws_api_gateway_integration_response" "ResourceMethodIntegration200" { 21 | rest_api_id = "${var.rest_api_id}" 22 | resource_id = "${var.resource_id}" 23 | http_method = "${aws_api_gateway_method.ResourceMethod.http_method}" 24 | status_code = "${aws_api_gateway_method_response.ResourceMethod200.status_code}" 25 | response_parameters = { 26 | "method.response.header.Access-Control-Allow-Origin" = "'*'" 27 | } 28 | response_templates = { "application/json" = "${var.integration_response_template}" } 29 | } 30 | 31 | resource "aws_api_gateway_integration_response" "ResourceMethodIntegration400" { 32 | rest_api_id = "${var.rest_api_id}" 33 | resource_id = "${var.resource_id}" 34 | http_method = "${aws_api_gateway_method.ResourceMethod.http_method}" 35 | status_code = "${aws_api_gateway_method_response.ResourceMethod400.status_code}" 36 | response_templates = { 37 | "application/json" = "${var.integration_error_template}" 38 | } 39 | response_parameters = { "method.response.header.Access-Control-Allow-Origin" = "'*'" } 40 | } 41 | 42 | resource "aws_api_gateway_method_response" "ResourceMethod200" { 43 | rest_api_id = "${var.rest_api_id}" 44 | resource_id = "${var.resource_id}" 45 | http_method = "${aws_api_gateway_method.ResourceMethod.http_method}" 46 | status_code = "200" 47 | response_models = { "application/json" = "${var.response_model}" } 48 | response_parameters = { "method.response.header.Access-Control-Allow-Origin" = "*" } 49 | } 50 | 51 | resource "aws_api_gateway_method_response" "ResourceMethod400" { 52 | rest_api_id = "${var.rest_api_id}" 53 | resource_id = "${var.resource_id}" 54 | http_method = "${aws_api_gateway_method.ResourceMethod.http_method}" 55 | status_code = "400" 56 | response_models = { "application/json" = "Error" } 57 | response_parameters = { "method.response.header.Access-Control-Allow-Origin" = "*" } 58 | } 59 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | # 2 | # API Gateway ID (required) 3 | # e.g., ${var.aws_api_gateway_rest_api..id} 4 | # 5 | 6 | variable "rest_api_id" {} 7 | 8 | # 9 | # Resource ID (required) 10 | # e.g., ${var.aws_api_gateway_resource..id} 11 | # 12 | 13 | variable "resource_id" {} 14 | 15 | # 16 | # HTTP Method ID (required) 17 | # e.g., ${var.aws_api_gateway_method..id} 18 | # 19 | 20 | variable "http_method" {} 21 | 22 | # 23 | # Name of Lambda function (required) 24 | # Point to name of a lambda function attached to your account and region 25 | # e.g., 'my_awesome_lambda_func' 26 | # 27 | 28 | variable "lambda_name" {} 29 | 30 | # 31 | # Account ID (required) 32 | # ID of AWS account e.g., 99999999999 33 | # 34 | 35 | variable "account_id" {} 36 | 37 | # 38 | # Region (required) 39 | # e.g., us-east-1 40 | # 41 | 42 | variable "region" {} 43 | 44 | # 45 | # Velocity template used to capture params from request and send to lambda (optional) 46 | # more info: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html 47 | # 48 | 49 | variable "integration_request_template" { 50 | default = "{}" 51 | } 52 | 53 | # 54 | # Velocity template used to capture params sent to response from lambda (optional) 55 | # more info: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html 56 | # 57 | 58 | variable "integration_response_template" { 59 | default = "#set($inputRoot = $input.path('$')){}" 60 | } 61 | 62 | # 63 | # Request Parameters 64 | # A map of request query string parameters and headers that should be passed to the integration. 65 | # 66 | 67 | variable "request_parameters" { 68 | default = {} 69 | } 70 | 71 | # 72 | # Method Request model (optional) 73 | # Name of model used for method request. 74 | # Reference the model by name e.g., `Empty`, `Error` or create a custom model and reference that by name 75 | # 76 | 77 | variable "request_model" { 78 | default = "Empty" 79 | } 80 | 81 | # 82 | # Method Response model (optional) 83 | # Name of model used for method Response. 84 | # Reference the model by name e.g., `Empty`, `Error` or create a custom model and reference that by name 85 | # 86 | 87 | variable "response_model" { 88 | default = "Empty" 89 | } 90 | 91 | # 92 | # Error Template (optional) 93 | # Velocity template used to deliver errors to response 94 | # Assumes all responses uses the same error template. 95 | # 96 | 97 | variable "integration_error_template" { 98 | default = <