Might be out of scope, not 100% sure:
If you schedule your lambda (@app.schedule), the event param doesn't hold the stage name (unlike when using a "regular" pure lambda function), so there's no easy way of detecting your current stage.
Use case: In your scheduled lambda function, you want to have different logic (e.g. accessing a different database) according to the stage you're currently running in.
Quick solution would be to just set an environment variable for each stage. What do you mean though that you can do it with a "regular" pure lambda function? The event for that is based on how it was invoked. It has no knowledge of chalice or stages.
from chalice import Rate
from chalice import Chalice
app = Chalice(app_name='test-lambda-stage-event')
@app.lambda_function()
def pure(event, context):
return event
$ chalice deploy --stage prod
Creating deployment package.
Creating IAM role: test-lambda-stage-event-prod
Creating lambda function: test-lambda-stage-event-prod-pure
Creating lambda function: test-lambda-stage-event-prod-scheduled
$ chalice invoke --name pure --stage prod
{}
$ chalice deploy --stage dev
Creating deployment package.
Creating IAM role: test-lambda-stage-event-dev
Creating lambda function: test-lambda-stage-event-dev-pure
Creating lambda function: test-lambda-stage-event-dev-scheduled
$ chalice invoke --name pure --stage dev
{}
What happens currently if I have a scheduled lambda function (e.g. app.schedule for once an hour)? If I deploy the code to both prod and dev - won't that code run twice an hour?
This is related to #1009 as well.
Different lambda functions would be getting invoked. Same codepath but a different deployment as stages are entirely separate from one another. So similar to the other thread, you can set a stage-specific env var to control whatever behavior you want.
So basically set it via the environment_variables config, in the config file for that stage? (https://chalice.readthedocs.io/en/latest/topics/configfile.html?highlight=environment#stage-specific-configuration)
Yep, in the config for that stage set the environment_variables config.
Most helpful comment
Yep, in the config for that stage set the
environment_variablesconfig.