3 Answers
The only event sources that are managed within lambda's own cli api are Kinesis Streams and DynamoDb Streams. You can manage them with the cli using aws lambda
S3 bucket events are managed within S3, as they can be sent to SNS topics, SQS queues, or Lambda functions. So you need to use the aws s3api cli commands, specifically put-bucket-notification-configuration
In the --notification-configuration arg, you'll have something like:
{
"LambdaFunctionConfigurations": [
{
"Id": "string",
"LambdaFunctionArn": "string",
"Events": [events]
}
]
}
Where events are list of s3 events
1 Comment
aws lambda - aws lambda help provides none either..... ah, the link to aws lambda is the clue. aws lambda create-event-source-mappingI was able to add SNS trigger for lambda using below AWS CLI
aws lambda add-permission \
--function-name {{LAMBDA-FUNCTION-NAME}} \
--statement-id {{UNIQUE-ID}} \
--action "lambda:InvokeFunction" \
--principal sns.amazonaws.com \
--source-arn arn:aws:sns:us-east-1:77889900:{{SNS-TOPIC-ARN}}
The SNS topic can also be from other region.
Hope this helps.
Comments
As @user1292364 mentioned we need to use add-permission to the lambda.
Only issue with it is that you need to make sure that lambda to sns subscription is added also. Otherwise this error will occur
A subscription for arn:aws:lambda:eu-west-1:276xxxxxx:function:HourlyLambdaFunction on the topic HourlyLambdaFunction could not be found.
I would prefer to use it over AWS CLI in this way:
#!/usr/bin/env bash
# Add Lambda to SNS as subscription
aws sns subscribe \
--topic-arn arn:aws:sns:eu-west-1:276xxxxxx:HourlyLambdaFunction \
--protocol lambda \
--notification-endpoint arn:aws:lambda:eu-west-1:276xxxxxx:function:HourlyLambdaFunction
# Give permissions to Lambda to access that subscription i.e. Add it through triggers
aws lambda add-permission \
--function-name HourlyLambdaFunction \
--statement-id 276xxxxxx\
--action "lambda:InvokeFunction" \
--principal sns.amazonaws.com \
--source-arn arn:aws:sns:eu-west-1:276xxxxxx:HourlyLambdaFunction
# Send message to publish and trigger lamda
aws sns publish \
--topic-arn arn:aws:sns:eu-west-1:276xxxxxx:HourlyLambdaFunction \
--subject "HourlyLambdaFunction" \
--message "{datawarehouse:banana_wh, database:banana_db, schema:banana. query:'select count(*) from banana.banana_loads;'}"
ps: \ in the code is new line for bash script (if anyone wonders)
Logs of the lambda function can be found on Cloudwatch

aws lambda add-permissions...!