Skip to content

Commit 2912c10

Browse files
committed
feat: implement initial version of the module
1 parent 3cc1e49 commit 2912c10

16 files changed

+1652
-916
lines changed

README.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,75 @@
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!**

iam.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "aws_cloudformation_stack" "lambda_permissions" {
2+
name = "${var.name}-lambda-permissions-${aws_api_gateway_rest_api.this.id}"
3+
template_body = jsonencode({
4+
Resources = merge([
5+
for urlPath, config in local.definition : {
6+
for httpMethod, definition in config : "AllowExecutionFromAPIGateway${substr(sha256("${upper(httpMethod)} ${urlPath} "), 0, 8)}" => {
7+
Type = "AWS::Lambda::Permission"
8+
Properties = {
9+
FunctionName = definition.lambda.function_name
10+
Action = "lambda:InvokeFunction"
11+
Principal = "apigateway.amazonaws.com"
12+
13+
# # More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
14+
SourceArn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.this.id}/*/${upper(httpMethod)}${urlPath}"
15+
}
16+
}
17+
}
18+
]...)
19+
})
20+
}

logs.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
resource "aws_cloudwatch_log_group" "execution" {
2+
for_each = {
3+
for stage, config in {
4+
for stage in local.log_stages : stage => lookup(var.log_settings_override, stage, {
5+
disabled = var.log_creation_disabled
6+
retention_in_days = var.log_retention_in_days
7+
kms_key_id = var.log_kms_key_id
8+
})
9+
} : stage => config if !config.disabled
10+
}
11+
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.this.id}/${each.key}"
12+
retention_in_days = each.value.retention_in_days
13+
kms_key_id = each.value.kms_key_id
14+
}
15+
16+
resource "aws_cloudwatch_log_group" "access" {
17+
for_each = {
18+
for stage, config in {
19+
for stage in local.log_stages : stage => lookup(var.log_settings_override, stage, {
20+
disabled = var.log_creation_disabled
21+
retention_in_days = var.log_retention_in_days
22+
kms_key_id = var.log_kms_key_id
23+
})
24+
} : stage => config if !config.disabled
25+
}
26+
name = "API-Gateway-Access-Logs_${aws_api_gateway_rest_api.this.id}/${each.key}"
27+
retention_in_days = each.value.retention_in_days
28+
kms_key_id = each.value.kms_key_id
29+
}

main.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
data "external" "spec" {
2+
program = ["npx", "-y", "ts-node", "-T", "--skip-project", "${path.module}/scripts/src/index.ts"]
3+
4+
query = {
5+
definition = jsonencode(local.definition)
6+
extensions = var.extensions
7+
}
8+
}
9+
resource "aws_api_gateway_rest_api" "this" {
10+
name = var.name
11+
description = coalesce(var.description, "API for ${var.name}")
12+
13+
disable_execute_api_endpoint = var.disable_execute_api_endpoint
14+
endpoint_configuration {
15+
types = [var.endpoint_type]
16+
vpc_endpoint_ids = var.endpoint_type == "PRIVATE" ? var.vpc_endpoint_ids : null
17+
}
18+
19+
body = jsonencode(jsondecode(data.external.spec.result.spec))
20+
}
21+
22+
resource "aws_api_gateway_method_settings" "this" {
23+
for_each = var.disable_global_method_settings ? [] : var.stages
24+
25+
rest_api_id = aws_api_gateway_rest_api.this.id
26+
stage_name = aws_api_gateway_stage.this[each.key].stage_name
27+
method_path = "*/*"
28+
29+
settings {
30+
logging_level = var.logging_level
31+
metrics_enabled = var.metrics_enabled
32+
data_trace_enabled = var.data_trace_enabled
33+
}
34+
}
35+
36+
resource "aws_api_gateway_deployment" "this" {
37+
rest_api_id = aws_api_gateway_rest_api.this.id
38+
39+
triggers = {
40+
redeployment = sha1(aws_api_gateway_rest_api.this.body)
41+
}
42+
43+
lifecycle {
44+
create_before_destroy = true
45+
}
46+
}
47+
48+
resource "aws_api_gateway_stage" "this" {
49+
for_each = var.stages
50+
deployment_id = aws_api_gateway_deployment.this.id
51+
rest_api_id = aws_api_gateway_rest_api.this.id
52+
stage_name = each.value
53+
54+
xray_tracing_enabled = var.xray_tracing_enabled
55+
56+
access_log_settings {
57+
destination_arn = aws_cloudwatch_log_group.access[each.key].arn
58+
format = jsonencode(var.custom_access_logs_format)
59+
}
60+
61+
depends_on = [
62+
aws_cloudwatch_log_group.execution,
63+
aws_cloudformation_stack.lambda_permissions,
64+
]
65+
}

outputs.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
output "rest_api" {
2+
value = aws_api_gateway_rest_api.this
3+
}
4+
5+
output "stage" {
6+
value = aws_api_gateway_stage.this
7+
}
8+
9+
output "access_log_groups" {
10+
value = aws_cloudwatch_log_group.access
11+
}
12+
13+
output "execution_log_groups" {
14+
value = aws_cloudwatch_log_group.execution
15+
}

0 commit comments

Comments
 (0)