Running Rust on AWS Lambda. Part 2

Running Rust on AWS Lambda. Part 2

with SQS consumer example

·

4 min read

In previous article 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

Choose AWS service as trusted entity and lambdas as a common use case, then click on the Next button image.png

Attach AWSLambdaSQSQueueExecutionRole policy to your new role and click on the Next button image.png Name your role as TestRustLambdaRole image.png Click on the Create Role button. image.png

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

Then just fill in the Basic information block this way

image.png

Choose TestRustLambdaRole as a execution role of our lambda function image.png Click on Create function button image.png

Test AWS lambda

Build and deploy

Firstly, we need to build our code. Everything we need is to run this command

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

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 Then add the SQS event sample below to Event JSON block image.png and click on the Save button image.png

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

After that let's move back to Code section. Here just choose our new test event and click on the Test button image.png As you can see, the output is the same as in local test from a first article. Our lambda is working!

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 and pressing Create queue button image.png

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

image.png

Select SQS as a source

image.png and our new created queue

image.png Then add this trigger to our lambda function

image.png

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

image.png

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

image.png

And push a test message to the queue

image.png

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

image.png

Here you can see that our lambda handled the new message

image.png