You can also go through the Your first AWS Lambda recipe in Chapter 1, Getting Started with Serverless Computing on AWS, and use CloudFormation for Lambda provisioning. Go through the following steps to deploy and invoke the Lambda function:
- Run mvn clean package from inside the Lambda project root folder to create the Uber JAR.
- Upload the Uber JAR to S3.
- Create a role for the Lambda with an appropriate trust relationship definition.
- Create a policy for basic logging permissions and attach it to the role.
- Create a policy for the required Kinesis permissions and attach it to the role by going through the following steps:
- 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:*:*:*"
]
}
]
}
-
- Save the file as lambda-kinesis-producer-permissions.txt.
- Create the policy and attach it to the role.
- 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
- 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:
-
- 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:
-
- 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.
- Finally, you need to decode the Base64-encoded data using the following code: