Skip to content

Commit 01010b8

Browse files
committed
fix: Websocket example is working; I hate javascript
1 parent 49b1638 commit 01010b8

File tree

18 files changed

+311
-193
lines changed

18 files changed

+311
-193
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ module "api_gateway" {
125125
| [aws_apigatewayv2_api.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_api) | resource |
126126
| [aws_apigatewayv2_api_mapping.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_api_mapping) | resource |
127127
| [aws_apigatewayv2_authorizer.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_authorizer) | resource |
128+
| [aws_apigatewayv2_deployment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_deployment) | resource |
128129
| [aws_apigatewayv2_domain_name.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_domain_name) | resource |
129130
| [aws_apigatewayv2_integration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_integration) | resource |
130131
| [aws_apigatewayv2_integration_response.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_integration_response) | resource |
@@ -152,9 +153,10 @@ module "api_gateway" {
152153
| <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 |
153154
| <a name="input_create_stage"></a> [create\_stage](#input\_create\_stage) | Whether to create default stage | `bool` | `true` | no |
154155
| <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 |
156+
| <a name="input_deploy_stage"></a> [deploy\_stage](#input\_deploy\_stage) | Whether to deploy the stage | `bool` | `true` | no |
155157
| <a name="input_description"></a> [description](#input\_description) | The description of the API. Must be less than or equal to 1024 characters in length | `string` | `null` | no |
156158
| <a name="input_disable_execute_api_endpoint"></a> [disable\_execute\_api\_endpoint](#input\_disable\_execute\_api\_endpoint) | Whether clients can invoke the API by using the default execute-api endpoint. By default, clients can invoke the API with the default `{api_id}.execute-api.{region}.amazonaws.com endpoint`. To require that clients use a custom domain name to invoke the API, disable the default endpoint | `bool` | `null` | no |
157-
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | The domain name to use for API gateway | `string` | `null` | no |
159+
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | The domain name to use for API gateway | `string` | `""` | no |
158160
| <a name="input_domain_name_certificate_arn"></a> [domain\_name\_certificate\_arn](#input\_domain\_name\_certificate\_arn) | The ARN of an AWS-managed certificate that will be used by the endpoint for the domain name. AWS Certificate Manager is the only supported source | `string` | `null` | no |
159161
| <a name="input_domain_name_ownership_verification_certificate_arn"></a> [domain\_name\_ownership\_verification\_certificate\_arn](#input\_domain\_name\_ownership\_verification\_certificate\_arn) | ARN of the AWS-issued certificate used to validate custom domain ownership (when certificate\_arn is issued via an ACM Private CA or mutual\_tls\_authentication is configured with an ACM-imported certificate.) | `string` | `null` | no |
160162
| <a name="input_fail_on_warnings"></a> [fail\_on\_warnings](#input\_fail\_on\_warnings) | Whether warnings should return an error while API Gateway is creating or updating the resource using an OpenAPI specification. Defaults to `false`. Applicable for HTTP APIs | `bool` | `null` | no |

examples/complete-http/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Note that this example may create resources which cost money. Run `terraform des
6464
| <a name="output_domain_name_hosted_zone_id"></a> [domain\_name\_hosted\_zone\_id](#output\_domain\_name\_hosted\_zone\_id) | The Amazon Route 53 Hosted Zone ID of the endpoint |
6565
| <a name="output_domain_name_id"></a> [domain\_name\_id](#output\_domain\_name\_id) | The domain name identifier |
6666
| <a name="output_domain_name_target_domain_name"></a> [domain\_name\_target\_domain\_name](#output\_domain\_name\_target\_domain\_name) | The target domain name |
67+
| <a name="output_integrations"></a> [integrations](#output\_integrations) | Map of the integrations created and their attributes |
68+
| <a name="output_routes"></a> [routes](#output\_routes) | Map of the routes created and their attributes |
6769
| <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 |
6870
| <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 |
6971
| <a name="output_stage_arn"></a> [stage\_arn](#output\_stage\_arn) | The stage ARN |

examples/complete-http/main.tf

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ module "api_gateway" {
3434
description = "My awesome HTTP API Gateway"
3535
fail_on_warnings = false
3636
name = local.name
37-
protocol_type = "HTTP"
3837

3938
# Authorizer(s)
4039
authorizers = {
@@ -135,21 +134,28 @@ module "api_gateway" {
135134
create_log_group = true
136135
log_group_retention_in_days = 7
137136
format = jsonencode({
138-
"requestId" : "$context.requestId",
139-
"extendedRequestId" : "$context.extendedRequestId",
140-
"ip" : "$context.identity.sourceIp",
141-
"caller" : "$context.identity.caller",
142-
"user" : "$context.identity.user",
143-
"requestTime" : "$context.requestTime",
144-
"httpMethod" : "$context.httpMethod",
145-
"resourcePath" : "$context.resourcePath",
146-
"status" : "$context.status",
147-
"protocol" : "$context.protocol",
148-
"responseLength" : "$context.responseLength",
149-
"domainName" : "$context.domainName",
150-
"errorMessage" : "$context.error.message",
151-
"errorResponseType" : "$context.error.responseType",
152-
"integrationErrorMessage" : "$context.integrationErrorMessage",
137+
context = {
138+
domainName = "$context.domainName"
139+
integrationErrorMessage = "$context.integrationErrorMessage"
140+
protocol = "$context.protocol"
141+
requestId = "$context.requestId"
142+
requestTime = "$context.requestTime"
143+
responseLength = "$context.responseLength"
144+
routeKey = "$context.routeKey"
145+
stage = "$context.stage"
146+
status = "$context.status"
147+
error = {
148+
message = "$context.error.message"
149+
responseType = "$context.error.responseType"
150+
}
151+
identity = {
152+
sourceIP = "$context.identity.sourceIp"
153+
}
154+
integration = {
155+
error = "$context.integration.error"
156+
integrationStatus = "$context.integration.integrationStatus"
157+
}
158+
}
153159
})
154160
}
155161

@@ -180,7 +186,6 @@ module "step_function" {
180186
role_name = "${local.name}-step-function"
181187
trusted_entities = [
182188
"apigateway.amazonaws.com",
183-
"lambda.amazonaws.com",
184189
]
185190

186191
attach_policies_for_integrations = true
@@ -220,9 +225,9 @@ module "lambda_function" {
220225
description = "My awesome lambda function"
221226
handler = "lambda.handler"
222227
runtime = "python3.12"
223-
224-
publish = true
225-
source_path = "lambda.py"
228+
architectures = ["arm64"]
229+
publish = true
230+
source_path = "lambda.py"
226231

227232
cloudwatch_logs_retention_in_days = 7
228233

examples/complete-http/outputs.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ output "acm_certificate_arn" {
7878
# Integration(s)
7979
################################################################################
8080

81-
# output "integrations" {
82-
# description = "Map of the integrations created and their attributes"
83-
# value = module.api_gateway.integrations
84-
# }
81+
output "integrations" {
82+
description = "Map of the integrations created and their attributes"
83+
value = module.api_gateway.integrations
84+
}
8585

8686
################################################################################
8787
# Route(s)
8888
################################################################################
8989

90-
# output "routes" {
91-
# description = "Map of the routes created and their attributes"
92-
# value = module.api_gateway.routes
93-
# }
90+
output "routes" {
91+
description = "Map of the routes created and their attributes"
92+
value = module.api_gateway.routes
93+
}
9494

9595
################################################################################
9696
# Stage

examples/vpc-link-http/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,28 @@ Note that this example may create resources which cost money. Run `terraform des
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.37 |
24-
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
25-
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2624

2725
## Providers
2826

2927
| Name | Version |
3028
|------|---------|
3129
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.37 |
32-
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
33-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
3430

3531
## Modules
3632

3733
| Name | Source | Version |
3834
|------|--------|---------|
3935
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 9.0 |
40-
| <a name="module_alb_security_group"></a> [alb\_security\_group](#module\_alb\_security\_group) | terraform-aws-modules/security-group/aws | ~> 5.0 |
4136
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | ../../ | n/a |
4237
| <a name="module_api_gateway_security_group"></a> [api\_gateway\_security\_group](#module\_api\_gateway\_security\_group) | terraform-aws-modules/security-group/aws | ~> 5.0 |
43-
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 4.0 |
38+
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 7.0 |
4439
| <a name="module_lambda_security_group"></a> [lambda\_security\_group](#module\_lambda\_security\_group) | terraform-aws-modules/security-group/aws | ~> 5.0 |
4540
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |
4641

4742
## Resources
4843

4944
| Name | Type |
5045
|------|------|
51-
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
52-
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
5346
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
5447

5548
## Inputs
@@ -60,6 +53,7 @@ No inputs.
6053

6154
| Name | Description |
6255
|------|-------------|
56+
| <a name="output_acm_certificate_arn"></a> [acm\_certificate\_arn](#output\_acm\_certificate\_arn) | The ARN of the certificate |
6357
| <a name="output_api_arn"></a> [api\_arn](#output\_api\_arn) | The ARN of the API |
6458
| <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 |
6559
| <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 |
@@ -73,6 +67,8 @@ No inputs.
7367
| <a name="output_domain_name_target_domain_name"></a> [domain\_name\_target\_domain\_name](#output\_domain\_name\_target\_domain\_name) | The target domain name |
7468
| <a name="output_integrations"></a> [integrations](#output\_integrations) | Map of the integrations created and their attributes |
7569
| <a name="output_routes"></a> [routes](#output\_routes) | Map of the routes created and their attributes |
70+
| <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 |
71+
| <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 |
7672
| <a name="output_stage_arn"></a> [stage\_arn](#output\_stage\_arn) | The stage ARN |
7773
| <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 |
7874
| <a name="output_stage_id"></a> [stage\_id](#output\_stage\_id) | The stage identifier |

examples/vpc-link-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/vpc-link-http/main.tf

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ locals {
1111
vpc_cidr = "10.0.0.0/16"
1212
azs = slice(data.aws_availability_zones.available.names, 0, 3)
1313

14-
package_url = "https://raw.githubusercontent.com/terraform-aws-modules/terraform-aws-lambda/master/examples/fixtures/python3.8-zip/existing_package.zip"
15-
downloaded = "downloaded_package_${md5(local.package_url)}.zip"
16-
1714
tags = {
1815
Example = local.name
1916
GithubRepo = "terraform-aws-apigateway-v2"
@@ -28,16 +25,17 @@ locals {
2825
module "api_gateway" {
2926
source = "../../"
3027

31-
name = local.name
32-
description = "HTTP API Gateway with VPC links"
33-
protocol_type = "HTTP"
34-
28+
# API
3529
cors_configuration = {
3630
allow_headers = ["content-type", "x-amz-date", "authorization", "x-api-key", "x-amz-security-token", "x-amz-user-agent"]
3731
allow_methods = ["*"]
3832
allow_origins = ["*"]
3933
}
4034

35+
description = "HTTP API Gateway with VPC links"
36+
name = local.name
37+
38+
# Routes & Integration(s)
4139
integrations = {
4240
"ANY /" = {
4341
lambda_arn = module.lambda_function.lambda_function_arn
@@ -58,6 +56,7 @@ module "api_gateway" {
5856
}
5957
}
6058

59+
# VPC Link
6160
vpc_links = {
6261
my-vpc = {
6362
name = local.name
@@ -84,15 +83,10 @@ module "vpc" {
8483
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)]
8584
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 48)]
8685

87-
tags = local.tags
88-
}
86+
enable_nat_gateway = true
87+
single_nat_gateway = true
8988

90-
################################
91-
# Supporting resources
92-
################################
93-
94-
resource "random_pet" "this" {
95-
length = 2
89+
tags = local.tags
9690
}
9791

9892
module "api_gateway_security_group" {
@@ -111,7 +105,6 @@ module "api_gateway_security_group" {
111105
tags = local.tags
112106
}
113107

114-
115108
module "alb" {
116109
source = "terraform-aws-modules/alb/aws"
117110
version = "~> 9.0"
@@ -121,6 +114,7 @@ module "alb" {
121114
vpc_id = module.vpc.vpc_id
122115
subnets = module.vpc.public_subnets
123116

117+
# Disable for example
124118
enable_deletion_protection = false
125119

126120
security_group_ingress_rules = {
@@ -140,49 +134,34 @@ module "alb" {
140134
}
141135
}
142136

143-
tags = local.tags
144-
}
145-
146-
module "alb_security_group" {
147-
source = "terraform-aws-modules/security-group/aws"
148-
version = "~> 5.0"
149-
150-
name = "${local.name}-alb"
151-
description = "ALB for example usage"
152-
vpc_id = module.vpc.vpc_id
153-
154-
ingress_cidr_blocks = ["0.0.0.0/0"]
155-
ingress_rules = ["http-80-tcp"]
156-
157-
egress_rules = ["all-all"]
158-
159-
tags = local.tags
160-
}
161-
162-
163-
resource "null_resource" "download_package" {
164-
triggers = {
165-
downloaded = local.downloaded
137+
listeners = {
138+
default = {
139+
port = 80
140+
protocol = "HTTP"
141+
fixed_response = {
142+
content_type = "text/plain"
143+
message_body = "Hello, World!"
144+
status_code = "200"
145+
}
146+
}
166147
}
167148

168-
provisioner "local-exec" {
169-
command = "curl -L -o ${local.downloaded} ${local.package_url}"
170-
}
149+
tags = local.tags
171150
}
172151

173152
module "lambda_function" {
174153
source = "terraform-aws-modules/lambda/aws"
175-
version = "~> 4.0"
154+
version = "~> 7.0"
176155

177156
function_name = local.name
178157
description = "My awesome lambda function"
179-
handler = "index.lambda_handler"
180-
runtime = "python3.8"
181-
182-
publish = true
158+
handler = "lambda.handler"
159+
runtime = "python3.12"
160+
architectures = ["arm64"]
161+
publish = true
162+
source_path = "lambda.py"
183163

184-
create_package = false
185-
local_existing_package = local.downloaded
164+
cloudwatch_logs_retention_in_days = 7
186165

187166
attach_network_policy = true
188167
vpc_subnet_ids = module.vpc.private_subnets
@@ -191,7 +170,7 @@ module "lambda_function" {
191170
allowed_triggers = {
192171
AllowExecutionFromAPIGateway = {
193172
service = "apigateway"
194-
source_arn = "${module.api_gateway.api_execution_arn}/*/*/*"
173+
source_arn = "${module.api_gateway.api_execution_arn}/*/*"
195174
}
196175
}
197176

examples/vpc-link-http/outputs.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ output "domain_name_hosted_zone_id" {
6565
value = module.api_gateway.domain_name_hosted_zone_id
6666
}
6767

68+
################################################################################
69+
# Domain - Certificate
70+
################################################################################
71+
72+
output "acm_certificate_arn" {
73+
description = "The ARN of the certificate"
74+
value = module.api_gateway.acm_certificate_arn
75+
}
76+
6877
################################################################################
6978
# Integration(s)
7079
################################################################################
@@ -107,6 +116,20 @@ output "stage_invoke_url" {
107116
value = module.api_gateway.stage_invoke_url
108117
}
109118

119+
################################################################################
120+
# Stage Access Logs - Log Group
121+
################################################################################
122+
123+
output "stage_access_logs_cloudwatch_log_group_name" {
124+
description = "Name of cloudwatch log group created"
125+
value = module.api_gateway.stage_access_logs_cloudwatch_log_group_name
126+
}
127+
128+
output "stage_access_logs_cloudwatch_log_group_arn" {
129+
description = "Arn of cloudwatch log group created"
130+
value = module.api_gateway.stage_access_logs_cloudwatch_log_group_arn
131+
}
132+
110133
################################################################################
111134
# VPC Link
112135
################################################################################

examples/vpc-link-http/versions.tf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,5 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 5.37"
88
}
9-
random = {
10-
source = "hashicorp/random"
11-
version = ">= 2.0"
12-
}
13-
null = {
14-
source = "hashicorp/null"
15-
version = ">= 2.0"
16-
}
179
}
1810
}

0 commit comments

Comments
 (0)