[英]How do I add trigger to an AWS Lambda function using AWS CLI?
The only event sources that are managed within lambda's own cli api are Kinesis Streams and DynamoDb Streams.在 lambda 自己的 cli api 中管理的唯一事件源是 Kinesis Streams 和 DynamoDb Streams。 You can manage them with the cli using
aws lambda
您可以使用
aws lambda
通过 cli 管理它们
S3 bucket events are managed within S3, as they can be sent to SNS topics, SQS queues, or Lambda functions. S3 存储桶事件在 S3 内进行管理,因为它们可以发送到 SNS 主题、SQS 队列或 Lambda 函数。 So you need to use the
aws s3api
cli commands, specifically put-bucket-notification-configuration
所以你需要使用
aws s3api
cli 命令,特别是put-bucket-notification-configuration
In the --notification-configuration
arg, you'll have something like:在
--notification-configuration
arg 中,您将拥有如下内容:
{
"LambdaFunctionConfigurations": [
{
"Id": "string",
"LambdaFunctionArn": "string",
"Events": [events]
}
]
}
I was able to add SNS trigger for lambda using below AWS CLI我能够使用下面的 AWS CLI 为 lambda 添加 SNS 触发器
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. SNS 主题也可以来自其他地区。
Hope this helps.希望这会有所帮助。
As @user1292364 mentioned we need to use add-permission to the lambda.正如@user1292364 提到的,我们需要对 lambda 使用添加权限。
Only issue with it is that you need to make sure that lambda to sns subscription is added als o.唯一的问题是您需要确保将lambda 添加到 sns 订阅。 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:我更愿意以这种方式在 AWS CLI 上使用它:
#!/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) ps:代码中的\\是bash脚本的新行(如果有人想知道的话)
Logs of the lambda function can be found on Cloudwatch可以在 Cloudwatch 上找到 lambda 函数的日志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.