13

Any way of doing this using AWS CLI? Adding trigger using AWS Management Console

1
  • 1
    I found the answer here. It can be done using aws lambda add-permissions...! Commented Nov 30, 2017 at 15:43

3 Answers 3

13

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

Sign up to request clarification or add additional context in comments.

1 Comment

uh - kind of missing an example/detail about how to do that with aws lambda - aws lambda help provides none either..... ah, the link to aws lambda is the clue. aws lambda create-event-source-mapping
10

I 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

7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.