Follow these steps to deploy and invoke the Lambda. You may follow Chapter 1, Getting Started with Serverless Computing on AWS and use CloudFormation for Lambda provisioning:
- Run mvn clean package from inside the Lambda project root folder to create the Uber JAR.
- Upload the Uber JAR to S3:
aws s3 cp
target/lambda-sqs-sdk-create-send-0.0.1-SNAPSHOT.jar
s3://serverless-cookbook/lambda-sqs-sdk-create-send-0.0.1-SNAPSHOT.jar
--profile admin
- Create a role for the Lambda with an appropriate trust relationship definition:
aws iam create-role
--role-name lambda-sqs-create-send-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"
}
]
}
- Create a policy for basic logging permissions and attach it to the role.
Create the policy document as follows:
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource":[
"arn:aws:logs:*:*:*"
]
}
]
}
Save this file as basic-lambda-permissions.txt.
Create the policy as follows:
aws iam create-policy
--policy-name lambda-basic-iam-policy
--policy-document file://basic-lambda-permissions.txt
--profile admin
Attach the policy to the role as follows:
aws iam attach-role-policy
--role-name lambda-sqs-create-send-role
--policy-arn arn:aws:iam::855923912133:policy/lambda-basic-iam-policy
--profile admin
- Create a policy for required SQS permissions, and attach it to the role.
Create the policy document with the required SQS permissions as follows:
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
sqs:CreateQueue,
sqs:GetQueueUrl,
"sqs:SendMessage"
],
"Resource":[
"arn:aws:sqs:*:*:*"
]
}
]
}
Save the file as lambda-sqs-create-send-permissions.txt.
Create the policy and attach it to the role, as we did in the previous step.
- Create the Lambda function as follows:
aws lambda create-function
--function-name lambda-sqs-create-send
--runtime java8
--role arn:aws:iam::<account id>:role/lambda-sqs-create-send-role
--handler tech.heartin.books.serverlesscookbook.LambdaSqsSdkCreateSendHandler::handleRequest
--code S3Bucket=serverless-cookbook,S3Key=lambda-sqs-sdk-create-send-0.0.1-SNAPSHOT.jar
--timeout 15
--memory-size 512
--region us-east-1
--profile admin
- Invoke the Lambda function as follows:
aws lambda invoke
--invocation-type RequestResponse
--function-name lambda-sqs-create-send
--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:
{
"queueName" : "create-send-demo-queue",
"message": "test payload 1"
}
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).
- Verify the invocation by retrieving the message from the queue:
aws sqs receive-message
--queue-url https://queue.amazonaws.com/855923912133/create-send-demo-queue
--profile admin
If successful, you should get the following message back: