Using environment variables to globally configure AWS SDKs and tools
Environment variables provide another way to specify configuration options and credentials when using AWS SDKs and tools. Environment variables can be useful for scripting or temporarily setting a named profile as the default. For the list of environment variables supported by most SDKs, see Environment variables list.
Precedence of options
-
If you specify a setting by using its environment variable, it overrides any value loaded from a profile in the shared AWS
config
andcredentials
files. -
If you specify a setting by using a parameter on the AWS CLI command line, it overrides any value from either the corresponding environment variable or a profile in the configuration file.
How to set environment variables
The following examples show how you can configure environment variables for the default user.
$
export AWS_ACCESS_KEY_ID=
AKIAIOSFODNN7EXAMPLE
$
export AWS_SECRET_ACCESS_KEY=
wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
$
export AWS_SESSION_TOKEN=
AQoEXAMPLEH4aoAH0gNCAPy...truncated...zrkuWJOgQs8IZZaIv2BXIa2R4Olgk
$
export AWS_REGION=
us-west-2
Setting the environment variable changes the value used until the end of your shell session, or until you set the variable to a different value. You can make the variables persistent across future sessions by setting them in your shell's startup script.
Serverless environment variable setup
If you use a serverless architecture for development, you have other options for setting environment variables. Depending on your container, you can use different strategies for code running in those containers to see and access environment variables, similar to non-cloud environments.
For example, with AWS Lambda, you can directly set environment variables. For details, see Using AWS Lambda environment variables in the AWS Lambda Developer Guide.
In Serverless Framework, you can often set SDK environment variables in the
serverless.yml
file under the provider key under the
environment setting. For information on the serverless.yml
file,
see General function settings
Regardless of which mechanism you use to set container environment variables, there are some that are reserved by the container, such as those documented for Lambda at Defined runtime environment variables. Always consult the official documentation for the container that you’re using to determine how environment variables are treated and whether there are any restrictions.