I have a lambda function that takes in a dataset name, and creates a new lambda specifically for that dataset. Here's the code that sets this up:
lambda_response = lambda_client.create_function(
FunctionName=job_name,
Runtime='python3.6',
Role=role_name,
Handler='streaming_data_lambda.lambda_handler',
Code={
'S3Bucket': code_bucket,
'S3Key': 'streaming_data_lambda.py.zip'
},
Timeout=30,
)
This appears to be creating a lambda correctly, and works when I kick it off manually. I want to have this created lambda run once an hour, so the original lambda script creates the following rules and targets:
rule_response = event_client.put_rule(
Name=rule_name,
ScheduleExpression=schedule_expression
)
event_client.put_targets(
Rule=rule_name,
Targets=[
{
'Id': lambda_response['FunctionName'],
'Arn': lambda_response['FunctionArn'],
'Input': json.dumps(input_string)
}
]
)
where input_string is just something like {"datasetName": "name"}. I can see the rule in the CloudWatch Rules UI, and can see it's linked to the correct lambda and the input text is present. It triggers correctly every hour also, but fails to invoke the lambda function. If I look at the lambda in the UI and add the CloudWatch event rule I created as a trigger in the Designer section there, then it correctly kicks off the lambda, but is there some way to set this up in Python so I don't have to do this last step in the UI?
With EventBridge (CloudWatch Events), you can create rules that match selected events in the stream and route them to your AWS Lambda function to take action. For example, you can automatically invoke an AWS Lambda function to log the state of an EC2 instance or AutoScaling group.
To view logs using the Lambda consoleOpen the Functions page of the Lambda console. Choose a function. Choose Monitor. Choose View logs in CloudWatch.
The Lambda function works triggered by using an event. The event can be anything – a schedule, a message to the queue, the addition of a file to S3, etc. All these events can also pass some information to the Lambda function for execution.
For anyone who might be looking for the answer to this in the future - you need to add add permission for cloudwatch events to invoke your lambda function, like so:
lambda_client.add_permission(
FunctionName=lambda_response['FunctionName'],
StatementId=some_random_number,
Action='lambda:InvokeFunction',
Principal='events.amazonaws.com',
SourceArn=rule_response['RuleArn']
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With