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.
Choose AWS service as trusted entity and lambdas as a common use case, then click on the Next button
Attach AWSLambdaSQSQueueExecutionRole policy to your new role and click on the Next button
Name your role as TestRustLambdaRole
Click on the Create Role button.
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.
Then just fill in the Basic information block this way
Choose TestRustLambdaRole as a execution role of our lambda function
Click on Create function button
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
Test in AWS console
Now we need to create a test event.
Go to the Test section, and name our new test event
Then add the SQS event sample below to Event JSON block
and click on the Save button
{
"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
After that let's move back to Code section.
Here just choose our new test event and click on the Test button
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
and pressing Create queue button
Next, go back to your lambda function interface and add a new trigger
Select SQS as a source
and our new created queue
Then add this trigger to our lambda function
To test that everything works as expected let's decrease trigger batch size to 1
and then move back to your SQS web interface and click on send and receive messages button
And push a test message to the queue
Then go to the Monitor -> logs section in your lambda function interface
Here you can see that our lambda handled the new message