Basic set up - AWS SDK for Kotlin

Basic set up

Overview

To successfully develop applications that access AWS services using the AWS SDK for Kotlin, the following requirements must be met.

  • An active AWS access portal session when you are ready to run your application. You use the AWS Command Line Interface to initiate the sign-in process to IAM Identity Center's AWS access portal.

Important

The instructions in this setup section assume that you or organization uses IAM Identity Center. If your organization uses an external identity provider that works independently of IAM Identity Center, find out how you can get temporary credentials for the SDK for Kotlin to use. Follow these instructions to add temporary credentials to the ~/.aws/credentials file.

If your identity provider adds temporary credentials automatically to the ~/.aws/credentials file, make sure that the profile name is [default] so that you do not need to provide a profile name to the SDK or AWS CLI.

Sign-in ability to the AWS access portal

The AWS access portal is the web location where you manually sign in to the IAM Identity Center. The format of the URL is d-xxxxxxxxxx.awsapps.com/start or your_subdomain.awsapps.com/start.

If you are not familiar with the AWS access portal, follow the guidance for account access in the IAM Identity Center authentication topic in the AWS SDKs and Tools Reference Guide.

Set up single sign-on access for the SDK

After you complete Step 2 in the programmatic access section in order for the SDK to use IAM Identity Center authentication, your system should contain the following elements.

  • The AWS CLI, which you use to start an AWS access portal session before you run your application.

  • An ~/.aws/config file that contains a default profile. The SDK for Kotlin uses the profile's SSO token provider configuration to acquire credentials before sending requests to AWS. The sso_role_name value, which is an IAM role connected to an IAM Identity Center permission set, should allow access to the AWS services used in your application.

    The following sample config file shows a default profile set up with SSO token provider configuration. The profile's sso_session setting refers to the named sso-session section. The sso-session section contains settings to initiate an AWS access portal session.

    [default] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole region = us-east-1 output = json [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://provided-domain.awsapps.com/start sso_registration_scopes = sso:account:access

For more details about the settings used in the SSO token provider configuration, see SSO token provider configuration in the AWS SDKs and Tools Reference Guide.

If your development environment is not set up for programmatic access as previously shown, follow Step 2 in the SDKs Reference Guide.

Sign in using the AWS CLI

Before running an application that accesses AWS services, you need an active AWS access portal session in order for the SDK to use IAM Identity Center authentication to resolve credentials. Run the following command in the AWS CLI to sign in to the AWS access portal.

aws sso login

Since you have a default profile setup, you don't need to call the command with a --profile option. If your SSO token provider configuration uses a named profile, the command is aws sso login --profile named-profile.

To test if you already have an active session, run the following AWS CLI command.

aws sts get-caller-identity

The response to this command should report the IAM Identity Center account and permission set configured in the shared config file.

Note

If you already have an active AWS access portal session and run aws sso login, you will not be required to provide credentials.

However, you will see a dialog that requests permission for botocore to access your information. botocore is the foundation for the AWS CLI .

Select Allow to authorize access to your information for the AWS CLI and SDK for Kotlin.

Install Java and a build tool

Your development environment needs the following:

Use temporary credentials

As an alternative to configuring IAM Identity Center single sign-on access for the SDK, you can configure your development environment with temporary credentials.

Set up a local credentials file for temporary credentials
  1. Create a shared credentials file

  2. In the credentials file, paste the following placeholder text until you paste in working temporary credentials:

    [default] aws_access_key_id=<value from AWS access portal> aws_secret_access_key=<value from AWS access portal> aws_session_token=<value from AWS access portal>
  3. Save the file. The file ~/.aws/credentials should now exist on your local development system. This file contains the [default] profile that the SDK for Kotlin uses if a specific named profile is not specified.

  4. Sign in to the AWS access portal

  5. Follow these instructions under the Manual credential refresh heading to copy IAM role credentials from the AWS access portal.

    1. For step 4 in the linked instructions, choose the IAM role name that grants access for your development needs. This role typically has a name like PowerUserAccess or Developer.

    2. For step 7, select the Manually add a profile to your AWS credentials file option and copy the contents.

  6. Paste the copied credentials into your local credentials file and remove the generated profile name. Your file should resemble the following:

    [default] aws_access_key_id=AKIAIOSFODNN7EXAMPLE aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY aws_session_token=IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZVERYLONGSTRINGEXAMPLE
  7. Save the credentials file

The SDK for Kotlin will access these temporary credentials when it create a service client and use them for each request. The settings for the IAM role chosen in step 5a determine how long the temporary credentials are valid. The maximum duration is twelve hours.

After the temporary credentials expire, repeat steps 4 through 7.