Is there a way for us to disable & enable the Lambda trigger programmatically (e.g. for scheduled maintains purposes)?
4 Answers
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.
- Using AWS CLI: You can use AWS CLI update-event-source-mapping command with
--enabled | --no-enabledparameters. Using AWS SDK (E.g NodeJS): You can use AWS SDK updateEventSourceMapping method with
Enabled: true || falseattribute.Using AWS REST API: You can use AWS REST API UpdateEventSourceMapping with
"Enabled": booleanattributes.
Note: You need to grant relevant permission for each of the approach to execute using IAM Roles/Users or temporarily access credentials.
Comments
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
AWSLambdaClientBuilder.defaultClient().listEventSourceMappings() and it returns an empty list. Doesn't seems like it? Or maybe it doesn't support S3 events?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.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
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
