Giter Club home page Giter Club logo

hands-on-with-aws-building-with-sqs-api-lambda's Introduction

DevOps Playground September 2022

Panda

Prerequisites

- Modern browser

-Optional: An API Client such as Postman or Insomnia


1. Create a Queue!

On your AWS management console, search for SQS using the search bar

Once on the page:

  1. Click Create Queue
  2. Select Standard
  3. Name your queue <your_panda_name>_notification_queue
  4. In configuration set receive message wait time to 10 SQS configuration
  5. Disable server-side encryption (we won't be using it for this demo)
  6. Set the following access policy for this demo SQS access policy
  7. Finally click create queue

We now have our very own messaging queue! ๐ŸŽ‰


2. Create a lambda function

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:

  1. Click create function
  2. Name your function <your_panda_name>_order_handler_lambda
  3. Select the configuration below lambda configuration
  4. Select Use existing role and then select panda_order_handler_lambda-role
  5. Click Create function
  6. Click Configuration > environnement variables > add the following env variable:
QUEUE_URL=YOUR QUEUE URL

This role has all the permissions for this lambda


3. Create an API and route

โ€ผ๏ธ Ideally we would want a REST API however, for the purposes of this demonstration, lets keep it simple with a HTTP API โ€ผ๏ธ

  1. Navigate to API Gateway

  2. Click build on HTTP API api build

  3. Name your API <your_panda_name>_api

  4. Click add integration

    • Select lambda from the drop down menu
    • Choose the lambda function you created previously api integration
  5. Click next

  6. Select PUT method and set resource path to /order_status api routes

  7. 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!

OR with cURL

curl -H "Content-Type: application/json" \
  --request PUT \
  --data '{"orderId": "1234993","customerEmail": "<YOUR EMAIL>","orderStatus": "DISPATCHED"}' \
  <INVOKE URL>/order_status

You are doing great!


4. Make your lambda send messages to SQS!

  1. Navigate to the lambda you created in section 2
  2. Copy the code in sendToSQS.js and paste it in the editor on the AWS console. Lets run through the code!
  3. 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. api routes

QueueUrl: 'QUEUE URL HERE',
  1. Click deploy
  2. 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"
}

OR with cURL

curl -H "Content-Type: application/json" \
  --request PUT \
  --data '{"orderId": "1234993","customerEmail": "<YOUR EMAIL>","orderStatus": "DISPATCHED"}' \
  <INVOKE URL>/order_status
  1. Check the response to see if it was successful!
  2. You can view the message in the queue by going to your queue and clicking send and receive messages and then clicking Poll for messages at the bottom.

5. Set up SQS poller lambda

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.

  1. Navigate to the lambda home page
  2. Select Use a blueprint
  3. Search for sqs in the search box
  4. Select Process messages in an SQS queue and click configure poller setup
  5. Name your lambda <your_panda_name>_notification_lambda
  6. Select Use existing role and then select panda_notification_lambda_role
  7. For your SQS trigger select your queue poller setup
  8. Click Create
  9. Copy the code in poller.js and paste it in the editor on the AWS console.

All Done! ๐ŸŽ‰ ๐ŸŽ‰ ๐ŸŽ‰ ๐ŸŽ‰

6. Let's take it for a spin!

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"
}

Check your inbox ๐Ÿ˜‰ ๐Ÿ˜Ž

hands-on-with-aws-building-with-sqs-api-lambda's People

Contributors

khizariqbal93 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.