aws events put-targets响应为肯定,但未添加触发器

时间:2019-12-16 14:07:56

标签: aws-lambda boto3 aws-cli amazon-cloudwatch

我正在部署lambda函数,我想每天调用它。我意识到即使没有错误,最后的部署步骤还是会失败

aws events put-rule --name "daily_lambda_rule" \ 
                    --schedule-expression 'rate(15 minutes)'


aws lambda add-permission --function-name daily_lambda_rule \
                          --statement-id daily_lambda_function_scheduler \
                          --action 'lambda:InvokeFunction' \
                          --principal events.amazonaws.com \
                          --source-arn arn:aws:events:us-east-1:your-account-id:rule/daily_lambda_rule

aws events put-targets --rule daily_lambda_rule \
                        --targets "Id": "1","Arn":"arn:aws:lambda:us-east-1:your-account-id:function:myfun"

最后一个输出是:

{
    "FailedEntryCount": 0,
    "FailedEntries": []
}

这表明一切正常,但是当我单击lambda函数时,没有迹象表明cloudwatch已添加此触发器。另外,手动添加触发器也可以。

使用boto3会得到相同的结果:

import boto3
events_client = boto3.client('events')
lambda_arn = 'arn:aws:lambda:eu-west-2:my-account-id:function:myfunction'
event_rule_name ='daily_lambda_rule'

scheduled_lambda = [
    {
        'Id': "myfunction", # lambda name
        'Arn': lambda_arn,
    }
]

response = events_client.put_targets(Rule=event_rule_name,
                          Targets=scheduled_lambda)

什么地方/什么地方会阻止脚本从adding the trigger到lambda函数?

此处我未成功检查一些链接: https://towardsdatascience.com/loading-reddit-posts-using-aws-lambda-and-cloudwatch-events-9edb03ba4c14

https://docs.aws.amazon.com/cli/latest/reference/events/put-targets.html

boto3-put_targest https://docs.aws.amazon.com/code-samples/latest/catalog/python-cloudwatch-put_targets.py.html

boto3-示例 https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/python/example_code/lambda/scheduled_lambda/scheduled_lambda.py

1 个答案:

答案 0 :(得分:4)

我做了两个观察: 首先:在CLI命令中,在放置规则的同时,也要启用状态:

aws events put-rule --name "daily_lambda_rule" \ 
                    --schedule-expression 'rate(15 minutes)'
                    --state 'ENABLED'

第二:在Python Boto3脚本中,您跳过了Lambda Permissions步骤-必须将触发器附加到Lambda。以下是对我有用的完整代码:

import boto3

lambda_client = boto3.client('lambda')
events_client = boto3.client('events')

lambda_name = "live_scoreboard"
fn_role = 'arn:aws:iam::{}:role/lambda_basic_execution'.format(MY_ACCOUNT_ID)

create_res = lambda_client.create_function(
    FunctionName=lambda_name,
    Runtime='python3.8',
    Role=fn_role,
    Handler="{0}.lambda_handler".format(lambda_name),
    Code={'ZipFile': open("CODE_FILE_PATH.ZIP", 'rb').read(), },
)
fn_arn = create_res['FunctionArn']
cron = "0/5 9-18 * 2 ? 2020"
rule_name = "sb_".format(lambda_name)

rule_res = events_client.put_rule(
    Name=rule_name,
    ScheduleExpression='cron({})'.format(cron),
    State='ENABLED',
)
lambda_client.add_permission(
    FunctionName=lambda_name,
    StatementId="{0}-Event".format(rule_name),
    Action='lambda:InvokeFunction',
    Principal='events.amazonaws.com',
    SourceArn=rule_res['RuleArn'],
)
events_client.put_targets(
    Rule=rule_name,
    Targets=[
        {
            'Id': "target_" + (SOME_ID),
            'Arn': fn_arn,
        },
    ]
)