Step 2 - Provisioning and testing Lambda (AWS CLI)

You can also go through the Your first AWS Lambda recipe in Chapter 1Getting Started with Serverless Computing on AWS, and use CloudFormation for Lambda provisioning. Go through the following steps to deploy and invoke the Lambda function:

  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 for the Lambda with an appropriate trust relationship definition.
  4. Create a policy for basic logging permissions and attach it to the role.
  5. Create a policy for the required Kinesis permissions and attach it to the role by going through the following steps:
    1. Create the policy document with the required Kinesis permissions using the following code:
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"kinesis:DescribeStream",
"kinesis:PutRecord",
"kinesis:PutRecords"
],
"Resource":[
"arn:aws:kinesis:*:*:*"
]
}
]
}
    1. Save the file as lambda-kinesis-producer-permissions.txt.
    2. Create the policy and attach it to the role.
  1. Create the Lambda function as follows:
aws lambda create-function 
--function-name lambda-kinesis-sdk-write
--runtime java8
--role arn:aws:iam::<account id>:role/lambda_kinesis_write_role
--handler tech.heartin.books.serverlesscookbook.LambdaKinesisSdkWriteHandler::handleRequest
--code S3Bucket=serverless-cookbook,S3Key=lambda-kinesis-sdk-write-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-kinesis-sdk-write
--log-type Tail
--payload file://resources/payload.json
--region us-east-1
--profile admin
outputfile.txt

The payload file should correspond to our input domain object (Request.java), as shown in the following code:

{
"streamName" : "my-first-kinesis-stream",
"partitionKey": "12345",
"payload": "testpayloadfromcli",
"count": 10,
"batchSize" : 5
}

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 messages from the stream using the following steps:

    1. First, retrieve the iterator, as shown in the following code:
aws kinesis get-shard-iterator 
--shard-id shardId-000000000000
--shard-iterator-type TRIM_HORIZON
--stream-name my-first-kinesis-stream
--region us-east-1
--profile admin

If successful, you should get the following message back:

    1. Get the records using the shard iterator, as shown in the following code:
aws kinesis get-records 
--shard-iterator <shard iterator>
--region us-east-1
--profile admin

Replace <shard iterator> with the shard iterator received in the previous step. This should return the following records:

I have not shown all the records here, only the first one. At the end, you will also get the next shard iterator, as shown in the following screenshot:

You may have to call get-records again with the shard iterator received in this step to retrieve further records.

  1. Finally, you need to decode the Base64-encoded data using the following code:
..................Content has been hidden....................

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