Skip to content

Commit 7ae31f3

Browse files
committed
fix: We are going to get this across the line once and for all!
1 parent 7bed76a commit 7ae31f3

File tree

16 files changed

+252
-154
lines changed

16 files changed

+252
-154
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ Terraform module which creates API Gateway v2 resources with HTTP/Websocket capa
44

55
This Terraform module is part of [serverless.tf framework](https://serverless.tf), which aims to simplify all operations when working with the serverless in Terraform.
66

7-
## Supported Features
8-
9-
- Conditional creation
10-
- VPC Links
11-
- HTTP API
12-
- Websocket API
13-
147
## Usage
158

169
### HTTP API Gateway
@@ -110,18 +103,20 @@ module "api_gateway" {
110103

111104
| Name | Version |
112105
|------|---------|
113-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
114-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.30 |
106+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3 |
107+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.37 |
115108

116109
## Providers
117110

118111
| Name | Version |
119112
|------|---------|
120-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.30 |
113+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.37 |
121114

122115
## Modules
123116

124-
No modules.
117+
| Name | Source | Version |
118+
|------|--------|---------|
119+
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | 5.0.1 |
125120

126121
## Resources
127122

@@ -136,6 +131,9 @@ No modules.
136131
| [aws_apigatewayv2_route.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_route) | resource |
137132
| [aws_apigatewayv2_stage.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_stage) | resource |
138133
| [aws_apigatewayv2_vpc_link.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_vpc_link) | resource |
134+
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
135+
| [aws_route53_record.api](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
136+
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
139137

140138
## Inputs
141139

@@ -148,7 +146,9 @@ No modules.
148146
| <a name="input_body"></a> [body](#input\_body) | An OpenAPI specification that defines the set of routes and integrations to create as part of the HTTP APIs. Supported only for HTTP APIs | `string` | `null` | no |
149147
| <a name="input_cors_configuration"></a> [cors\_configuration](#input\_cors\_configuration) | The cross-origin resource sharing (CORS) configuration. Applicable for HTTP APIs | `any` | `{}` | no |
150148
| <a name="input_create"></a> [create](#input\_create) | Controls if resources should be created | `bool` | `true` | no |
149+
| <a name="input_create_certificate"></a> [create\_certificate](#input\_create\_certificate) | Whether to create a certificate for the domain | `bool` | `false` | no |
151150
| <a name="input_create_domain_name"></a> [create\_domain\_name](#input\_create\_domain\_name) | Whether to create API domain name resource | `bool` | `false` | no |
151+
| <a name="input_create_domain_records"></a> [create\_domain\_records](#input\_create\_domain\_records) | Whether to create Route53 records for the domain name | `bool` | `false` | no |
152152
| <a name="input_create_routes_and_integrations"></a> [create\_routes\_and\_integrations](#input\_create\_routes\_and\_integrations) | Whether to create routes and integrations resources | `bool` | `true` | no |
153153
| <a name="input_create_stage"></a> [create\_stage](#input\_create\_stage) | Whether to create default stage | `bool` | `true` | no |
154154
| <a name="input_credentials_arn"></a> [credentials\_arn](#input\_credentials\_arn) | Part of quick create. Specifies any credentials required for the integration. Applicable for HTTP APIs | `string` | `null` | no |
@@ -171,6 +171,7 @@ No modules.
171171
| <a name="input_stage_name"></a> [stage\_name](#input\_stage\_name) | The name of the stage. Must be between 1 and 128 characters in length | `string` | `"$default"` | no |
172172
| <a name="input_stage_tags"></a> [stage\_tags](#input\_stage\_tags) | A mapping of tags to assign to the stage resource | `map(string)` | `{}` | no |
173173
| <a name="input_stage_variables"></a> [stage\_variables](#input\_stage\_variables) | A map that defines the stage variables for the stage | `map(string)` | `{}` | no |
174+
| <a name="input_subdomain"></a> [subdomain](#input\_subdomain) | An optional subdomain to use for API gateway (prepended to the `domain_name` when the records are created) | `string` | `null` | no |
174175
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to API gateway resources | `map(string)` | `{}` | no |
175176
| <a name="input_target"></a> [target](#input\_target) | Part of quick create. Quick create produces an API with an integration, a default catch-all route, and a default stage which is configured to automatically deploy changes. For HTTP integrations, specify a fully qualified URL. For Lambda integrations, specify a function ARN. The type of the integration will be HTTP\_PROXY or AWS\_PROXY, respectively. Applicable for HTTP APIs | `string` | `null` | no |
176177
| <a name="input_vpc_link_tags"></a> [vpc\_link\_tags](#input\_vpc\_link\_tags) | A map of tags to add to the VPC Links created | `map(string)` | `{}` | no |
@@ -180,6 +181,7 @@ No modules.
180181

181182
| Name | Description |
182183
|------|-------------|
184+
| <a name="output_acm_certificate_arn"></a> [acm\_certificate\_arn](#output\_acm\_certificate\_arn) | The ARN of the certificate |
183185
| <a name="output_api_arn"></a> [api\_arn](#output\_api\_arn) | The ARN of the API |
184186
| <a name="output_api_endpoint"></a> [api\_endpoint](#output\_api\_endpoint) | URI of the API, of the form `https://{api-id}.execute-api.{region}.amazonaws.com` for HTTP APIs and `wss://{api-id}.execute-api.{region}.amazonaws.com` for WebSocket APIs |
185187
| <a name="output_api_execution_arn"></a> [api\_execution\_arn](#output\_api\_execution\_arn) | The ARN prefix to be used in an `aws_lambda_permission`'s `source_arn` attribute or in an `aws_iam_policy` to authorize access to the `@connections` API |
@@ -193,6 +195,8 @@ No modules.
193195
| <a name="output_domain_name_target_domain_name"></a> [domain\_name\_target\_domain\_name](#output\_domain\_name\_target\_domain\_name) | The target domain name |
194196
| <a name="output_integrations"></a> [integrations](#output\_integrations) | Map of the integrations created and their attributes |
195197
| <a name="output_routes"></a> [routes](#output\_routes) | Map of the routes created and their attributes |
198+
| <a name="output_stage_access_logs_cloudwatch_log_group_arn"></a> [stage\_access\_logs\_cloudwatch\_log\_group\_arn](#output\_stage\_access\_logs\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
199+
| <a name="output_stage_access_logs_cloudwatch_log_group_name"></a> [stage\_access\_logs\_cloudwatch\_log\_group\_name](#output\_stage\_access\_logs\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
196200
| <a name="output_stage_arn"></a> [stage\_arn](#output\_stage\_arn) | The stage ARN |
197201
| <a name="output_stage_execution_arn"></a> [stage\_execution\_arn](#output\_stage\_execution\_arn) | The ARN prefix to be used in an aws\_lambda\_permission's source\_arn attribute or in an aws\_iam\_policy to authorize access to the @connections API |
198202
| <a name="output_stage_id"></a> [stage\_id](#output\_stage\_id) | The stage identifier |

examples/complete-http/README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,34 @@ Note that this example may create resources which cost money. Run `terraform des
1919

2020
| Name | Version |
2121
|------|---------|
22-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
23-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.0 |
24-
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
22+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.37 |
2524
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | >= 3.1 |
2625

2726
## Providers
2827

2928
| Name | Version |
3029
|------|---------|
31-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0 |
32-
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
30+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.37 |
3331
| <a name="provider_tls"></a> [tls](#provider\_tls) | >= 3.1 |
3432

3533
## Modules
3634

3735
| Name | Source | Version |
3836
|------|--------|---------|
39-
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
4037
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | ../../ | n/a |
41-
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 4.0 |
42-
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 3.0 |
43-
| <a name="module_step_function"></a> [step\_function](#module\_step\_function) | terraform-aws-modules/step-functions/aws | ~> 2.0 |
38+
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 7.0 |
39+
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 4.0 |
40+
| <a name="module_step_function"></a> [step\_function](#module\_step\_function) | terraform-aws-modules/step-functions/aws | ~> 4.0 |
4441

4542
## Resources
4643

4744
| Name | Type |
4845
|------|------|
49-
| [aws_cloudwatch_log_group.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
5046
| [aws_cognito_user_pool.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool) | resource |
51-
| [aws_route53_record.api](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
5247
| [aws_s3_object.truststore](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_object) | resource |
53-
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
5448
| [tls_private_key.private_key](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource |
5549
| [tls_self_signed_cert.example](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/self_signed_cert) | resource |
56-
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
5750

5851
## Inputs
5952

@@ -65,6 +58,7 @@ Note that this example may create resources which cost money. Run `terraform des
6558

6659
| Name | Description |
6760
|------|-------------|
61+
| <a name="output_acm_certificate_arn"></a> [acm\_certificate\_arn](#output\_acm\_certificate\_arn) | The ARN of the certificate |
6862
| <a name="output_api_arn"></a> [api\_arn](#output\_api\_arn) | The ARN of the API |
6963
| <a name="output_api_endpoint"></a> [api\_endpoint](#output\_api\_endpoint) | URI of the API, of the form `https://{api-id}.execute-api.{region}.amazonaws.com` for HTTP APIs and `wss://{api-id}.execute-api.{region}.amazonaws.com` for WebSocket APIs |
7064
| <a name="output_api_execution_arn"></a> [api\_execution\_arn](#output\_api\_execution\_arn) | The ARN prefix to be used in an `aws_lambda_permission`'s `source_arn` attribute or in an `aws_iam_policy` to authorize access to the `@connections` API |
@@ -78,6 +72,8 @@ Note that this example may create resources which cost money. Run `terraform des
7872
| <a name="output_domain_name_target_domain_name"></a> [domain\_name\_target\_domain\_name](#output\_domain\_name\_target\_domain\_name) | The target domain name |
7973
| <a name="output_integrations"></a> [integrations](#output\_integrations) | Map of the integrations created and their attributes |
8074
| <a name="output_routes"></a> [routes](#output\_routes) | Map of the routes created and their attributes |
75+
| <a name="output_stage_access_logs_cloudwatch_log_group_arn"></a> [stage\_access\_logs\_cloudwatch\_log\_group\_arn](#output\_stage\_access\_logs\_cloudwatch\_log\_group\_arn) | Arn of cloudwatch log group created |
76+
| <a name="output_stage_access_logs_cloudwatch_log_group_name"></a> [stage\_access\_logs\_cloudwatch\_log\_group\_name](#output\_stage\_access\_logs\_cloudwatch\_log\_group\_name) | Name of cloudwatch log group created |
8177
| <a name="output_stage_arn"></a> [stage\_arn](#output\_stage\_arn) | The stage ARN |
8278
| <a name="output_stage_execution_arn"></a> [stage\_execution\_arn](#output\_stage\_execution\_arn) | The ARN prefix to be used in an aws\_lambda\_permission's source\_arn attribute or in an aws\_iam\_policy to authorize access to the @connections API |
8379
| <a name="output_stage_id"></a> [stage\_id](#output\_stage\_id) | The stage identifier |

examples/complete-http/lambda.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
def handler(event, context):
4+
print(json.dumps(event))
5+
6+
return event

examples/complete-http/main.tf

Lines changed: 40 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ locals {
66
name = "ex-${basename(path.cwd)}"
77
region = "eu-west-1"
88

9-
subdomain = "complete-http"
10-
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python3.8-zip/existing_package.zip"
11-
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
12-
139
tags = {
1410
Example = local.name
1511
GithubRepo = "terraform-aws-apigateway-v2"
@@ -24,41 +20,23 @@ locals {
2420
module "api_gateway" {
2521
source = "../../"
2622

27-
name = local.name
28-
description = "My awesome HTTP API Gateway"
29-
30-
protocol_type = "HTTP"
31-
create_domain_name = true
32-
33-
# create_default_stage_access_log_group = true
34-
35-
fail_on_warnings = false
23+
# API
24+
body = templatefile("api.yaml", {
25+
example_function_arn = module.lambda_function.lambda_function_arn
26+
})
3627

3728
cors_configuration = {
3829
allow_headers = ["content-type", "x-amz-date", "authorization", "x-api-key", "x-amz-security-token", "x-amz-user-agent"]
3930
allow_methods = ["*"]
4031
allow_origins = ["*"]
4132
}
4233

43-
mutual_tls_authentication = {
44-
truststore_uri = "s3://${module.s3_bucket.s3_bucket_id}/${aws_s3_object.truststore.id}"
45-
truststore_version = aws_s3_object.truststore.version_id
46-
}
47-
48-
domain_name = var.domain_name
49-
domain_name_certificate_arn = module.acm.acm_certificate_arn
50-
51-
stage_access_log_settings = {
52-
destination_arn = aws_cloudwatch_log_group.logs.arn
53-
format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
54-
}
55-
56-
stage_default_route_settings = {
57-
detailed_metrics_enabled = true
58-
throttling_burst_limit = 100
59-
throttling_rate_limit = 100
60-
}
34+
description = "My awesome HTTP API Gateway"
35+
fail_on_warnings = false
36+
name = local.name
37+
protocol_type = "HTTP"
6138

39+
# Authorizer(s)
6240
authorizers = {
6341
"cognito" = {
6442
authorizer_type = "JWT"
@@ -71,6 +49,19 @@ module "api_gateway" {
7149
}
7250
}
7351

52+
# Domain Name
53+
create_domain_name = true
54+
domain_name = var.domain_name
55+
create_domain_records = true
56+
create_certificate = true
57+
subdomain = "example"
58+
59+
mutual_tls_authentication = {
60+
truststore_uri = "s3://${module.s3_bucket.s3_bucket_id}/${aws_s3_object.truststore.id}"
61+
truststore_version = aws_s3_object.truststore.version_id
62+
}
63+
64+
# Routes & Integration(s)
7465
integrations = {
7566
"ANY /" = {
7667
lambda_arn = module.lambda_function.lambda_function_arn
@@ -146,12 +137,20 @@ module "api_gateway" {
146137
}
147138
])
148139
}
140+
}
149141

142+
# Stage
143+
stage_access_log_settings = {
144+
create_log_group = true
145+
log_group_retention_in_days = 7
146+
format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
150147
}
151148

152-
body = templatefile("api.yaml", {
153-
example_function_arn = module.lambda_function.lambda_function_arn
154-
})
149+
stage_default_route_settings = {
150+
detailed_metrics_enabled = true
151+
throttling_burst_limit = 100
152+
throttling_rate_limit = 100
153+
}
155154

156155
tags = local.tags
157156
}
@@ -160,33 +159,6 @@ module "api_gateway" {
160159
# Supporting Resources
161160
################################################################################
162161

163-
data "aws_route53_zone" "this" {
164-
name = var.domain_name
165-
}
166-
167-
module "acm" {
168-
source = "terraform-aws-modules/acm/aws"
169-
version = "~> 3.0"
170-
171-
domain_name = var.domain_name
172-
zone_id = data.aws_route53_zone.this.id
173-
subject_alternative_names = ["${local.subdomain}.${var.domain_name}"]
174-
175-
tags = local.tags
176-
}
177-
178-
resource "aws_route53_record" "api" {
179-
zone_id = data.aws_route53_zone.this.zone_id
180-
name = local.subdomain
181-
type = "A"
182-
183-
alias {
184-
name = module.api_gateway.domain_name_configuration[0].target_domain_name
185-
zone_id = module.api_gateway.domain_name_configuration[0].hosted_zone_id
186-
evaluate_target_health = false
187-
}
188-
}
189-
190162
resource "aws_cognito_user_pool" "this" {
191163
name = local.name
192164

@@ -195,7 +167,7 @@ resource "aws_cognito_user_pool" "this" {
195167

196168
module "step_function" {
197169
source = "terraform-aws-modules/step-functions/aws"
198-
version = "~> 2.0"
170+
version = "~> 4.0"
199171

200172
name = local.name
201173
role_name = "${local.name}-step-function"
@@ -222,36 +194,17 @@ module "step_function" {
222194
tags = local.tags
223195
}
224196

225-
resource "aws_cloudwatch_log_group" "logs" {
226-
name = local.name
227-
228-
tags = local.tags
229-
}
230-
231-
# Using packaged function from Lambda module
232-
resource "null_resource" "download_package" {
233-
triggers = {
234-
downloaded = local.downloaded
235-
}
236-
237-
provisioner "local-exec" {
238-
command = "curl -L -o ${local.downloaded} ${local.package_url}"
239-
}
240-
}
241-
242197
module "lambda_function" {
243198
source = "terraform-aws-modules/lambda/aws"
244-
version = "~> 4.0"
199+
version = "~> 7.0"
245200

246201
function_name = local.name
247202
description = "My awesome lambda function"
248-
handler = "index.lambda_handler"
249-
runtime = "python3.8"
250-
251-
publish = true
203+
handler = "lambda.handler"
204+
runtime = "python3.12"
252205

253-
create_package = false
254-
local_existing_package = local.downloaded
206+
publish = true
207+
source_path = "lambda.py"
255208

256209
allowed_triggers = {
257210
AllowExecutionFromAPIGateway = {
@@ -265,7 +218,7 @@ module "lambda_function" {
265218

266219
module "s3_bucket" {
267220
source = "terraform-aws-modules/s3-bucket/aws"
268-
version = "~> 3.0"
221+
version = "~> 4.0"
269222

270223
bucket_prefix = "${local.name}-"
271224

@@ -276,11 +229,6 @@ module "s3_bucket" {
276229
attach_deny_insecure_transport_policy = true
277230
attach_require_latest_tls_policy = true
278231

279-
block_public_acls = true
280-
block_public_policy = true
281-
ignore_public_acls = true
282-
restrict_public_buckets = true
283-
284232
server_side_encryption_configuration = {
285233
rule = {
286234
apply_server_side_encryption_by_default = {

0 commit comments

Comments
 (0)