Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Get a value from an environment variable

Focus mode
Get a value from an environment variable - AWS Cloud Development Kit (AWS CDK) v2

This is the AWS CDK v2 Developer Guide. The older CDK v1 entered maintenance on June 1, 2022 and ended support on June 1, 2023.

This is the AWS CDK v2 Developer Guide. The older CDK v1 entered maintenance on June 1, 2022 and ended support on June 1, 2023.

To get the value of an environment variable, use code like the following. This code gets the value of the environment variable amzn-s3-demo-bucket.

TypeScript
// Sets bucket_name to undefined if environment variable not set var bucket_name = process.env.amzn-s3-demo-bucket; // Sets bucket_name to a default if env var doesn't exist var bucket_name = process.env.amzn-s3-demo-bucket || "DefaultName";
JavaScript
// Sets bucket_name to undefined if environment variable not set var bucket_name = process.env.amzn-s3-demo-bucket; // Sets bucket_name to a default if env var doesn't exist var bucket_name = process.env.amzn-s3-demo-bucket || "DefaultName";
Python
import os # Raises KeyError if environment variable doesn't exist bucket_name = os.environ["amzn-s3-demo-bucket"] # Sets bucket_name to None if environment variable doesn't exist bucket_name = os.getenv("amzn-s3-demo-bucket") # Sets bucket_name to a default if env var doesn't exist bucket_name = os.getenv("amzn-s3-demo-bucket", "DefaultName")
Java
// Sets bucketName to null if environment variable doesn't exist String bucketName = System.getenv("amzn-s3-demo-bucket"); // Sets bucketName to a default if env var doesn't exist String bucketName = System.getenv("amzn-s3-demo-bucket"); if (bucketName == null) bucketName = "DefaultName";
C#
using System; // Sets bucket name to null if environment variable doesn't exist string bucketName = Environment.GetEnvironmentVariable("amzn-s3-demo-bucket"); // Sets bucket_name to a default if env var doesn't exist string bucketName = Environment.GetEnvironmentVariable("amzn-s3-demo-bucket") ?? "DefaultName";
// Sets bucket_name to undefined if environment variable not set var bucket_name = process.env.amzn-s3-demo-bucket; // Sets bucket_name to a default if env var doesn't exist var bucket_name = process.env.amzn-s3-demo-bucket || "DefaultName";
PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.