Feedback

Follow Us

In order to authenticate requests, AWS services require you to provide your AWS access keys, also known as your AWS access key ID and secret access key.
In the AWS SDK for PHP, these access keys are often referred to collectively as your credentials. This guide
demonstrates how to provide your credentials to the AWS SDK for SDK.

The technique that you use to provide credentials to the SDK for your application is entirely up to you. Please read
each section on this page to determine what is the best fit for you. What you choose will depend on many different
factors, including:

The environment you are operating in (e.g., development, testing, production)

The type of project you are developing (e.g., application, CLI, library)

How often you rotate your credentials

If you rely on temporary or federated credentials

Your deployment process

Your application framework

Regardless of the technique used, it is encouraged that you follow the IAM Best Practices when managing your credentials, including the
recommendation to not use your AWS account's root credentials. Instead, create separate IAM users with their own access
keys for each project, and tailor the permissions of the users specific to those projects.

In general, it is recommended that you use IAM roles when running your application on Amazon EC2 and use credential
profiles or environment variables elsewhere.

If you do not provide credentials to a client object at the time of its instantiation (e.g., via the client's factory
method or via a service builder configuration), the SDK will attempt to find credentials in your environment when you
call your first operation. The SDK will use the $_SERVER superglobal and/or getenv() function to look for the
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. These credentials are referred to as
environment credentials.

Using IAM roles is the preferred technique for providing credentials to applications running on Amazon EC2. IAM roles
remove the need to worry about credential management from your application. They allow an instance to "assume" a role by
retrieving temporary credentials from the EC2 instance's metadata server. These temporary credentials, often referred to
as instance profile credentials, allow access to the actions and resources that the role's policy allows.

When launching an EC2 instance, you can choose to associate it with an IAM role. Any application running on that EC2
instance is then allowed to assume the associated role. Amazon EC2 handles all the legwork of securely authenticating
instances to the IAM service to assume the role and periodically refreshing the retrieved role credentials, keeping your
application secure with almost no work on your part.

If you do not explicitly provide credentials to the client object and no environment variable credentials are available,
the SDK attempts to retrieve instance profile credentials from an Amazon EC2 instance metadata server. These credentials
are available only when running on Amazon EC2 instances that have been configured with an IAM role.

Note

Instance profile credentials and other temporary credentials generated by the AWS Security Token Service (AWS STS)
are not supported by every service. Please check if the service you are using supports temporary credentials by
reading AWS Services that Support AWS STS.

While using IAM role credentials is the preferred method for providing credentials to an application running on an
Amazon EC2 instance, the roundtrip from the application to the instance metadata server on each request can introduce
latency. In these situations, you might find that utilizing a caching layer on top of your IAM role credentials can
eliminate the introduced latency.

The easiest way to add a cache to your IAM role credentials is to specify a credentials cache using the
credentials.cache option in a client's factory method or in a service builder configuration file. The
credentials.cache configuration setting should be set to an object that implements Guzzle's
Guzzle\Cache\CacheAdapterInterface (see Guzzle cache adapters). This interface provides an
abstraction layer over various cache backends, including Doctrine Cache, Zend Framework 2 cache, etc.

<?phprequire'vendor/autoload.php';useDoctrine\Common\Cache\FilesystemCache;useGuzzle\Cache\DoctrineCacheAdapter;// Create a cache adapter that stores data on the filesystem$cacheAdapter=newDoctrineCacheAdapter(newFilesystemCache('/tmp/cache'));// Provide a credentials.cache to cache credentials to the file system$s3Client=Aws\S3\S3Client::factory(array('credentials.cache'=>$cacheAdapter));

In the preceding example, the addition of credentials.cache causes credentials to be cached to the local filesystem
using Doctrine's caching system. Every request that uses this cache adapter first
checks if the credentials are in the cache. If the credentials are found in the cache, the client then ensures that the
credentials are not expired. In the event that cached credentials become expired, the client automatically refreshes the
credentials on the next request and populates the cache with the updated credentials.

A credentials cache can also be used in a service builder configuration:

Starting with the AWS SDK for PHP version 2.6.2, you can use an AWS credentials file to specify your credentials. This
is a special, INI-formatted file stored under your HOME directory, and is a good way to manage credentials for your
development environment. The file should be placed at ~/.aws/credentials, where ~ represents your HOME
directory.

Using an AWS credentials file offers a few benefits:

Your projects' credentials are stored outside of your projects, so there is no chance of accidentally committing
them into version control.

You can define and name multiple sets of credentials in one place.

You can easily reuse the same credentials between projects.

Other AWS SDKs and tools support, or will soon support, this same credentials file. This allows you to reuse your
credentials with other tools.

The format of the AWS credentials file should look something like the following:

Each section (e.g., [default], [project1]), represents a separate credential profile. Profiles can be
referenced from an SDK configuration file, or when you are instantiating a client, using the profile option:

<?phpuseAws\DynamoDb\DynamoDbClient;// Instantiate a client with the credentials from the project1 profile$dynamoDbClient=DynamoDbClient::factory(array('profile'=>'project1','region'=>'us-west-2',));

If no credentials or profiles were explicitly provided to the SDK and no credentials were defined in environment
variables, but a credentials file is defined, the SDK will use the "default" profile. You can change the default
profile by specifying an alternate profile name in the AWS_PROFILE environment variable.

The SDK allows you to explicitly set your credentials in your project in a few different ways. These techniques are
useful for rapid development, integrating with existing configurations systems (e.g., your PHP framework of choice), and
using temporary credentials. However, be careful to not hard-code your credentials
inside of your applications. Hard-coding your credentials can be dangerous, because it is easy to accidentally commit
your credentials into an SCM repository, potentially exposing your credentials to more people than intended. It can also
make it difficult to rotate credentials in the future.

The SDK provides a service builder that can be used to share configuration values across multiple clients. The service
builder allows you to specify default configuration values (e.g., credentials and regions) that are used by every
client. The service builder is configured using either JSON configuration files or PHP scripts that return an array.

The following is an example of a configuration script that returns an array of configuration data that can be used by
the service builder:

AWS Security Token Service (AWS STS) enables you to
request limited-privilege, temporary credentials for AWS IAM users or for users that you authenticate via identity
federation. One common use case for using temporary credentials is to grant mobile or client-side applications access to
AWS resources by authenticating users through third-party identity providers (read more about Web Identity Federation).

Note

Temporary credentials generated by AWS STS are not supported by every service. Please check if the service you are
using supports temporary credentials by reading AWS Services that Support AWS STS.

AWS STS has several operations that return temporary credentials, but the GetSessionToken operation is the simplest
for demonstration purposes. Assuming you have an instance of Aws\Sts\StsClient stored in the $stsClient
variable, this is how you call it:

$result=$stsClient->getSessionToken();

The result for GetSessionToken and the other AWS STS operations always contains a 'Credentials' value. If you
print the result (e.g., print_r($result)), it looks like the following:

However, the best way to provide temporary credentials is to use the createCredentials() helper method included
with the StsClient. This method extracts the data from an AWS STS result and creates the Credentials object for
you.

In some cases, you may want to create a client that is not associated with any credentials. This allows you to make
anonymous requests to a service. For example, both S3 Objects and CloudSearch Domains can be configured to allow
anonymous access.

To create an anonymous client, you can set the 'credentials' option to false.

$s3Client=S3Client::factory(array('credentials'=>false));// Makes an anonymous request. The Object would need to be publicly readable for this to succeed.$result=$s3Client->getObject(array('Bucket'=>'my-bucket','Key'=>'my-key',));