Skip to content

Commit f3bb532

Browse files
committed
chore: cleanup and doc updates
1 parent cfc7b2e commit f3bb532

File tree

14 files changed

+223
-217
lines changed

14 files changed

+223
-217
lines changed

README.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ This Terraform module is part of [serverless.tf framework](https://serverless.tf
66

77
## Supported Features
88

9-
- Support many of features of HTTP API Gateway, but rather limited support for WebSocket API Gateway
9+
- Nearly all features of HTTP API Gateway
1010
- Conditional creation for many types of resources
1111

12-
## Feature Roadmap
13-
14-
- Some features are still missing (especially for WebSocket support)
15-
1612
## Usage
1713

1814
### HTTP API Gateway
@@ -36,8 +32,8 @@ module "api_gateway" {
3632
domain_name_certificate_arn = "arn:aws:acm:eu-west-1:052235179155:certificate/2b3a7ed9-05e1-4f9e-952b-27744ba06da6"
3733
3834
# Access logs
39-
default_stage_access_log_destination_arn = "arn:aws:logs:eu-west-1:835367859851:log-group:debug-apigateway"
40-
default_stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
35+
stage_access_log_destination_arn = "arn:aws:logs:eu-west-1:835367859851:log-group:debug-apigateway"
36+
stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
4137
4238
# Routes and integrations
4339
integrations = {
@@ -68,12 +64,12 @@ module "api_gateway" {
6864
6965
create = false # to disable all resources
7066
71-
create_api_gateway = false # to control creation of API Gateway
72-
create_api_domain_name = false # to control creation of API Gateway Domain Name
73-
create_default_stage = false # to control creation of "$default" stage
74-
create_default_stage_api_mapping = false # to control creation of "$default" stage and API mapping
75-
create_routes_and_integrations = false # to control creation of routes and integrations
76-
create_vpc_link = false # to control creation of VPC link
67+
create_api_gateway = false # to control creation of API Gateway
68+
create_api_domain_name = false # to control creation of API Gateway Domain Name
69+
create_stage = false # to control creation of "$default" stage
70+
create_stage_api_mapping = false # to control creation of "$default" stage and API mapping
71+
create_routes_and_integrations = false # to control creation of routes and integrations
72+
create_vpc_link = false # to control creation of VPC link
7773
7874
# ... omitted
7975
}
@@ -87,6 +83,7 @@ module "api_gateway" {
8783

8884
- [Complete HTTP](https://github.com/terraform-aws-modules/terraform-aws-apigateway-v2/tree/master/examples/complete-http) - Create API Gateway, authorizer, domain name, stage and other resources in various combinations
8985
- [HTTP with VPC Link](https://github.com/terraform-aws-modules/terraform-aws-apigateway-v2/tree/master/examples/vpc-link-http) - Create API Gateway with VPC link and integration with resources in VPC (eg. ALB)
86+
- [Websocket](https://github.com/terraform-aws-modules/terraform-aws-apigateway-v2/tree/master/examples/websocket) - Create Websocket API
9087

9188
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
9289
## Requirements
@@ -173,13 +170,13 @@ No modules.
173170
| <a name="output_apigatewayv2_domain_name_id"></a> [apigatewayv2\_domain\_name\_id](#output\_apigatewayv2\_domain\_name\_id) | The domain name identifier |
174171
| <a name="output_apigatewayv2_domain_name_target_domain_name"></a> [apigatewayv2\_domain\_name\_target\_domain\_name](#output\_apigatewayv2\_domain\_name\_target\_domain\_name) | The target domain name |
175172
| <a name="output_apigatewayv2_route"></a> [apigatewayv2\_route](#output\_apigatewayv2\_route) | Map containing the routes created and their attributes |
173+
| <a name="output_apigatewayv2_stage_arn"></a> [apigatewayv2\_stage\_arn](#output\_apigatewayv2\_stage\_arn) | The stage ARN |
174+
| <a name="output_apigatewayv2_stage_domain_name"></a> [apigatewayv2\_stage\_domain\_name](#output\_apigatewayv2\_stage\_domain\_name) | Domain name of the stage (useful for CloudFront distribution) |
175+
| <a name="output_apigatewayv2_stage_execution_arn"></a> [apigatewayv2\_stage\_execution\_arn](#output\_apigatewayv2\_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. |
176+
| <a name="output_apigatewayv2_stage_id"></a> [apigatewayv2\_stage\_id](#output\_apigatewayv2\_stage\_id) | The stage identifier |
177+
| <a name="output_apigatewayv2_stage_invoke_url"></a> [apigatewayv2\_stage\_invoke\_url](#output\_apigatewayv2\_stage\_invoke\_url) | The URL to invoke the API pointing to the stage |
176178
| <a name="output_apigatewayv2_vpc_link_arn"></a> [apigatewayv2\_vpc\_link\_arn](#output\_apigatewayv2\_vpc\_link\_arn) | The map of VPC Link ARNs |
177179
| <a name="output_apigatewayv2_vpc_link_id"></a> [apigatewayv2\_vpc\_link\_id](#output\_apigatewayv2\_vpc\_link\_id) | The map of VPC Link identifiers |
178-
| <a name="output_default_apigatewayv2_stage_arn"></a> [default\_apigatewayv2\_stage\_arn](#output\_default\_apigatewayv2\_stage\_arn) | The default stage ARN |
179-
| <a name="output_default_apigatewayv2_stage_domain_name"></a> [default\_apigatewayv2\_stage\_domain\_name](#output\_default\_apigatewayv2\_stage\_domain\_name) | Domain name of the stage (useful for CloudFront distribution) |
180-
| <a name="output_default_apigatewayv2_stage_execution_arn"></a> [default\_apigatewayv2\_stage\_execution\_arn](#output\_default\_apigatewayv2\_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. |
181-
| <a name="output_default_apigatewayv2_stage_id"></a> [default\_apigatewayv2\_stage\_id](#output\_default\_apigatewayv2\_stage\_id) | The default stage identifier |
182-
| <a name="output_default_apigatewayv2_stage_invoke_url"></a> [default\_apigatewayv2\_stage\_invoke\_url](#output\_default\_apigatewayv2\_stage\_invoke\_url) | The URL to invoke the API pointing to the stage |
183180
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
184181

185182
## Authors

examples/complete-http/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Note that this example may create resources which cost money. Run `terraform des
3030

3131
| Name | Version |
3232
|------|---------|
33-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.35 |
34-
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
35-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
36-
| <a name="provider_tls"></a> [tls](#provider\_tls) | >= 3.1 |
33+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 3.61.0 |
34+
| <a name="provider_null"></a> [null](#provider\_null) | 3.1.0 |
35+
| <a name="provider_random"></a> [random](#provider\_random) | 3.1.0 |
36+
| <a name="provider_tls"></a> [tls](#provider\_tls) | 3.1.0 |
3737

3838
## Modules
3939

examples/complete-http/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ module "api_gateway" {
4040
domain_name = var.domain_name
4141
domain_name_certificate_arn = module.acm.acm_certificate_arn
4242

43-
default_stage_access_log_destination_arn = aws_cloudwatch_log_group.logs.arn
44-
default_stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
43+
stage_access_log_destination_arn = aws_cloudwatch_log_group.logs.arn
44+
stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
4545

4646
default_route_settings = {
4747
detailed_metrics_enabled = true

examples/complete-http/versions.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ terraform {
22
required_version = ">= 0.13.1"
33

44
required_providers {
5-
aws = ">= 3.35"
6-
random = ">= 2.0"
7-
null = ">= 2.0"
8-
tls = ">= 3.1"
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 3.35"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 2.0"
12+
}
13+
null = {
14+
source = "hashicorp/null"
15+
version = ">= 2.0"
16+
}
17+
tls = {
18+
source = "hashicorp/tls"
19+
version = ">= 3.1"
20+
}
921
}
1022
}

examples/vpc-link-http/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Note that this example may create resources which cost money. Run `terraform des
2929

3030
| Name | Version |
3131
|------|---------|
32-
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
33-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
32+
| <a name="provider_null"></a> [null](#provider\_null) | 3.1.0 |
33+
| <a name="provider_random"></a> [random](#provider\_random) | 3.1.0 |
3434

3535
## Modules
3636

examples/vpc-link-http/versions.tf

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ terraform {
22
required_version = ">= 0.13.1"
33

44
required_providers {
5-
aws = ">= 3.35"
6-
random = ">= 2.0"
7-
null = ">= 2.0"
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 3.35"
8+
}
9+
random = {
10+
source = "hashicorp/random"
11+
version = ">= 2.0"
12+
}
13+
null = {
14+
source = "hashicorp/null"
15+
version = ">= 2.0"
16+
}
817
}
918
}

examples/websocket/README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Websocket AWS API Gateway examples
2-
3-
Configuration in this directory creates AWS API Gateway with Domain Name, ACM Certificate, and integrates it with Lambda and Step Function and shows the variety of supported features.
1+
# AWS Websocket API example
42

3+
Configuration in this directory creates an AWS Websocket API.
4+
This example is based off of https://github.com/aws-samples/simple-websockets-chat-app
55

66
## Usage
77

@@ -15,13 +15,40 @@ $ terraform apply
1515

1616
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
1717

18+
## Testing the chat API
19+
20+
To test the WebSocket API, you can use [wscat](https://github.com/websockets/wscat), an open-source command line tool.
21+
22+
1. [Install NPM](https://www.npmjs.com/get-npm).
23+
2. Install wscat:
24+
25+
```bash
26+
$ npm install -g wscat
27+
```
28+
29+
3. On the console, connect to your published API endpoint by executing the following command:
30+
31+
```bash
32+
$ wscat -c wss://{YOUR-API-ID}.execute-api.{YOUR-REGION}.amazonaws.com/{STAGE}
33+
```
34+
35+
4. To test the sendMessage function, send a JSON message like the following example. The Lambda function sends it back using the callback URL:
36+
37+
```bash
38+
$ wscat -c wss://{YOUR-API-ID}.execute-api.{YOUR-REGION}.amazonaws.com/{STAGE}
39+
connected (press CTRL+C to quit)
40+
> {"action":"sendmessage", "data":"hello world"}
41+
< hello world
42+
```
43+
1844
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1945
## Requirements
2046

2147
| Name | Version |
2248
|------|---------|
2349
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
2450
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.35 |
51+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2552

2653
## Providers
2754

@@ -58,5 +85,21 @@ No inputs.
5885

5986
| Name | Description |
6087
|------|-------------|
61-
| <a name="output_wss"></a> [wss](#output\_wss) | n/a |
88+
| <a name="output_apigatewayv2_api_api_endpoint"></a> [apigatewayv2\_api\_api\_endpoint](#output\_apigatewayv2\_api\_api\_endpoint) | The URI of the API |
89+
| <a name="output_apigatewayv2_api_arn"></a> [apigatewayv2\_api\_arn](#output\_apigatewayv2\_api\_arn) | The ARN of the API |
90+
| <a name="output_apigatewayv2_api_execution_arn"></a> [apigatewayv2\_api\_execution\_arn](#output\_apigatewayv2\_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. |
91+
| <a name="output_apigatewayv2_api_id"></a> [apigatewayv2\_api\_id](#output\_apigatewayv2\_api\_id) | The API identifier |
92+
| <a name="output_apigatewayv2_api_mapping_id"></a> [apigatewayv2\_api\_mapping\_id](#output\_apigatewayv2\_api\_mapping\_id) | The API mapping identifier |
93+
| <a name="output_apigatewayv2_domain_name_api_mapping_selection_expression"></a> [apigatewayv2\_domain\_name\_api\_mapping\_selection\_expression](#output\_apigatewayv2\_domain\_name\_api\_mapping\_selection\_expression) | The API mapping selection expression for the domain name |
94+
| <a name="output_apigatewayv2_domain_name_arn"></a> [apigatewayv2\_domain\_name\_arn](#output\_apigatewayv2\_domain\_name\_arn) | The ARN of the domain name |
95+
| <a name="output_apigatewayv2_domain_name_configuration"></a> [apigatewayv2\_domain\_name\_configuration](#output\_apigatewayv2\_domain\_name\_configuration) | The domain name configuration |
96+
| <a name="output_apigatewayv2_domain_name_hosted_zone_id"></a> [apigatewayv2\_domain\_name\_hosted\_zone\_id](#output\_apigatewayv2\_domain\_name\_hosted\_zone\_id) | The Amazon Route 53 Hosted Zone ID of the endpoint |
97+
| <a name="output_apigatewayv2_domain_name_id"></a> [apigatewayv2\_domain\_name\_id](#output\_apigatewayv2\_domain\_name\_id) | The domain name identifier |
98+
| <a name="output_apigatewayv2_domain_name_target_domain_name"></a> [apigatewayv2\_domain\_name\_target\_domain\_name](#output\_apigatewayv2\_domain\_name\_target\_domain\_name) | The target domain name |
99+
| <a name="output_apigatewayv2_route"></a> [apigatewayv2\_route](#output\_apigatewayv2\_route) | Map containing the routes created and their attributes |
100+
| <a name="output_apigatewayv2_stage_arn"></a> [apigatewayv2\_stage\_arn](#output\_apigatewayv2\_stage\_arn) | The default stage ARN |
101+
| <a name="output_apigatewayv2_stage_domain_name"></a> [apigatewayv2\_stage\_domain\_name](#output\_apigatewayv2\_stage\_domain\_name) | Domain name of the stage (useful for CloudFront distribution) |
102+
| <a name="output_apigatewayv2_stage_execution_arn"></a> [apigatewayv2\_stage\_execution\_arn](#output\_apigatewayv2\_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. |
103+
| <a name="output_apigatewayv2_stage_id"></a> [apigatewayv2\_stage\_id](#output\_apigatewayv2\_stage\_id) | The default stage identifier |
104+
| <a name="output_apigatewayv2_stage_invoke_url"></a> [apigatewayv2\_stage\_invoke\_url](#output\_apigatewayv2\_stage\_invoke\_url) | The URL to invoke the API pointing to the stage |
62105
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/websocket/main.tf

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ locals {
1818
region = "eu-west-1"
1919

2020
dynamodb_table_name = local.name
21+
dynamodb_table_arn = "arn:aws:dynamodb:${local.region}:${data.aws_caller_identity.current.account_id}:table/${local.dynamodb_table_name}" # hack to avoid race condition
2122
dynamodb_crud_permissions = {
2223
effect = "Allow",
2324
actions = [
@@ -33,8 +34,8 @@ locals {
3334
"dynamodb:ConditionCheckItem",
3435
],
3536
resources = [
36-
module.dynamodb_table.dynamodb_table_arn,
37-
"${module.dynamodb_table.dynamodb_table_arn}/index/*"
37+
local.dynamodb_table_arn,
38+
"${local.dynamodb_table_arn}/index/*"
3839
]
3940
}
4041

@@ -84,10 +85,6 @@ resource "aws_iam_role" "cloudwatch" {
8485
tags = local.tags
8586
}
8687

87-
########################
88-
# Websocket API Gateway
89-
########################
90-
9188
module "connect_lambda_function" {
9289
source = "terraform-aws-modules/lambda/aws"
9390
version = "~> 2"
@@ -107,8 +104,8 @@ module "connect_lambda_function" {
107104

108105
allowed_triggers = {
109106
AllowExecutionFromAPIGateway = {
110-
service = "apigateway"
111-
principal = "apigateway.amazonaws.com"
107+
service = "apigateway"
108+
source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*"
112109
}
113110
}
114111

@@ -120,7 +117,6 @@ module "connect_lambda_function" {
120117
tags = local.tags
121118
}
122119

123-
124120
module "disconnect_lambda_function" {
125121
source = "terraform-aws-modules/lambda/aws"
126122
version = "~> 2"
@@ -140,8 +136,8 @@ module "disconnect_lambda_function" {
140136

141137
allowed_triggers = {
142138
AllowExecutionFromAPIGateway = {
143-
service = "apigateway"
144-
principal = "apigateway.amazonaws.com"
139+
service = "apigateway"
140+
source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*"
145141
}
146142
}
147143

@@ -153,7 +149,6 @@ module "disconnect_lambda_function" {
153149
tags = local.tags
154150
}
155151

156-
157152
module "send_message_lambda_function" {
158153
source = "terraform-aws-modules/lambda/aws"
159154
version = "~> 2"
@@ -173,8 +168,8 @@ module "send_message_lambda_function" {
173168

174169
allowed_triggers = {
175170
AllowExecutionFromAPIGateway = {
176-
service = "apigateway"
177-
principal = "apigateway.amazonaws.com"
171+
service = "apigateway"
172+
source_arn = "${module.api_gateway.apigatewayv2_api_execution_arn}/*/*"
178173
}
179174
}
180175

@@ -208,6 +203,10 @@ module "dynamodb_table" {
208203
tags = local.tags
209204
}
210205

206+
########################
207+
# Websocket API Gateway
208+
########################
209+
211210
module "api_gateway" {
212211
source = "../../"
213212

@@ -229,7 +228,6 @@ module "api_gateway" {
229228
stage_access_log_format = jsonencode({
230229
context = {
231230
domainName = "$context.domainName"
232-
httpMethod = "$context.httpMethod"
233231
integrationErrorMessage = "$context.integrationErrorMessage"
234232
protocol = "$context.protocol"
235233
requestId = "$context.requestId"
@@ -282,7 +280,3 @@ module "api_gateway" {
282280

283281
tags = local.tags
284282
}
285-
286-
output "wss" {
287-
value = "wscat -c ${module.api_gateway.default_apigatewayv2_stage_invoke_url}"
288-
}

0 commit comments

Comments
 (0)