# Running Rust on AWS Lambda. Part 2

In [previous article](https://paramako.com/running-rust-on-aws-lambda-part-1) we`ve created a simple code example of **AWS lambda** **SQS** consumer.

In this one, we will learn how to configure and run it on **AWS**.

This article is not about the CI/CD best practises. I just want to show you in a simple examples how to run and test your code on **AWS lambda**.

Let's go!

## Create Role
Firstly, we need to create a role, that will allow our lambda functions to pop data from **SQS**.

Enter the **AWS console** roles web interface and click on the **Create role** button.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659007875758/abthWf885.png align="left")

Choose **AWS service** as trusted entity and **lambdas** as a common use case, then click on the **Next** button
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659008182066/KVH5s0BXI.png align="left")

Attach **AWSLambdaSQSQueueExecutionRole** policy to your new role and click on the **Next** button
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659008600624/rLrIJY9tO.png align="left")
Name your role as **TestRustLambdaRole**
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659008703274/ap9hJrlhm.png align="left")
Click on the **Create Role** button.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659008753399/eu_omrIjE.png align="left")

Perfect! Our new role is created successfully!
## Create AWS lambda function

Enter the **AWS console** lambdas web interface and click on the **Create function** button.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659007652081/a84ZULHV-.png align="left")

Then just fill in the **Basic information** block this way

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659007738461/07BYn5wNB.png align="left")

Choose **TestRustLambdaRole** as a execution role of our lambda function
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659009195250/QZgDp9xDi.png align="left")
Click on **Create function** button
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659009234929/R9vDy7-rG.png align="left")

## Test AWS lambda
### Build and deploy
Firstly, we need to build our code.
Everything we need is to run this command
 ```bash
cargo lambda build --release --bin rust-lambda-example --arm64 --output-format zip
```
Our compiled binary will be stored in path: `target/lambda/rust-lambda-example`.

Then in your new lambda function web interface go to the **Code** section and click on `Upload from -> .zip file` and upload our binary
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659010268580/gbekduaek.png align="left")
### Test in AWS console
Now we need to create a test event.

Go to the **Test** section, and name our new test event

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659010558537/nvUkUgWT9.png align="left")
 Then add the SQS event sample below to Event JSON block
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659010742507/40YD962UG.png align="left")
and click on the **Save** button
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659010786228/7X8gBMfck.png align="left")
```json
{
    "Records": [
      {
        "messageId" : "MessageID_1",
        "receiptHandle" : "MessageReceiptHandle",
        "body" : "Hello from message 1!",
        "md5OfBody" : "fce0ea8dd236ccb3ed9b37dae260836f",
        "md5OfMessageAttributes" : "582c92c5c5b6ac403040a4f3ab3115c9",
        "eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:SQSQueue",
        "eventSource": "aws:sqs",
        "awsRegion": "us-west-2",
        "attributes" : {
          "ApproximateReceiveCount" : "2",
          "SentTimestamp" : "1520621625029",
          "SenderId" : "AROAIWPX5BD2BHG722MW4:sender",
          "ApproximateFirstReceiveTimestamp" : "1520621634884"
        },
        "messageAttributes" : {
          "Attribute3" : {
            "binaryValue" : "MTEwMA==",
            "stringListValues" : ["abc", "123"],
            "binaryListValues" : ["MA==", "MQ==", "MA=="],
            "dataType" : "Binary"
          },
          "Attribute2" : {
            "stringValue" : "123",
            "stringListValues" : [ ],
            "binaryListValues" : ["MQ==", "MA=="],
            "dataType" : "Number"
          },
          "Attribute1" : {
            "stringValue" : "AttributeValue1",
            "stringListValues" : [ ],
            "binaryListValues" : [ ],
            "dataType" : "String"
          }
        }
      },
      {
        "messageId" : "MessageID_2",
        "receiptHandle" : "MessageReceiptHandle",
        "body" : "Hello from message 2!",
        "md5OfBody" : "fce0ea8dd236ccb3ed9b37dae260836f",
        "md5OfMessageAttributes" : "582c92c5c5b6ac403040a4f3ab3115c9",
        "eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:SQSQueue",
        "eventSource": "aws:sqs",
        "awsRegion": "us-west-2",
        "attributes" : {
          "ApproximateReceiveCount" : "2",
          "SentTimestamp" : "1520621625029",
          "SenderId" : "AROAIWPX5BD2BHG722MW4:sender",
          "ApproximateFirstReceiveTimestamp" : "1520621634884"
        },
        "messageAttributes" : {
          "Attribute3" : {
            "binaryValue" : "MTEwMA==",
            "stringListValues" : ["abc", "123"],
            "binaryListValues" : ["MA==", "MQ==", "MA=="],
            "dataType" : "Binary"
          },
          "Attribute2" : {
            "stringValue" : "123",
            "stringListValues" : [ ],
            "binaryListValues" : ["MQ==", "MA=="],
            "dataType" : "Number"
          },
          "Attribute1" : {
            "stringValue" : "AttributeValue1",
            "stringListValues" : [ ],
            "binaryListValues" : [ ],
            "dataType" : "String"
          }
        }
      }
    ]
  }
```
The next thing we should do to make the code from previous article work is to add environment variable

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659010910118/yjdkahCen.png align="left")

After that let's move back to **Code** section.
Here just choose our new test event and click on the **Test** button
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659011008860/vtLu8O6f5.png align="left")
As you can see, the output is the same as in local test from a first article. Our lambda is working!
```bash
Function Logs
START RequestId: 56cbfd84-3601-4c13-9da6-84b94aed972a Version: $LATEST
test
Hello from message 1!
Hello from message 2!
END RequestId: 56cbfd84-3601-4c13-9da6-84b94aed972a
REPORT RequestId: 56cbfd84-3601-4c13-9da6-84b94aed972a	Duration: 0.99 ms	Billed Duration: 19 ms	Memory Size: 128 MB	Max Memory Used: 13 MB	Init Duration: 17.17 ms
```
## Connect lambda with SQS events
Create SQS queue with default settings by filling in the name

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659011483428/Z5RhibSWM.png align="left")
and pressing **Create queue** button
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659011679605/WtpqBZtKN.png align="left")

Next, go back to your lambda function interface and add a new trigger

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659011822889/EF940N1L8.png align="left")

Select **SQS** as a source

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659011904018/0BJ846LiC.png align="left")
and our new created queue

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659011940803/hlPDaXB37.png align="left")
Then add this trigger to our lambda function

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659012043982/DX8Ez9LzC.png align="left")

To test that everything works as expected let's decrease trigger batch size to 1

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659012397639/JPRVk2Npr.png align="left")
 

and then move back to your SQS web interface and click on 
**send and receive messages** button

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659012434539/x-SbgV0lz.png align="left")

And push a test message to the queue

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659012267836/3EoFw3peS.png align="left")

Then go to the **Monitor** -> **logs** section in your lambda function interface

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659012511866/xAGbE_jSl.png align="left")

Here you can see that our lambda handled the new message

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659012542422/pGcvY-nCc.png align="left")

