This video demonstrates implementation of AWS Kinesis Producer and Consumer using Java + LocalStack setup.
Steps to follow for WebSocket API creation
-
Build API and give
- API name
- Route selection expression —>
request.body.action
-
Add routes
- Add $connect route
- Add $disconnect route
-
Attach integrations
-
$connect
- Type → HTTP
- Method → POST
- URL → <enter api-endpoint here>/connect
-
$disconnect
- Type → HTTP
- Method → POST
- URL → <enter api-endpoint here>/disconnect
-
-
Add stages
- name → prod
- Create and deploy
Edit $disconnect route
Open ‘Integration Request’
- Uncheck ‘Use HTTP Proxy integration'
- Click ‘Save’
-
Edit ‘Request Templates’
- Template Selection Expression →
\$default
→ click ‘check’ button
- Template Selection Expression →
Edit $connect route
Open Integration Request
- Uncheck ‘Use HTTP Proxy integration'
- Click ‘Save’
-
Edit ‘Request Templates’
- Template Selection Expression →
\$default
→ click ‘check’ button
- Template Selection Expression →
Add integration response
-
Open integration response
- Response Selection Expression will be
${integration.response.statuscode}
- Response Selection Expression will be
-
Add Response
- Response Key →
$default
→ click ‘check’ button - Edit ‘Template Selection Expression’ →
\$default
→ click ‘check’ button
- Response Key →
Authorizers
Click ‘Create New Authorizer’
- Name → **
lambda-authorizer
** - Lambda Function →
lambda-authorizer (us-east-1)
-
Identity Sources
- Query String → token
- Click ‘Create’ → ‘Grant & Create’
Attach the authorizer to $connect
route
- Open ‘Route Request’
- Click ‘Pencil icon’ of Authorization
-
Select the configured Lambda Authorizer and save
- If you don’t see the lambda → refresh the page once
Update $connect
Route using AWS CLI
First gather some information
-
API ID
- Note down API ID from URL of the API page of AWS Console
-
RouteId
andIntegrationId
-
Execute below command
aws apigatewayv2 get-routes --api-id bnzlep5vqk
- Note down
RouteId
andTarget
from response of below command, whereRouteKey
is$connect
- ‘Integration Id’ is the alphanumeric part after the slash in
Target
value. Eg:f3m2883
is the ‘Integration Id’ in the"Target": "integrations/f3m2883"
-
Now update the route as follows
aws apigatewayv2 update-route \
--api-id bnzlep5vqk \
--route-id ik7cvjt \
--request-parameters '
{
"route.request.querystring.stocks":{"Required":true}
}'
Update $connect
Integration using AWS CLI
aws apigatewayv2 update-integration \
--integration-id f3m2443 \
--api-id bnzlep5vqk \
--request-parameters 'integration.request.querystring.stocks'='route.request.querystring.stocks'
aws apigatewayv2 update-integration \
--integration-id f3m2443 \
--api-id bnzlep5vqk \
--request-parameters 'integration.request.querystring.connectionId'='context.connectionId'
aws apigatewayv2 update-integration \
--integration-id f3m2443 \
--api-id bnzlep5vqk \
--request-parameters 'integration.request.querystring.userId'='context.authorizer.userId'
aws apigatewayv2 update-integration \
--integration-id f3m2443 \
--api-id bnzlep5vqk \
--request-parameters 'integration.request.querystring.orgId'='context.authorizer.orgId'
You can confirm the updates by fetching the integration details as below
aws apigatewayv2 get-integration \
--integration-id f3m2443 \
--api-id bnzlep5vqk
Update $disconnect
Integration using AWS CLI
First gather some information
API ID
remains same from the previous step-
RouteId
andIntegrationId
-
Execute below command
aws apigatewayv2 get-routes --api-id bnzlep5vqk
- Note down
RouteId
andTarget
from response of below command, whereRouteKey
is$disconnect
- ‘Integration Id’ is the alphanumeric part after the slash in
Target
value. Eg:f3m2883
is the ‘Integration Id’ in the"Target": "integrations/f3m2883"
-
Update $disconnect
Integration using AWS CLI
aws apigatewayv2 update-integration \
--integration-id 478fpcf \
--api-id bnzlep5vqk \
--request-parameters 'integration.request.querystring.connectionId'='context.connectionId'
Deploy & Test
Refresh the console page once and DEPLOY 🚀
To test use below command (make sure the tool is installed)
wscat -c "wss://ojp5zwbo3k.execute-api.ap-southeast-1.amazonaws.com/prod?stocks=aapl,goog"
Configure API
After deployment, the API endpoint should be configured in the backend
project as an environment variable like below
WSAPIENDPOINT | https://bnzlep5vqk.execute-api.ap-southeast-1.amazonaws.com/prod |
---|
| HIH
| Take care
~ @raevilman
Helpful?
If you think this is helpful 🎈
Don't keep it to yourself 🙊
Share it with your lovely followers at twitter 🗽