Home > database >  How to trigger a lambda via cloud watch events/event bridge in localstack using awslocal
How to trigger a lambda via cloud watch events/event bridge in localstack using awslocal

Time:12-03

I am struggling to find any information on how to setup triggers/rules on a lambda so it will trigger with certain event bridge events via the cli as almost all examples on aws are via http requests to set it up.

In this scenario I am using localstack on docker to spin up everything locally for developers systems so ideally I am trying to work out what syntax I need to provide the awslocal cli (basically a wrapper around aws cli).

Here is what I have so far which seems to create the lambda and but I cant find out what command I need to run to setup the rule to direct certain events to the lambda.

# Setup IAM stuffs
awslocal iam create-role --role-name lambda-dotnet-ex --assume-role-policy-document '{"Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
awslocal iam attach-role-policy --role-name lambda-dotnet-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

# Setup handler for each event type
awslocal lambda create-function --function-name some-event-handler --zip-file fileb:///Dist/SomeEventHandlingLambda.0.0.0.zip --role arn:aws:iam::000000000000:role/lambda-dotnet-ex

Then inside the lambda there is a doetnet (C#) handler sig like so:

[LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
public async Task ProcessEvent(CloudWatchEvent<string> message, ILambdaContext context)

Currently I'm fine to just route ALL events to this lambda, but struggling to find docs/examples around this, so any help would be great.

CodePudding user response:

To create a rule and direct events to your lambda function using the AWS CLI, you can use the put-rule command with the lambda target. Here's an example of the syntax:

awslocal events put-rule --name my-rule --event-pattern '{"source": ["my-source"]}' --target arn:aws:lambda:us-east-1:000000000000:function:some-event-handler

Replace my-rule with the name of your rule, my-source with the source of the events you want to capture, and arn:aws:lambda:us-east-1:000000000000:function:some-event-handler with the ARN of your lambda function.

If you want to capture all events, you can use a wildcard * for the event-pattern:

awslocal events put-rule --name my-rule --event-pattern '{"source": ["*"]}' --target arn:aws:lambda:us-east-1:000000000000:function:some-event-handler
  • Related