|
1 | | -# Library project layout |
2 | | -This is the boilerplate for a library package |
| 1 | +# SkyLeague `aws-rest-api` - AWS API Gateway REST API simplified |
| 2 | + |
| 3 | +This module simplifies the deployment of AWS API Gateway REST API (v1) by consolidating all the neccessary infrastructure into this module. It leverages the capability of the REST API to deploy using the [`body`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_rest_api#body) argument, rather than deploying all the resources/methods/integrations separately. This makes for a very dynamic deployment without the hassle of maintaining the sub-resource <-> parent relations between all the path parts (for examples, see the [`aws_api_gateway_integration`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_integration) docs of Terraform). |
| 4 | + |
| 5 | +In addition, it simplifies the integration of AWS Lambda by providing a standardized syntax to integrate AWS Lambda using the `AWS_PROXY` integration, as well as creating all the neccesary permissions for the API to invoke the Lambda functionss that are integrated with it. |
| 6 | + |
| 7 | +## Dependencies |
| 8 | + |
| 9 | +In order to deploy this Terraform module, you need a working `node` installation available during the deployment, with accompanying `npx` executable (usually present when `node` is installed). `node` is used in order to dynamically generate the OpenAPI `body` that defines the integrations with the REST API. The `ajv` package is a required dependency for validating the input of the generation script. This can be installed in the project `node_modules` (it likely is if you're using Javascript/Typescript in your toolchain), or as a global `npm` package. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```terraform |
| 14 | +module "api" { |
| 15 | + source = "git@github.com:skyleague/aws-rest-api.git?ref=v1.0.0" |
| 16 | +
|
| 17 | + name = "my-awesome-api" |
| 18 | +
|
| 19 | + definition = jsonencode({ |
| 20 | + "/v1/hello-world" = { |
| 21 | + "GET" = { |
| 22 | + # This also supports the aws_lambda_alias type |
| 23 | + lambda = aws_lambda_function.hello_world |
| 24 | + } |
| 25 | + } |
| 26 | + }) |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Options |
| 31 | + |
| 32 | +For a complete reference of all variables, have a look at the descriptions in [`variables.tf`](./variables.tf). |
| 33 | + |
| 34 | +## Advanced options |
| 35 | + |
| 36 | +Besides the `definition`, the module allows you to pass an `extensions` argument. This argument will get augmented to the top-level of the OpenAPI `body` in the creation of the REST API. The `extensions` argument allows full configuration using the [API Gateway extensions to OpenAPI](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html). As opposed to top-level extensions, the endpoint-level extensions can be configured at the same level as the `lambda` configuration. See the example below to get a glimpse of how this would work. |
| 37 | + |
| 38 | +```hcl |
| 39 | +module "api" { |
| 40 | + source = "git@github.com:skyleague/aws-rest-api.git?ref=v1.0.0" |
| 41 | +
|
| 42 | + name = "my-awesome-api" |
| 43 | +
|
| 44 | + definition = jsonencode({ |
| 45 | + "/v1/hello-world" = { |
| 46 | + "GET" = { |
| 47 | + parameters = [{ |
| 48 | + name = "name", |
| 49 | + in = "query", |
| 50 | + required = true, |
| 51 | + type = "string" |
| 52 | + }] |
| 53 | + "x-amazon-apigateway-integration" = { |
| 54 | + cacheKeyParameters = ["method.request.querystring.name"] |
| 55 | + } |
| 56 | + lambda = aws_lambda_function.hello_world |
| 57 | + } |
| 58 | + } |
| 59 | + }) |
| 60 | +
|
| 61 | + extensions = jsonencode({ |
| 62 | + "x-amazon-apigateway-binary-media-types": [ "application/octet", "image/jpeg" ] |
| 63 | + }) |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Future additions |
| 68 | + |
| 69 | +This is the initial release of the module, with a very minimal set of standardized functionality. Most other functionality can already be achieved through [API Gateway extensions to OpenAPI](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html), even the ones mentioned for standardization below. We plan on standardizing more integrations, so feel free to leave suggestions! Candidates include: |
| 70 | + |
| 71 | +- Authorizers (custom, apiKey, etc) |
| 72 | +- Direct S3 integrations |
| 73 | +- Standardized `MOCK` integrations |
| 74 | +- Standardized `HTTP_PROXY` integrations |
| 75 | +- ... **Your suggestions!** |
0 commit comments