콘텐츠로 이동
문서Aspire 사용해 보기
문서사용해 보기

Environments

이 콘텐츠는 아직 번역되지 않았습니다.

When you run or deploy an Aspire application, the environment determines how the AppHost configures resources, what parameter values are used, and how your deployment is organized. Environments let you use the same AppHost to target different deployment contexts — development, staging, production, or any custom name — without duplicating your application model.

The Aspire environment is a single named value — like Development, Staging, or Production — that the AppHost receives as input. Environment names are case-insensitive strings; you can use any name that makes sense for your workflow. Your AppHost code can branch on this value to change resource topology, resolve different parameter values, or set environment variables on child services.

The environment name flows through the system in three stages:

  1. Input: You pass the environment name to the AppHost. Deployment commands such as aspire deploy, aspire publish, and aspire do expose --environment directly, and aspire start also accepts --environment <name>.
  2. AppHost evaluation: Your AppHost code reads the environment to branch logic, resolve parameters, or configure resources.
  3. Downstream configuration: Your AppHost explicitly sets framework-specific environment variables (like DOTNET_ENVIRONMENT or NODE_ENV) on child resources as needed.

aspire run is a development-oriented command, so the AppHost runs in development mode by default. If you want to evaluate a different AppHost environment locally, start the AppHost explicitly and specify the environment:

Start locally with a staging environment
aspire start --environment Staging

Deployment-oriented commands such as aspire publish, aspire deploy, and aspire do default to Production. Override them with the --environment flag:

Deploy to staging
aspire deploy --environment staging
Publish for production
aspire publish --environment production

Each environment maintains its own deployment state cache, so staging and production deployments track their provisioning settings independently.

The environment name and the execution context are independent concepts:

ConceptWhat it answersDefault for aspire runDefault for aspire publish
EnvironmentWhere is the app targeting?DevelopmentProduction
Execution contextHow was the AppHost invoked?Run modePublish mode

You can start an AppHost locally with aspire start --environment Staging for validation, or publish to a Development cloud environment if your workflow needs that. The two axes are independent — environment names are arbitrary strings, not tied to run vs publish.

Your AppHost code can check the current environment to change behavior. The environment is available through the builder’s host environment API:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const env: IHostEnvironment
env
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.environment: PropertyAccessor<IHostEnvironment>

Gets the Environment property

environment
.
function get(): Promise<IHostEnvironment>
get
();
// Check for specific environments
if (await
const env: IHostEnvironment
env
.
IHostEnvironment.isDevelopment(): boolean

Checks if the environment is Development.

isDevelopment
()) {
// Development-specific configuration
}
// Check for a custom environment name
if (await
const env: IHostEnvironment
env
.
IHostEnvironment.isEnvironment(environmentName: string): boolean

Checks if the environment matches the specified name.

isEnvironment
('Testing')) {
// Testing-specific configuration
}

The following convenience methods are available:

MethodReturns true when environment is
IsDevelopment() / isDevelopment()Development
IsStaging() / isStaging()Staging
IsProduction() / isProduction()Production
IsEnvironment(name) / isEnvironment(name)Any custom name

Set environment variables on child resources

Section titled “Set environment variables on child resources”

Your services often need to know which environment they’re running in. Different frameworks use different environment variables — use WithEnvironment to set the appropriate one for each service:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const env: IHostEnvironment
env
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.environment: PropertyAccessor<IHostEnvironment>

Gets the Environment property

environment
.
function get(): Promise<IHostEnvironment>
get
();
const
const isDevelopment: boolean
isDevelopment
= await
const env: IHostEnvironment
env
.
IHostEnvironment.isDevelopment(): boolean

Checks if the environment is Development.

isDevelopment
();
const
const dotnetEnvironment: "Development" | "Production"
dotnetEnvironment
=
const isDevelopment: boolean
isDevelopment
? 'Development' : 'Production';
const
const appEnvironment: "development" | "production"
appEnvironment
=
const isDevelopment: boolean
isDevelopment
? 'development' : 'production';
// .NET services use DOTNET_ENVIRONMENT
const
const api: ProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
('api', '../Api/Api.csproj');
await
const api: ProjectResource
api
.
ProjectResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ProjectResource

Sets an environment variable

withEnvironment
('DOTNET_ENVIRONMENT',
const dotnetEnvironment: "Development" | "Production"
dotnetEnvironment
);
// Node.js services use NODE_ENV
const
const frontend: NodeAppResource
frontend
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
(
'frontend',
'../frontend',
'server.js'
);
await
const frontend: NodeAppResource
frontend
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
('NODE_ENV',
const appEnvironment: "development" | "production"
appEnvironment
);
// Any container can receive custom env vars
const
const worker: ContainerResource
worker
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource

Adds a container resource to the application.

addContainer
('worker', {
AddContainerOptions.image?: string | undefined
image
: 'myorg/worker',
AddContainerOptions.tag?: string | undefined
tag
: 'latest' });
await
const worker: ContainerResource
worker
.
ContainerResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ContainerResource

Sets an environment variable

