Step 2 - Provisioning and testing Lambda (AWS CLI)

Go through the following steps to deploy and invoke the lambda. You may also follow Your first Lambda with AWS CLI recipe of 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.
  3. Create a role named lambda-invoke-kinesis-event-role for the lambda with an appropriate trust relationship definition.
  4. Create and attach a policy for basic logging permissions and attach it to the role.
  5. Create a policy for the required Kinesis permissions using the following policy document and attach it to the role:
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"kinesis:GetRecords",
"kinesis:GetShardIterator",
"kinesis:DescribeStream",
"kinesis:ListStreams"
],
"Resource":[
"arn:aws:kinesis:*:*:*"
]
}
]
}

Save the file as lambda-kinesis-producer-permissions.txt, create a policy using this file, and attach it to the role.

  1. Create the lambda function as follows:
aws lambda create-function 
--function-name lambda-invoke-kinesis-event
--runtime java8
--role arn:aws:iam::<account id>:role/lambda-invoke-kinesis-event-role
--handler tech.heartin.books.serverlesscookbook.LambdaKinesisEventHandler::handleRequest
--code S3Bucket=serverless-cookbook,S3Key=lambda-invoke-kinesis-event-0.0.1-SNAPSHOT.jar
--timeout 15
--memory-size 512
--region us-east-1
--profile admin
  1. Create an event source mapping for invoking the lambda function, as follows:
aws lambda create-event-source-mapping 
--event-source-arn arn:aws:kinesis:us-east-1:<account id>:stream/kinesis-stream-for-event
--function-name lambda-invoke-kinesis-event
--starting-position LATEST
--batch-size 3
--region us-east-1
--profile admin
  1. Verify the invocation by sending messages to the stream. You can do this by going through the following steps:
    1. Send messages with different payload text, following this:
aws kinesis put-record 
--stream-name kinesis-stream-for-event
--partition-key 12345
--data sampledata01
--region us-east-1
--profile admin
    1. Check the CloudWatch logs.

We can check CloudWatch logs from Management console as follows:

      1. Log in to the Management console and go to the Lambda service.
      2. Click on your Lambda to see its configuration.
      3. Click on the Monitoring tab.
      4. Click on View logs in CloudWatch.Click on a Log Stream and check the logs.
      5. You should see logs similar to those shown in the following screenshot:
..................Content has been hidden....................

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