19/01/2022 - AWS
Use commands below if you wish to trigger a Lambda function when a new message is published to SNS topic.
// Create lambda function
GOOS=linux CGO_ENABLED=0 go build -ldflags "-s -w" -o lambda/plain/main lambda/plain/main.go
zip lambda.zip lambda/plain/main
aws --profile localstack --endpoint-url http://localhost:4566 lambda create-function \
--function-name test-lambda \
--handler lambda/plain/main \
--runtime go1.x \
--role test-role \
--zip-file fileb://lambda.zip
// Create SNS topic
aws --profile localstack --endpoint-url http://localhost:4566 sns create-topic \
--name test-topic
// Add Lambda permissions for SNS topic
aws --profile localstack --endpoint-url http://localhost:4566 lambda add-permission \
--function-name test-lambda \
--source-arn arn:aws:sns:eu-west-1:000000000000:test-topic \
--statement-id test-lambda \
--action lambda:InvokeFunction \
--principal sns.amazonaws.com
// Subscribe Lambda function to SNS topic
aws --profile localstack --endpoint-url http://localhost:4566 sns subscribe \
--protocol lambda \
--topic-arn arn:aws:sns:eu-west-1:000000000000:test-topic \
--notification-endpoint arn:aws:lambda:eu-west-1:000000000000:function:test-lambda
aws --profile localstack --endpoint-url http://localhost:4566 sns publish \
--topic-arn arn:aws:sns:eu-west-1:000000000000:test-topic \
--message Greeting \
--subject Testing
This is what your Lambda event look like.
map[
Records:[
map[
EventSource:aws:sns
EventSubscriptionArn:arn:aws:sns:eu-west-1:000000000000:test-topic:bf5c62cc-6ad0-4d26-953d-24c46d484188
EventVersion:1.0
Sns:map[
Message:Greeting
MessageAttributes:map[]
MessageId:8e9c2f18-57d3-45f4-abc8-f46d607a5469
Signature:EXAMPLEpH+..
SignatureVersion:1
SigningCertUrl:https://sns.us-east-1.amazonaws.com/SimpleNotificationService-000000000.pem
Subject:Testing
Timestamp:2022-01-30T00:04:11.730Z
TopicArn:arn:aws:sns:eu-west-1:000000000000:test-topic
Type:Notification
UnsubscribeUrl:http://localhost:4566/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:000000000000:test-topic:bf5c62cc-6ad0-4d26-953d-24c46d484188
]
]
]
]