Templates
SPA on CloudFront

Add Custom Variables

5min

By default, this template comes with one predefined environment variable called BACKEND_ENDPOINT. This variable can be used in the ReactJS application with window.env.BACKEND_ENDPOINT.

You can also add more custom environment variables by following the steps below. In this example we will add a new variable called MyCustomVar.

  • Update the input properties in the ./.microtica/schema.json file by adding the new variable of type string.
.microtica/schema.json

  • Update the CloudFormation template by adding the new variable as parameter and then reference the parameter in the DeploymentCustom resource properties.
.microtica/index.json

  • The last step is to update the logic that provides the environment variable in your web application. This code stores a /env.js file that assigns all environment variables under window.env. The new variable can be used with window.env.MyCustomVar.
.microtica/functions/deployment/src/index.ts

  • At the end, please check if you have the /env.js file imported in the public/index.html file.
public/index.html

  • Commit and push your changes. Automated build and deploy should start, once the deployment is done, go to Website resource settings, enter the value of the new environment variable
Add Custom Variables in Microtica
Add Custom Variables in Microtica


You can repeat the same process for additional environment variables.