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-sns-publish-with-sdk-0.0.1-SNAPSHOT.jar
s3://serverless-cookbook/lambda-sns-publish-with-sdk-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-sns-publish-with-sdk-role
--assume-role-policy-document file://iam-role-trust-relationship.txt
--profile admin

Refer to the previous recipes or the code files for the trust relationship document file, iam-role-trust-relationship.txt.

  1. Create a policy for basic logging permissions and attach it to the role.
  2. Create a policy for required SNS permissions and attach it to the role.

The policy document with required SNS permissions is shown as follows:

{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"sns:Publish"
],
"Resource":[
"arn:aws:sns:*:*:*"
]
}
]
}
  1. Create the Lambda function, as shown here:
aws lambda create-function 
--function-name lambda-sns-publish-with-sdk
--runtime java8
--role arn:aws:iam::855923912133:role/lambda-sns-publish-with-sdk-role
--handler tech.heartin.books.serverlesscookbook.LambdaSnsPublishHandler::handleRequest
--code S3Bucket=serverless-cookbook,S3Key=lambda-sns-publish-with-sdk-0.0.1-SNAPSHOT.jar
--timeout 15
--memory-size 512
--region us-east-1
--profile admin
  1. You can invoke the Lambda as follows:
aws lambda invoke 
--invocation-type RequestResponse
--function-name lambda-sns-publish-with-sdk
--log-type Tail
--payload file://payload.json
--region us-east-1
--profile admin
outputfile.txt

The payload.json file has the following contents:

{
"topicArn" : "arn:aws:sns:us-east-1:<account id>:my-first-sns-topic",
"message": "test payload 1"
}

If successful, you will get notifications to the configured email and SMS.

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

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