Select Serverless configuration variables using the stage parameter

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (48/100)

When we use Serverless, the only distinction between production deployment and the testing environment is the configuration we use during the deployment. Serverless makes it relatively easy by providing the “stage” parameter during deployment.

This article will show how to use the stage argument to pick the correct configuration variables for a given environment.

First, we have to define a few custom variables in the yml file. For every variable, we define two values, one with the “dev” key and one with the “prod” key:

    prod: prod_value
    dev: dev_value

Now, in the environment section of the function configuration, we will extract the correct parameter using the templates two times:

      PARAM: ${self:custom.some_parameter.${opt:stage}}

We see that the templates are nested. The inner one gets the stage parameter from the options when we run the deploy command. After that, the outer template reads the correct value from the custom variables.

Would you like to help fight youth unemployment while getting mentoring experience?

Develhope is looking for tutors (part-time, freelancers) for their upcoming Data Engineer Courses.

The role of a tutor is to be the point of contact for students, guiding them throughout the 6-month learning program. The mentor supports learners through 1:1 meetings, giving feedback on assignments, and responding to messages in Discord channels—no live teaching sessions.

Expected availability: 15h/week. You can schedule the 1:1 sessions whenever you want, but the sessions must happen between 9 - 18 (9 am - 6 pm) CEST Monday-Friday.

Check out their job description.

(free advertisement, no affiliate links)

Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you want to contact me, send me a message on LinkedIn or Twitter.

Bartosz Mikulski
Bartosz Mikulski * MLOps Engineer / data engineer * conference speaker * co-founder of Software Craft Poznan & Poznan Scala User Group

Subscribe to the newsletter and get access to my free email course on building trustworthy data pipelines.