withEnvironment
('APP_ENV',
const appEnvironment: "development" | "production"
appEnvironment
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Common environment variable conventions by framework and ecosystem:

FrameworkEnvironment variableTypical values
ASP.NET CoreASPNETCORE_ENVIRONMENTDevelopment, Staging, Production
.NET (non-web)DOTNET_ENVIRONMENTDevelopment, Staging, Production
Node.jsNODE_ENVdevelopment, production
Python (Flask)FLASK_ENV (deprecated)development, production
Python (Django)DJANGO_SETTINGS_MODULEModule path
Ruby on RailsRAILS_ENVdevelopment, staging, production
GoAPP_ENVdevelopment, staging, production
RustAPP_ENV / RUST_ENVdevelopment, staging, production
Java (Spring)SPRING_PROFILES_ACTIVEdev, test, prod

Use parameters to externalize values that change between environments, such as SKU tiers, replica counts, or API endpoints:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const apiKey: ParameterResource
apiKey
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
('apiKey', {
secret?: boolean | undefined
secret
: true });
const
const api: ProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
('api', '../Api/Api.csproj');
await
const api: ProjectResource
api
.
ProjectResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ProjectResource

Sets an environment variable

withEnvironment
('API_KEY',
const apiKey: ParameterResource
apiKey
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

When you call AddParameter("apiKey"), Aspire looks for a value under the configuration key Parameters:apiKey. The value can come from multiple sources:

All AppHost languages:

SourceExampleWhen to use
Environment variablesParameters__apiKey=valueCI/CD pipelines, containers
Command-line arguments--Parameters:apiKey=valueQuick overrides
Interactive promptPrompted during aspire deployFirst-time setup

C# AppHosts only (via .NET configuration):

SourceExampleWhen to use
appsettings.{env}.json{ "Parameters": { "apiKey": "value" } }Per-environment defaults in source control
appsettings.jsonSame structureBaseline defaults
User secretsdotnet user-secrets set "Parameters:apiKey" "value"Local development secrets

The double-underscore (__) in environment variable names replaces the colon (:) used in configuration keys. So the parameter apiKey maps to the environment variable Parameters__apiKey.

Per-environment defaults with config files (C# AppHosts)

Section titled “Per-environment defaults with config files (C# AppHosts)”

C# AppHosts can use appsettings.{environment}.json files to set different parameter values per environment:

appsettings.json
{
"Parameters": {
"apiKey": "dev-key-for-local-testing"
}
}
appsettings.Staging.json
{
"Parameters": {
"apiKey": "staging-key-value"
}
}

When the AppHost runs with --environment Staging, the staging file overrides the base values automatically. This is standard .NET configuration layering — no Aspire-specific mechanism is needed.

In CI/CD pipelines, set parameters as environment variables. This works for any AppHost language:

.github/workflows/deploy.yml
- name: Deploy to production
run: aspire deploy --environment production
env:
Parameters__apiKey: ${{ secrets.API_KEY }}
Parameters__replicas: '3'
Parameters__sku: 'Premium'

Environment variables take the highest priority, so they override any values from config files or defaults.

Use execution context for run vs publish decisions

Section titled “Use execution context for run vs publish decisions”

Use the execution context for choices that differ between local orchestration and publish/deploy workflows. For example, use a local Redis container in run mode and an Azure Managed Redis resource when publishing:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const isRunMode: boolean
isRunMode
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.executionContext: PropertyAccessor<DistributedApplicationExecutionContext>

Execution context for this invocation of the AppHost.

executionContext
.
DistributedApplicationExecutionContext.isRunMode: () => Promise<boolean>

Returns true if the current operation is running.

isRunMode
();
const
const cache: RedisResource | AzureManagedRedisResource
cache
=
const isRunMode: boolean
isRunMode
? await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRedis(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): RedisResource (+1 overload)

Adds a Redis container to the application model.

addRedis
('cache')
: await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addAzureManagedRedis(name: string): AzureManagedRedisResource

Adds an Azure Managed Redis resource to the application model.

addAzureManagedRedis
('cache');
const
const api: ProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
('api', '../Api/Api.csproj');
await
const api: ProjectResource
api
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: RedisResource | AzureManagedRedisResource
cache
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

In a CI/CD pipeline, you typically pass the environment name as part of the deployment command. Many CI systems have their own environment concepts that map naturally to Aspire environments.

GitHub Environments provide scoped secrets and protection rules. Map them to Aspire environments by passing the environment name to the CLI:

.github/workflows/deploy.yml
jobs:
deploy-staging:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- name: Deploy to staging
run: aspire deploy --environment staging
env:
Parameters__apiKey: ${{ secrets.API_KEY }}
deploy-production:
runs-on: ubuntu-latest
environment: production
needs: deploy-staging
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: aspire deploy --environment production
env:
Parameters__apiKey: ${{ secrets.API_KEY }}

With this approach:

  • Each GitHub Environment scopes its own secrets (the API_KEY secret has different values in staging and production).
  • Protection rules on the production environment can require approvals before deployment.
  • The --environment flag ensures Aspire uses the correct deployment state cache and passes the environment name to your AppHost.

For workflow-first guidance, see CI/CD overview. For a worked GitHub Actions example, see Example app lifecycle workflow. For GitHub workflow guidance that complements this page, see Deployment state caching.