Provisioning and testing the Lambda (AWS CLI)

Follow this steps to deploy and invoke the Lambda. You may follow Chapter 1Getting Started with Serverless Computing on AWS and use CloudFormation for Lambda provisioning:

  1. Run mvn clean package from inside the Lambda project root folder to create the Uber JAR.
  2. Upload the Uber JAR to S3:
aws s3 cp 
target/lambda-sqs-sdk-receive-send-batch-0.0.1-SNAPSHOT.jar
    s3://serverless-cookbook/lambda-sqs-sdk-receive-send-batch-0.0.1-SNAPSHOT.jar 
--profile admin
  1. Create a role for the Lambda with an appropriate trust relationship definition:
aws iam create-role 
--role-name lambda-sqs-sdk-receive-send-batch-role
--assume-role-policy-document file://iam-role-trust-relationship.txt
--profile admin

The trust document, iam-role-trust-relationship.txt, is defined as follows:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
  1. Create a policy for basic logging permissions and attach it to the role.
  2. Create a policy for required SQS permissions and attach it to the role.

The policy document with required SQS permissions is shown as follows:

{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"sqs:ReceiveMessage",
"sqs:SendMessage",
"sqs:SendMessageBatch",
"sqs:DeleteQueue"
],
"Resource":[
"arn:aws:sqs:*:*:*"
]
}
]
}
  1. Create the Lambda function, as follows:
aws lambda create-function 
--function-name lambda-sqs-sdk-receive-send-batch
--runtime java8
--role arn:aws:iam::<account id>:role/lambda-sqs-sdk-receive-send-batch-role
--handler tech.heartin.books.serverlesscookbook.LambdaSqsSdkReceiveSendBatchHandler::handleRequest
--code S3Bucket=serverless-cookbook,S3Key=lambda-sqs-sdk-receive-send-batch-0.0.1-SNAPSHOT.jar
--timeout 15
--memory-size 512
--region us-east-1
--profile admin
  1. Invoke the Lambda function, as follows:
aws lambda invoke 
--invocation-type RequestResponse
--function-name lambda-sqs-sdk-receive-send-batch
--log-type Tail
--payload file://payload.json
--region us-east-1
--profile admin
outputfile.txt

The payload file should correspond to our input domain object (Request.java) as follows:

{
"inputQueueURL" : "https://queue.amazonaws.com/855923912133/my-input-queue",
"outputQueueURL" : "https://queue.amazonaws.com/855923912133/my-output-queue",
"maxMessagesToReceive" : 5,
"delay": 10
}

If the aws lambda invoke command is successful, you should see a success message in the output file, outputfile.txt (assuming you return a success message from the Lambda similar to the code files).

  1. Verify the invocation by retrieving the message from the queue:
aws sqs receive-message 
--queue-url https://queue.amazonaws.com/855923912133/my-output-queue
--max-number-of-messages 7
--profile admin

If successful, you should get between zero and seven (maximum) messages in a single receive-message call. Even if you have more messages than this value in the queue, the exact number of messages returned is not guaranteed, but the maximum returned will be as per the value of the max-number-of-messages property.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.227.4