Skip to content

Resource option: envVarMappings ​

The envVarMappings resource option allows you to remap environment variables that a provider expects to custom environment variable names. This is useful when you need to run multiple providers or provider instances that require different values for the same environment variable.

When to use envVarMappings ​

Use this option when:

  • Running multiple providers targeting different accounts or regions: For example, two AWS providers targeting different accounts can each use their own environment variable-based credentials without conflicting.

Example ​

The following example shows how to remap CUSTOM_ARM_CLIENT_SECRET to ARM_CLIENT_SECRET so the provider reads from your custom environment variable:

typescript
import * as azure from "@pulumi/azure-native";

const provider = new azure.Provider("azure-provider", {}, {
    envVarMappings: {
        "CUSTOM_ARM_CLIENT_SECRET": "ARM_CLIENT_SECRET",
    },
});

Multi-provider example ​

A common use case is running two providers targeting different cloud accounts. Here's an example with two AWS providers for production and staging environments:

typescript
import * as aws from "@pulumi/aws";

// Production provider reads from PROD_AWS_ACCESS_KEY_ID and PROD_AWS_SECRET_ACCESS_KEY
const prodProvider = new aws.Provider("aws-prod", {}, {
    envVarMappings: {
        "PROD_AWS_ACCESS_KEY_ID": "AWS_ACCESS_KEY_ID",
        "PROD_AWS_SECRET_ACCESS_KEY": "AWS_SECRET_ACCESS_KEY",
    },
});

// Staging provider reads from STAGING_AWS_ACCESS_KEY_ID and STAGING_AWS_SECRET_ACCESS_KEY
const stagingProvider = new aws.Provider("aws-staging", {}, {
    envVarMappings: {
        "STAGING_AWS_ACCESS_KEY_ID": "AWS_ACCESS_KEY_ID",
        "STAGING_AWS_SECRET_ACCESS_KEY": "AWS_SECRET_ACCESS_KEY",
    },
});

// Use the providers explicitly
const prodBucket = new aws.s3.Bucket("prod-bucket", {}, { provider: prodProvider });
const stagingBucket = new aws.s3.Bucket("staging-bucket", {}, { provider: stagingProvider });

How it works ​

The envVarMappings option is a map where:

  • Keys are custom environment variables you have defined in your environment
  • Values are the names of environment variables the provider expects

When the provider initializes, Pulumi checks if your custom environment variable exists. If it does, the provider sees its value as if it were set under the mapped variable name.

Limitations ​

  • Provider resources only: This option only works on provider resources, not on regular resources or components.
  • One-way mapping: Each custom variable maps to exactly one provider variable. You cannot map multiple custom variables to the same provider variable on a single provider.

Content from the official Pulumi documentation (github.com/pulumi/docs).