Skip to content

Commit cda8cbd

Browse files
author
Manasvi Jain
committed
Final update of the pattern
1 parent 384971b commit cda8cbd

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
=======
8+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-rest-api-vpclink
9+
10+
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.
11+
12+
## Requirements
13+
14+
* [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.
15+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
16+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
17+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
18+
19+
## Deployment Instructions
20+
21+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
22+
```
23+
git clone https://github.com/aws-samples/serverless-patterns
24+
```
25+
2. Change directory to the pattern directory:
26+
```
27+
cd apigw-websocket-api-vpclink
28+
```
29+
30+
3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
31+
```
32+
sam deploy -g
33+
```
34+
4. During the prompts:
35+
* Enter a stack name
36+
* Select the desired AWS Region
37+
* Enter the DNS name for the internal NLB (NlbInternalDns)
38+
* Enter the ARN for the internal NLB (NlbInternalArn)
39+
* Allow SAM to create roles with the required permissions if needed.
40+
41+
Once you have run guided mode once, you can use `sam deploy` in future to use these defaults.
42+
43+
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
44+
45+
## Testing
46+
47+
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.
48+
49+
1. [Install NPM](https://www.npmjs.com/get-npm).
50+
51+
2. Install wscat:
52+
```
53+
$ npm install -g wscat
54+
```
55+
56+
3. Connect to your WebSocketURL by executing the following command:
57+
```
58+
$ wscat -c <YOUR WEBSOCKET URL>
59+
```
60+
61+
## Cleanup
62+
63+
1. Delete the stack
64+
```
65+
aws cloudformation delete-stack --stack-name <YOUR STACK NAME>
66+
```
67+
68+
2. Confirm the stack has been deleted
69+
```
70+
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'<YOUR STACK NAME>')].StackStatus"
71+
```
72+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
"bio": "Associate Partner Solutions Architect at AWS",
35+
36+
"linkedin": "https://www.linkedin.com/in/manasvi-jain-36b9941a3/"
37+
},
38+
{
39+
"name": "Umang Aggarwal",
40+
"image": "https://avatars.githubusercontent.com/Umang071",
41+
"bio": "Technical Account Manager @ AWS",
42+
43+
"linkedin": "https://www.linkedin.com/in/umangaggarwal"
44+
}
45+
]
46+
}
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 with a VPC link.
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 with a VPC link.
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)