Skip to content

Commit cacb480

Browse files
committed
chore: stash work in progress
1 parent 7016f1d commit cacb480

File tree

12 files changed

+652
-7
lines changed

12 files changed

+652
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ terraform.rc
3030

3131
# Zip archive
3232
*.zip
33+
builds

examples/complete-http/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ 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

4040
| Name | Source | Version |
4141
|------|--------|---------|
4242
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
43-
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | ../../ | |
43+
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | ../../ | n/a |
4444
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 2.0 |
4545
| <a name="module_step_function"></a> [step\_function](#module\_step\_function) | terraform-aws-modules/step-functions/aws | ~> 2.0 |
4646

@@ -64,7 +64,7 @@ Note that this example may create resources which cost money. Run `terraform des
6464

6565
| Name | Description | Type | Default | Required |
6666
|------|-------------|------|---------|:--------:|
67-
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | Custom domain name to use on API Gateway endpoint | `string` | n/a | yes |
67+
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | Custom domain name to use on API Gateway endpoint | `string` | `"terraform-aws-modules.modules.tf"` | no |
6868

6969
## Outputs
7070

examples/complete-http/terraform.tfvars.example

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/complete-http/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
variable "domain_name" {
22
description = "Custom domain name to use on API Gateway endpoint"
33
type = string
4+
default = "terraform-aws-modules.modules.tf"
45
}

examples/websocket/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.
4+
5+
6+
## Usage
7+
8+
To run this example you need to execute:
9+
10+
```bash
11+
$ terraform init
12+
$ terraform plan
13+
$ terraform apply
14+
```
15+
16+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.
17+
18+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
19+
## Requirements
20+
21+
| Name | Version |
22+
|------|---------|
23+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
24+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.35 |
25+
26+
## Providers
27+
28+
| Name | Version |
29+
|------|---------|
30+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 3.61.0 |
31+
| <a name="provider_random"></a> [random](#provider\_random) | 3.1.0 |
32+
33+
## Modules
34+
35+
| Name | Source | Version |
36+
|------|--------|---------|
37+
| <a name="module_api_gateway"></a> [api\_gateway](#module\_api\_gateway) | ../../ | n/a |
38+
| <a name="module_connect_lambda_function"></a> [connect\_lambda\_function](#module\_connect\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 2 |
39+
| <a name="module_disconnect_lambda_function"></a> [disconnect\_lambda\_function](#module\_disconnect\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 2 |
40+
| <a name="module_dynamodb_table"></a> [dynamodb\_table](#module\_dynamodb\_table) | terraform-aws-modules/dynamodb-table/aws | ~> 1 |
41+
| <a name="module_send_message_lambda_function"></a> [send\_message\_lambda\_function](#module\_send\_message\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 2 |
42+
43+
## Resources
44+
45+
| Name | Type |
46+
|------|------|
47+
| [aws_api_gateway_account.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_account) | resource |
48+
| [aws_cloudwatch_log_group.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
49+
| [aws_iam_role.cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
50+
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
51+
52+
## Inputs
53+
54+
No inputs.
55+
56+
## Outputs
57+
58+
| Name | Description |
59+
|------|-------------|
60+
| <a name="output_wss"></a> [wss](#output\_wss) | n/a |
61+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
const AWS = require("aws-sdk");
5+
6+
const ddb = new AWS.DynamoDB.DocumentClient({
7+
apiVersion: "2012-08-10",
8+
region: process.env.AWS_REGION,
9+
});
10+
11+
exports.handler = async (event) => {
12+
const putParams = {
13+
TableName: process.env.TABLE_NAME,
14+
Item: {
15+
connectionId: event.requestContext.connectionId,
16+
},
17+
};
18+
19+
try {
20+
await ddb.put(putParams).promise();
21+
} catch (err) {
22+
return {
23+
statusCode: 500,
24+
body: "Failed to connect: " + JSON.stringify(err),
25+
};
26+
}
27+
28+
return { statusCode: 200, body: "Connected." };
29+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
// https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-route-keys-connect-disconnect.html
5+
// The $disconnect route is executed after the connection is closed.
6+
// The connection can be closed by the server or by the client. As the connection is already closed when it is executed,
7+
// $disconnect is a best-effort event.
8+
// API Gateway will try its best to deliver the $disconnect event to your integration, but it cannot guarantee delivery.
9+
10+
const AWS = require("aws-sdk");
11+
12+
const ddb = new AWS.DynamoDB.DocumentClient({
13+
apiVersion: "2012-08-10",
14+
region: process.env.AWS_REGION,
15+
});
16+
17+
exports.handler = async (event) => {
18+
const deleteParams = {
19+
TableName: process.env.TABLE_NAME,
20+
Key: {
21+
connectionId: event.requestContext.connectionId,
22+
},
23+
};
24+
25+
try {
26+
await ddb.delete(deleteParams).promise();
27+
} catch (err) {
28+
return {
29+
statusCode: 500,
30+
body: "Failed to disconnect: " + JSON.stringify(err),
31+
};
32+
}
33+
34+
return { statusCode: 200, body: "Disconnected." };
35+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2018-2020Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
const AWS = require("aws-sdk");
5+
6+
const ddb = new AWS.DynamoDB.DocumentClient({
7+
apiVersion: "2012-08-10",
8+
region: process.env.AWS_REGION,
9+
});
10+
11+
const { TABLE_NAME } = process.env;
12+
13+
exports.handler = async (event) => {
14+
let connectionData;
15+
16+
try {
17+
connectionData = await ddb
18+
.scan({ TableName: TABLE_NAME, ProjectionExpression: "connectionId" })
19+
.promise();
20+
} catch (e) {
21+
return { statusCode: 500, body: e.stack };
22+
}
23+
24+
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
25+
apiVersion: "2018-11-29",
26+
endpoint:
27+
event.requestContext.domainName + "/" + event.requestContext.stage,
28+
});
29+
30+
const postData = JSON.parse(event.body).data;
31+
32+
const postCalls = connectionData.Items.map(async ({ connectionId }) => {
33+
try {
34+
await apigwManagementApi
35+
.postToConnection({ ConnectionId: connectionId, Data: postData })
36+
.promise();
37+
} catch (e) {
38+
if (e.statusCode === 410) {
39+
console.log(`Found stale connection, deleting ${connectionId}`);
40+
await ddb
41+
.delete({ TableName: TABLE_NAME, Key: { connectionId } })
42+
.promise();
43+
} else {
44+
throw e;
45+
}
46+
}
47+
});
48+
49+
try {
50+
await Promise.all(postCalls);
51+
} catch (e) {
52+
return { statusCode: 500, body: e.stack };
53+
}
54+
55+
return { statusCode: 200, body: "Data sent." };
56+
};

0 commit comments

Comments
 (0)