Skip to content

Commit 0ac8458

Browse files
author
Manasvi Jain
committed
websocket-vpclink
1 parent 384971b commit 0ac8458

File tree

3 files changed

+237
-0
lines changed

3 files changed

+237
-0
lines changed

apigw-websocket-api-vpclink/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Amazon API Gateway Websocket API with VPC Link integration
2+
3+
The SAM template deploys an Amazon API Gateway Websocket API endpoint with a VPC Link integration.
4+
5+
Since Websocket APIs only support VPC Links associated with NLBs (Network Load Balancers), this pattern assumes that an internal NLB already exists in a VPC in the same Region.
6+
7+
<<<<<<< HEAD
8+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/
9+
=======
10+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-rest-api-vpclink
11+
>>>>>>> 79ec4deb37a6cd3b5e7b9877c6fa6e5175854d17
12+
13+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
14+
15+
## Requirements
16+
17+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
18+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
19+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
20+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
21+
22+
## Deployment Instructions
23+
24+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
25+
```
26+
git clone https://github.com/aws-samples/serverless-patterns
27+
```
28+
2. Change directory to the pattern directory:
29+
```
30+
<<<<<<< HEAD
31+
cd apigw-websocket-api-vpclink
32+
=======
33+
cd apigw-rest-api-vpclink
34+
>>>>>>> 79ec4deb37a6cd3b5e7b9877c6fa6e5175854d17
35+
```
36+
3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
37+
```
38+
sam deploy -g
39+
```
40+
4. During the prompts:
41+
* Enter a stack name
42+
* Select the desired AWS Region
43+
* Enter the DNS name for the internal NLB (NlbInternalDns)
44+
* Enter the ARN for the internal NLB (NlbInternalArn)
45+
* Allow SAM to create roles with the required permissions if needed.
46+
47+
Once you have run guided mode once, you can use `sam deploy` in future to use these defaults.
48+
49+
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
50+
51+
## Testing
52+
53+
Once the application is deployed, retrieve the WebSocketURL value from CloudFormation Outputs. To test the WebSocket API, you can use [wscat](https://github.com/websockets/wscat) which is an open-source command line tool.
54+
55+
1. [Install NPM](https://www.npmjs.com/get-npm).
56+
57+
2. Install wscat:
58+
```
59+
$ npm install -g wscat
60+
```
61+
62+
3. Connect to your WebSocketURL by executing the following command:
63+
```
64+
$ wscat -c <YOUR WEBSOCKET URL>
65+
```
66+
67+
4. To test the custom route and its associated function, send a JSON-formatted request like the following example. The Lambda function sends back the value of the "data" key using the callback URL:
68+
```
69+
$ wscat -c <YOUR WEBSOCKET URL>
70+
connected (press CTRL+C to quit)
71+
```
72+
73+
## Cleanup
74+
75+
1. Delete the stack
76+
```
77+
aws cloudformation delete-stack --stack-name <YOUR STACK NAME>
78+
```
79+
80+
2. Confirm the stack has been deleted
81+
```
82+
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'<YOUR STACK NAME>')].StackStatus"
83+
```
84+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"title": "Amazon API Gateway Websocket API with VPC Link integration",
3+
"description": "The SAM template deploys an Amazon API Gateway Websocket API endpoint with a VPC Link integration.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"gitHub": {
8+
"template": {
9+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-websocket-api-vpclink",
10+
"templateURL": "serverless-patterns/apigw-websocket-api-vpclink",
11+
"projectFolder": "apigw-websocket-api-vpclink",
12+
"templateFile": "template.yaml"
13+
}
14+
},
15+
"deploy": {
16+
"text": [
17+
"sam deploy"
18+
]
19+
},
20+
"testing": {
21+
"text": [
22+
"See the GitHub repo for detailed testing instructions."
23+
]
24+
},
25+
"cleanup": {
26+
"text": [
27+
"Delete the stack: <code>sam delete</code>."
28+
]
29+
},
30+
"authors": [
31+
{
32+
"name": "Manasvi Jain",
33+
"image": "https://avatars.githubusercontent.com/u/56217984?v=4",
34+
<<<<<<< HEAD
35+
"bio": "Associate Partner Solutions Architect at AWS",
36+
=======
37+
"bio": "Cloud Engineer at AWS",
38+
>>>>>>> 79ec4deb37a6cd3b5e7b9877c6fa6e5175854d17
39+
"linkedin": "https://www.linkedin.com/in/manasvi-jain-36b9941a3/"
40+
},
41+
{
42+
"name": "Umang Aggarwal",
43+
"image": "https://avatars.githubusercontent.com/Umang071",
44+
<<<<<<< HEAD
45+
"bio": "Technical Account Manager @ AWS",
46+
=======
47+
"bio": "Cloud Engineer II @ AWS",
48+
>>>>>>> 79ec4deb37a6cd3b5e7b9877c6fa6e5175854d17
49+
"linkedin": "https://www.linkedin.com/in/umangaggarwal"
50+
}
51+
]
52+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: An Amazon API Gateway WebSocket API and an AWS Lambda function.
4+
5+
Parameters:
6+
NlbInternalDns:
7+
Type: String
8+
NlbInternalArn:
9+
Type: String
10+
11+
Resources:
12+
# API Gateway WebSocket API
13+
MyWebSocketApi:
14+
Type: AWS::ApiGatewayV2::Api
15+
Properties:
16+
Name: !Ref AWS::StackName
17+
Description: An Amazon API Gateway WebSocket API and an AWS Lambda function.
18+
ProtocolType: WEBSOCKET
19+
RouteSelectionExpression: $request.body.action
20+
21+
ConnectRoute:
22+
Type: AWS::ApiGatewayV2::Route
23+
Properties:
24+
ApiId: !Ref MyWebSocketApi
25+
RouteKey: $connect
26+
Target: !Sub integrations/${ConnectIntegration}
27+
28+
MessageRoute:
29+
Type: AWS::ApiGatewayV2::Route
30+
Properties:
31+
ApiId: !Ref MyWebSocketApi
32+
RouteKey: $default
33+
Target: !Sub integrations/${MessageIntegration}
34+
35+
DisconnectRoute:
36+
Type: AWS::ApiGatewayV2::Route
37+
Properties:
38+
ApiId: !Ref MyWebSocketApi
39+
RouteKey: $disconnect
40+
Target: !Sub integrations/${DisconnectIntegration}
41+
42+
ConnectIntegration:
43+
Type: AWS::ApiGatewayV2::Integration
44+
Properties:
45+
ApiId: !Ref MyWebSocketApi
46+
IntegrationType: HTTP_PROXY
47+
IntegrationMethod: ANY
48+
IntegrationUri: !Sub http://${NlbInternalDns}/connect
49+
ConnectionType: VPC_LINK
50+
ConnectionId: !Ref VPCLinkRestNlbInternal
51+
52+
MessageIntegration:
53+
Type: AWS::ApiGatewayV2::Integration
54+
Properties:
55+
ApiId: !Ref MyWebSocketApi
56+
IntegrationType: HTTP_PROXY
57+
IntegrationMethod: ANY
58+
IntegrationUri: !Sub http://${NlbInternalDns}/message
59+
ConnectionType: VPC_LINK
60+
ConnectionId: !Ref VPCLinkRestNlbInternal
61+
62+
DisconnectIntegration:
63+
Type: AWS::ApiGatewayV2::Integration
64+
Properties:
65+
ApiId: !Ref MyWebSocketApi
66+
IntegrationType: HTTP_PROXY
67+
IntegrationMethod: ANY
68+
IntegrationUri: !Sub http://${NlbInternalDns}/disconnect
69+
ConnectionType: VPC_LINK
70+
ConnectionId: !Ref VPCLinkRestNlbInternal
71+
72+
Deployment:
73+
Type: AWS::ApiGatewayV2::Deployment
74+
DependsOn:
75+
- ConnectRoute
76+
- DisconnectRoute
77+
- MessageRoute
78+
Properties:
79+
ApiId: !Ref MyWebSocketApi
80+
81+
Stage:
82+
Type: AWS::ApiGatewayV2::Stage
83+
Properties:
84+
StageName: Prod
85+
ApiId: !Ref MyWebSocketApi
86+
DeploymentId: !Ref Deployment
87+
88+
VPCLinkRestNlbInternal:
89+
Type: AWS::ApiGateway::VpcLink
90+
Properties:
91+
Name: VPCLinkRestNlbInternal
92+
TargetArns:
93+
- !Ref NlbInternalArn
94+
95+
Outputs:
96+
97+
# API Gateway endpoint to be used during tests
98+
AppApiEndpoint:
99+
Description: API Endpoint
100+
Value: !Sub https://${MyWebSocketApi}.execute-api.${AWS::Region}.amazonaws.com/Prod
101+

0 commit comments

Comments
 (0)