3

Is there a way for us to disable & enable the Lambda trigger programmatically (e.g. for scheduled maintains purposes)?

enter image description here

4 Answers 4

19

You can disable and enable Lambda triggers by using following approaches with Update Event Source Mapping, based on how you are going to do it.

Note: You need to grant relevant permission for each of the approach to execute using IAM Roles/Users or temporarily access credentials.

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

Comments

2

It is documented under EventSourceMapping, you specify which event arn should map to a given lambda, it will do the trigger association.

Below is the API using node js,

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#createEventSourceMapping-property

Using CLI: http://docs.aws.amazon.com/cli/latest/reference/lambda/create-event-source-mapping.html

All supported languages have this API as well.

3 Comments

I try using the AWSLambdaClientBuilder.defaultClient().listEventSourceMappings() and it returns an empty list. Doesn't seems like it? Or maybe it doesn't support S3 events?
Try the clip before you take it to code. There are other things you might be configured wrong.
Using EventSourceMapping won't work because those functions only work with Kinesis streams, SQS, and DynamoDB streams: see the documentation for listEventSourceMappings under the input documentation for params: docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/… I've tried to return all by not specifying a function ARN and only get those with SQS (I don't have any Kinesis or DynamoDB events), no SNS or others.
1

Enable/Disable in Java:

AWSLambda client = AWSLambdaClientBuilder.standard().build();
UpdateEventSourceMappingRequest request = new UpdateEventSourceMappingRequest()
    .withUUID(uuid)
    .withFunctionName("myFunction")
    .withEnabled(true) // false to disable
    .withBatchSize(123);
UpdateEventSourceMappingResult response = client.updateEventSourceMapping(request);

In Kotlin:

val client: AWSLambda = AWSLambdaClientBuilder.standard().build()
val request: UpdateEventSourceMappingRequest = UpdateEventSourceMappingRequest()
                .withUUID(uuid)
                .withFunctionName("myFunction")
                .withEnabled(false) // true to enable
                .withBatchSize(10)
val response: UpdateEventSourceMappingResult = client.updateEventSourceMapping(request)

Dependency Needed:

implementation 'com.amazonaws:aws-java-sdk-lambda:1.11.+'

Comments

1

All credits to @ashan, for anyone like me who would prefer to have an example of how to do it using AWS cli, replace $uuid with your trigger UUID:

# Enable lambda polling & invocation
aws lambda update-event-source-mapping --uuid  "$uuid" --enabled

# Disable it
aws lambda update-event-source-mapping --uuid  "$uuid" --no-enabled

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.