How to create and use environment variables in Typescript & Serverless

A step by step guide on adding environment variables to a Typescript & Serverless project through its serverless.yml and using them within a function.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 12/16/2022 at 12:17
Last Updated on 07/08/2023 at 17:57
A flower that represents Typescript next to one that represents Serverless. Beneath it sits the text "Environment Variables".

A step by step guide on adding environment variables to a Typescript & Serverless project through its serverless.yml and using them within a function.

The following tutorial makes use of our Typescript & Serverless Starter project and walks you through how to create and use environment variables. To learn more about environment variables in Serverless or how we created the Typescript Serverless starter project please use the links below.

SubscribeDownload Open Source ProjectServerless Environment Variables GuideHow to create a Typescript Serverless projectHow to add & use path aliases with Serverless & Typescript

We recommend downloading our Open Source project, checking out the main branch and carrying out the steps outlined below. All relevant changes can be found on the tutorial/environment-variables branch.

git clone git@github.com:delasign/typescript-serverless-starter.git
View Github Repository

Step One: Add the environment variable

A screenshot of VSCode showing you how to add the Environment Variables to a serverless.yml. Add them indented in an environment parameter that is indented under the provider parameter.

In the serverless.yml under the provider, add an indentation and add a new parameter called environment.

Within the environment parameter, add your environment variables as shown below.

Step Two: Use an environment variable in a function

A screenshot of VSCode showing you how to use an environment variable in a serverless function.

To use an environment variable in a function you must use the following notation, where ENV_NAME is the name of your environment variable declared in your serverless.yaml.

process.env.ENV_NAME

In the sample code below, we have modified the Serverless function found at functions/sample.ts and have used our log system to log the environment variable for the purposes of confirming that our methodology works.

Learn more about our Typescript & Serverless Log System

Step Three: Test

A screenshot of Terminal showing the logged environment variable which was coded in the prior step.

In Terminal, invoke your function locally to test that the environment variable registered in the function.

How to invoke a Serverless function locally using terminal

Step Four: Use an environment variable in the serverless.yml

A screenshot of VSCode showing you how to use an environment variable within a serverless.yml. Use it using the markdown provided below.

To use an environment variable in the serverless.yml you must use the following notation, where ENV_NAME is the name of your environment variable declared in your serverless.yaml.

${self:provider.environment.ENV_NAME}

In the sample code below, we have replace the sample with the sample environment variable, to demonstrate that you can use an environment variable to give name to a function.

Step Five: Test

A screenshot of Terminal showing the logged environment variable which was coded in the prior step.

In Terminal, invoke your function locally to test that the environment variable registered in the serverless.yml.

How to invoke a Serverless function locally using terminal

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe