On your AWS management console, search for SQS using the search bar
Once on the page:
- Click Create Queue
- Select Standard
- Name your queue
<your_panda_name>_notification_queue
- In configuration set receive message wait time to 10
- Disable server-side encryption (we won't be using it for this demo)
- Set the following access policy for this demo
- Finally click create queue
Top tip: Duplicate the previous tab on your browser!
Navigate to the lambda page on the aws console using the search bar
Once on the page:
- Click create function
- Name your function
<your_panda_name>_order_handler_lambda
- Select the configuration below
- Select
Use existing role
and then selectpanda_order_handler_lambda-role
- Click Create function
- Click
Configuration
>environnement variables
> add the following env variable:
QUEUE_URL=YOUR QUEUE URL
This role has all the permissions for this lambda
โผ๏ธ Ideally we would want a REST API however, for the purposes of this demonstration, lets keep it simple with a HTTP APIโผ๏ธ
-
Navigate to API Gateway
-
Name your API
<your_panda_name>_api
-
Click add integration
-
Click next
-
Click next and next again and then create finally
You can now see if it works by making a PUT request to your invoke url followed by the path /order_status
using your favourite API client. You should receive a 200 OK
response with "Hello from Lambda!"
in the body!
curl -H "Content-Type: application/json" \
--request PUT \
--data '{"orderId": "1234993","customerEmail": "<YOUR EMAIL>","orderStatus": "DISPATCHED"}' \
<INVOKE URL>/order_status
You are doing great!
- Navigate to the lambda you created in section 2
- Copy the code in
sendToSQS.js
and paste it in the editor on the AWS console. Lets run through the code! - REMEMBER TO ADD YOUR QUEUE URL TO THE PARAMS OBJECT IN THE CODE
you can get your Queue URL by going to SQS on the console > sqs > queues > click your queue. It should be displayed on the main page.
QueueUrl: 'QUEUE URL HERE',
- Click deploy
- Let's test it out by sending a PUT request like we did before with the following in our request body:
{
"orderId": "1234993",
"customerEmail": "<YOUR EMAIL>",
"orderStatus": "DISPATCHED"
}
curl -H "Content-Type: application/json" \
--request PUT \
--data '{"orderId": "1234993","customerEmail": "<YOUR EMAIL>","orderStatus": "DISPATCHED"}' \
<INVOKE URL>/order_status
- Check the response to see if it was successful!
- You can view the message in the queue by going to your queue and clicking
send and receive messages
and then clickingPoll for messages
at the bottom.
Now lets set up a lambda that will consume messages from the queue and then send an email to the customer updating them about their order.
- Navigate to the lambda home page
- Select
Use a blueprint
- Search for sqs in the search box
- Select
Process messages in an SQS queue
and click configure - Name your lambda
<your_panda_name>_notification_lambda
- Select
Use existing role
and then selectpanda_notification_lambda_role
- For your SQS trigger select your queue
- Click Create
- Copy the code in
poller.js
and paste it in the editor on the AWS console.
If you don't have an API client use this curl command to send a request to the API.
โ ๏ธ remember to put in your email and your API invoke URL
curl -H "Content-Type: application/json" \
--request PUT \
--data '{"orderId": "1234993","customerEmail": "<YOUR EMAIL>","orderStatus": "DISPATCHED"}' \
<INVOKE URL>/order_status
If you have Postman or Insomnia send a PUT request to INVOKE URL/order_status
with following json in the request body:
โ ๏ธ remember to put in your email and your API invoke URL
{
"orderId": "1234993",
"customerEmail": "<YOUR EMAIL>",
"orderStatus": "DISPATCHED"
}