Follow these steps to deploy and invoke the Lambda. You may follow Chapter 1, Getting Started with Serverless Computing on AWS and use CloudFormation for Lambda provisioning:
- Run mvn clean package from inside the Lambda project root folder to create the Uber JAR.
- 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
- 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.
- Create a policy for basic logging permissions and attach it to the role.
- 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:*:*:*"
]
}
]
}
- 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
- 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.