问题
Is there a way for us to disable & enable the Lambda trigger programmatically (e.g. for scheduled maintains purposes)?
回答1:
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-enabled
parameters. Using AWS SDK (E.g NodeJS): You can use AWS SDK updateEventSourceMapping method with
Enabled: true || false
attribute.Using AWS REST API: You can use AWS REST API UpdateEventSourceMapping with
"Enabled": boolean
attributes.
Note: You need to grant relevant permission for each of the approach to execute using IAM Roles/Users or temporarily access credentials.
回答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:
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.+'
来源:https://stackoverflow.com/questions/46199256/disable-and-enable-aws-lambda-trigger-programmatically