Provisioning and testing the Lambda (AWS CLI)

Follow these 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-create-send-0.0.1-SNAPSHOT.jar
s3://serverless-cookbook/lambda-sqs-sdk-create-send-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-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"
}
]
}
  1. 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
  1. 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.

  1. 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
  1. 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).

  1. 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:

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

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