Use credentials from environment variables - AWS SDK for PHP

Use credentials from environment variables

Using environment variables to contain your credentials prevents you from accidentally sharing your AWS secret access key. We recommend that you never add your AWS access keys directly to the client in any production files. Many developers have had their account compromised by leaked keys.

To authenticate to Amazon Web Services, the SDK first checks for credentials in your environment variables. The SDK uses the getenv() function to look for the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables. These credentials are referred to as environment credentials. For instructions on how to obtain these values, see Authenticate using short-term credentials in the AWS SDKs and Tools Reference Guide.

If you’re hosting your application on AWS Elastic Beanstalk, you can set the AWS_ACCESS_KEY_ID, AWS_SECRET_KEY, and AWS_SESSION_TOKEN environment variables through the AWS Elastic Beanstalk console so that the SDK can use those credentials automatically.

For more information on how to set environment variables, see Environment variables support in the AWS SDKs and Tools Reference Guide. Also, for a list of all environment variables supported by most AWS SDKs, see Environment variables list.

You can also set the environment variables in the command line, as shown here.

Linux

$ export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE # The access key for your AWS account. $ export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY # The secret access key for your AWS account. $ export AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of security token> # The temporary session key for your AWS account. # The AWS_SECURITY_TOKEN environment variable can also be used, but is only supported for backward compatibility purposes. # AWS_SESSION_TOKEN is supported by multiple AWS SDKs other than PHP.

Windows

C:\> SET AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE # The access key for your AWS account. C:\> SET AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY # The secret access key for your AWS account. C:\> SET AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of security token> # The temporary session key for your AWS account. # The AWS_SECURITY_TOKEN environment variable can also be used, but is only supported for backward compatibility purposes. # AWS_SESSION_TOKEN is supported by multiple AWS SDKs besides PHP.