2

I need the syntax for python to enable and disable lambda function programmatically

I have checked boto3 I couldn't find any or am I missing something not sure please help

Thank you.

2
  • What do you mean by "disable"? There is no such functionality. Commented Dec 24, 2020 at 5:48
  • stackoverflow.com/questions/46199256/…. Please check above link there is aws cli to --enable and --no-enable can we do it in python I see when we give --no-enable lambda function wont execute else executes. Please correct if my understanding is wrong Commented Dec 24, 2020 at 5:52

2 Answers 2

3

The link provided is about enabling/disabling event source mapping for a lambda function. This can be done using update_event_source_mapping in boto3 with Enabled option:

Enabled (boolean) -- If true, the event source mapping is active. Set to false to pause polling and invocation.

However, this only disables the mapping, not the function itself. There is no API call to disable a function. Your function still will be invokable, just not through the source mappings.

As a workaround which somehow can mimic "disabling" a function, is to deny its invocation or remove invocation permissions from any IAM user/role which shouldn't be able to invoke it.

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

3 Comments

Thank you @Marcin. My definition of invocation was when I run update_event_source_mapping with "not enabled" the function shouldn't "execute/lambda handler shouldn't trigger " else the function should "execute/run/lambda handler should trigger" will this update_event_source_mapping work as per my requirement ?
@user3292373 Event source mapping works only with SQS, Kinesis, DynamoDB and Kafka. So if you disable, lets say, mapping for SQS, the other ones will still work. Also if you have triggers different than from source mapping, such as S3 events, they will still work.
@user3292373 as per the question your only option is to revoke IAM permissions from lambda. Or make sure you iterate over all the integration for your lambda as marcin described there could be many which you can configure, so you iterate over all of them. Simplest I see is to remove the IAM permissions which are just trust policy for the IAM role being used by lambda that's all.
3

As a work around, for disabling lambda, you can set concurrency to 0 using https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.put_function_concurrency

And you can enable by setting it to a positive number.

1 Comment

Thank you for noted that you'd already tested setting the concurrency to 0 to disable the function! After reading the docs that seemed like the only way to do it programmatically but the docs don't explicitly say 0 is a valid option.

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.