1. Answers
  2. Setting Environment Variables Across Deployment Stages

How Do I Set Environment Variables for Different Deployment Stages?

Introduction

In this guide, you’ll learn how to set environment variables for different deployment stages. This is crucial for managing configurations like API keys, database URLs, and other sensitive information that vary between development, testing, staging, and production environments. By defining environment variables for different stages using variables and conditional logic, you can streamline your deployment process and ensure consistency across different environments.

Setting Up Environment Variables

To set up environment variables for different deployment stages, follow these steps:

  1. Import Required Modules: Start by importing necessary modules from Pulumi and AWS SDKs.
  2. Configure Environment Settings: Use Pulumi’s configuration to retrieve environment-specific settings. The deployment stage is determined by the env variable, which defaults to “dev” if not specified.
  3. Define Conditional Logic: Use conditional logic to set environment-specific values for the database URL and API key. This ensures that the correct values are used based on the deployment stage.
  4. Configure Resources: Use these environment-specific values in your resource configurations. For example, set environment variables for an AWS EC2 instance using the userData script.
  5. Output Configuration: Return the configured values to verify the settings applied to your resources.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

export = async () => {
    const config = new pulumi.Config();
    // The deployment stage (dev, test, prod)
    const env = config.get("env") || "dev";
    // Database URL for the application
    const databaseUrl = config.get("databaseUrl") || "localhost:5432";
    // API Key for the application
    const apiKey = config.get("apiKey") || "";
    const dbUrl = env == "prod" ? "prod-db.example.com" : env == "test" ? "test-db.example.com" : databaseUrl;
    const myApiKey = env == "prod" ? "PROD_API_KEY" : env == "test" ? "TEST_API_KEY" : apiKey;
    // Example resource (e.g., an AWS instance) setting environment variables
    const example = new aws.ec2.Instance("example", {
        ami: "ami-08d4ac5b634553e16",
        instanceType: aws.ec2.InstanceType.T2_Micro,
        tags: {
            Name: "example-instance",
            Env: env,
        },
        userData: `#!/bin/bash
export DATABASE_URL=${dbUrl}
export API_KEY=${myApiKey}
`,
    });
    return {
        databaseUrl: dbUrl,
        apiKey: myApiKey,
    };
}

Conclusion

By following this guide, you have learned how to set up and manage environment variables across different deployment stages using Pulumi. This method allows you to maintain organized and efficient configurations for each environment, reducing the risk of errors and ensuring that your applications have access to the correct resources and credentials. Leveraging conditional logic in your infrastructure-as-code setups can significantly enhance the management of your deployment processes.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up