Using Amazon SDK for C++ credential providers - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using Amazon SDK for C++ credential providers

To make requests to Amazon using the Amazon SDK for C++, the SDK uses cryptographically-signed credentials issued by Amazon. At runtime, the SDK retrieves configuration values for credentials by checking several locations.

Authentication with Amazon can be handled outside of your codebase. Many authentication methods can be automatically detected, used, and refreshed by the SDK using the credential provider chain.

For guided options for getting started on Amazon authentication for your project, see Authentication and access in the Amazon SDKs and Tools Reference Guide.

The credential provider chain

If you don't explicitly specify a credential provider when constructing a client, the SDK for C++ uses a credential provider chain that checks a series of places where you can supply credentials. Once the SDK finds credentials in one of these locations, the search stops.

Credential retrieval order

All SDKs have a series of places (or sources) that they check in order to get valid credentials to use to make a request to an Amazon Web Services service. After valid credentials are found, the search is stopped. This systematic search is called the credential provider chain.

For each step in the chain, there are different ways to set the values. Setting values directly in code always takes precedence, followed by setting as environment variables, and then in the shared Amazon config file. For more information, see Precedence of settings in the Amazon SDKs and Tools Reference Guide.

The SDK attempts to load credentials from the [default] profile in the shared Amazon config and credentials files. You can use the AWS_PROFILE environment variable to choose a named profile you want the SDK to load instead of using [default]. The config and credentials files are shared by Amazon SDKs and tools. The Amazon SDKs and Tools Reference Guide has information on SDK configuration settings used by all Amazon SDKs and the Amazon CLI. To learn more about how to configure the SDK through the shared Amazon config file, see Shared config and credentials files. To learn more about how to configure the SDK through setting environment variables, see Environment variables support.

To authenticate with Amazon, the SDK for C++ checks the credential providers in the following order.

  1. Amazon access keys (temporary and long-term credentials)

    The SDK attempts to load credentials from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables, or from the shared Amazon credentials file.

    • For guidance on configuring this provider, see Amazon access keys in the Amazon SDKs and Tools Reference Guide.

    • For details on SDK configuration properties for this provider, see Amazon access keys in the Amazon SDKs and Tools Reference Guide.

  2. Amazon STS web identity

    When creating mobile applications or client-based web applications that require access to Amazon, Amazon Security Token Service (Amazon STS) returns a set of temporary security credentials for federated users who are authenticated through a public identity provider (IdP).

    • When you specify this in a profile, the SDK or tool attempts to retrieve temporary credentials using Amazon STS AssumeRoleWithWebIdentity API method. For details on this method, see AssumeRoleWithWebIdentity in the Amazon Security Token Service API Reference.

    • For guidance on configuring this provider, see Federate with web identity or OpenID Connect in the Amazon SDKs and Tools Reference Guide.

    • For details on SDK configuration properties for this provider, see Assume role credential provider in the Amazon SDKs and Tools Reference Guide.

  3. IAM Identity Center

    If you use IAM Identity Center to authenticate, this is when the SDK for C++ uses the single sign-on token that was set up by running Amazon CLI command aws sso login. The SDK uses the temporary credentials that the IAM Identity Center exchanged for a valid token. The SDK then uses the temporary credentials when it calls Amazon Web Services services. For detailed information about this process, see Understand SDK credential resolution for Amazon Web Services services in the Amazon SDKs and Tools Reference Guide.

  4. External process provider

    This provider can be used to provide custom implementations, such as retrieving credentials from an on-premises credentials store or integrating with your on-premises identify provider.

    • For guidance on one way to configure this provider, see IAM Roles Anywhere in the Amazon SDKs and Tools Reference Guide.

    • For details on SDK configuration properties for this provider, see Process credential provider in the Amazon SDKs and Tools Reference Guide.

  5. Amazon ECS and Amazon EKS container credentials

    Your Amazon Elastic Container Service tasks and Kubernetes service accounts can have an IAM role associated with them. The permissions granted in the IAM role are assumed by the containers running in the task or containers of the pod. This role allows your SDK for C++ application code (on the container) to use other Amazon Web Services services.

    The SDK attempts to retrieve credentials from the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI or AWS_CONTAINER_CREDENTIALS_FULL_URI environment variables, which can be set automatically by Amazon ECS and Amazon EKS.

  6. Amazon EC2 Instance Metadata Service

    Create an IAM role and attach it to your instance. The SDK for C++ application on the instance attempts to retrieve the credentials provided by the role from the instance metadata.

The credential provider chain can be reviewed at AWSCredentialsProviderChain in the Amazon SDK for C++ source code on GitHub.

If you followed the recommended approach for new users to get started, you set up Amazon IAM Identity Center authentication during Authenticating the Amazon SDK for C++ with Amazon of the Getting started topic. Other authentication methods are useful for different situations. To avoid security risks, we recommend always using short-term credentials. For other authentication method procedures, see Authentication and access in the Amazon SDKs and Tools Reference Guide.

Explicit credential provider

Instead of relying on the credential provider chain to detect your authentication method, you can specify a specific credential provider that the SDK should use. You can do this by providing credentials in your service client's constructor.

The following example creates an Amazon Simple Storage Service client by directly providing temporary access credentials instead of using the chain.

SDKOptions options; Aws::InitAPI(options); { const auto cred_provider = Aws::MakeShared<Auth::SimpleAWSCredentialsProvider>("TestAllocationTag", "awsAccessKeyId", "awsSecretKey", "sessionToken"); S3Client client{cred_provider}; } Aws::ShutdownAPI(options);

Identity caching

The SDK will cache credentials and other identity types such as SSO tokens. By default, the SDK uses a lazy cache implementation that loads credentials upon first request, caches them, and then attempts to refresh them during another request when they are close to expiring. Clients created from the same Aws::Client::ClientConfiguration share a cache.