Configuring S3 bucket with lambda function, lambda function with SQS
Tags: devops, aws, cloud, serverless
Published: 2021-06-04 09:13:01

Hola Developers, Cloud Enthusiasts!!

The most important reason to write this is to help others, because this is not available anywhere if you find it.

In my first summer internship we are working on a Document management system designed using serverless architecture. In this project I was assigned this task of sending notifications of S3 bucket to SQS using a lambda function.

So let's get started.

Step1: First make a bucket no need to do anything else in that just simply make a bucket with standard storage.

Step2: Now go to Lambda function in services, and click on create a function. Following picture will make you understand.

![Creating lambda function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ho41xgoq50ggca1o90wx.png)

Step 3: Create a lambda function by clicking on **Create a function** button.

Step 4: It will show you the following page

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pifp0yfp0svejnle70ft.png)

In this select **Author from scratch** than move down where you will see the following

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nma5r6f1zkttktl69zdd.png)

Here give your function a name of your choice and then select a language under **Runtime** , preferably python.

Then under **Permissions** Choose or create an execution role. Here you will get 3 options as shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nxhoj1ent6scq93pyo3.png)

Step 5: For that go to IAM dashboard shown in the following picture.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxcdok4odv60k96oj6wn.png)

Here now got to **Roles** under **Access management** after that create a role as shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk78pa53tx937bsi43kh.png)

In create role it will ask you to choose a use case in that select lambda.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kyvw7ar3c5vhva411gae.png)

Than click next which will for permissions.

Allow for the following policies for the services to be in a sync.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9ki103pvwl81134tspw.png)

SQS Full access to write the message in the message queue.

S3 Full access to get the notification in lambda which will give to SQS.

CloudWatch Full access to watch the logs, by default logs of lambda are shown in CloudWatch but when these are integrated it is necessary to attach this policy.

AWSLambdaSQSQueueExecutionRole to send data from lambda to SQS.

After assigning these policies go next for tags they are optional, then go for review. Then give the name of this role as shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9k2tbggqegd3f9udr0j.png)

After this click **Create role** and the role is created.

Step 6: Go back to the lambda dashboard and select **Select an existing role** which was made in IAM.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ao4ea23l6es4l43f2vzp.png)

As shown below after selecting Selecting an existing role a dropdown box will appear and select the role which was created in IAM.

After that **Create function**.

Step 7 : It will show like this as shown below, function name under **Configuration under Designer** dropdown it will show these configuration.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfeskgf7fmhet7fo0gs1.png)

Now add the trigger by clicking on the button on the left, can be shown in the above picture.

In the Add **trigger** give S3 and choose the bucket name which was created and under Event type dropdown select put, which means whenever an object is uploaded to the object it will trigger the lambda function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xd4imhfiyi5jglyuxhg6.png)

Then **Add** these triggers to the lambda function. After adding these trigger to the function dashboard will look like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fbx2esixjpwuvywc2yz.png)

Step 8: Add the destination which is SQS.

For that click on **Add destination**, in which configure the destination.

Select source as **Asynchronous invocation**.

Select condition on which condition lambda will throw it to destination, so select **On success**.

After that select **Destination type**, under dropdown select SQS queue.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jg234q49wmw96dfq9tzu.png)

After that select destination, for that navigate to SQS queue dashboard.

Make a queue for receiving messages from lambda.

Click on **Create New queue**.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6owrq92gixlckood8mat.png)

After that give the name of queue, and keep the queue type as **Standard queue**.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtl524nm1ofd34sb97rt.png)

Scroll down and click on **Quick Create-queue** and the queue is ready for receiving messages.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5ympm6sh8f3q0zgdygt.png)

**Go back to lambda dashboard** and in the Destination Selection select the name of the queue which was created. As shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc4wxhz1i3fipz79779x.png)

Now Save the destination and the lambda dashboard will look like these.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9eyyvcsz8k9qc8lhp8r.png)

Now select the lambda function and scroll down where there will a **Function code** window.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbdqr6k0vpt51hgq3cyd.png)

Any changes can be made in the **return** part of the code.Now **Save** and **Test** the function.

Below it will show the execution result as shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skr3ui4n2e4uybgm9ued.png)

Step 9: Now add a file to the S3 bucket and check the SQS queue for messages.

For doing that upload a file to S3 bucket and navigate to the SQS dashboard and refresh it will show you the file uploaded and time stamp, file name, date of which the file was uploaded.

Go in **Queue Actions** dropdown and select View/Delete messages and start polling.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/104qjpzzjbwcjw0eqhto.png)

After that it will show you the message as shown below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06njtlc89hitgog84eos.png)

This is how you configure S3-Lambda-SQS.