Quick revision
The short answer
A Java service on AWS should get short-lived credentials from an IAM role and never from hard-coded or long-lived access keys. In SDK 2.x we simply build clients without credentials, and DefaultCredentialsProvider tries, in order, system properties, environment variables, a web identity token file, shared credentials and config profiles, the container credentials endpoint and finally EC2 instance metadata. EC2 delivers the role through an instance profile, ECS through the task role, and EKS through IAM Roles for Service Accounts or EKS Pod Identity; the SDK refreshes the credentials before they expire, and the role grants only the actions and resources the service needs.
What to remember
In this answer, we saw that a Java service should get temporary credentials from an IAM role, never from stored access keys. DefaultCredentialsProvider in SDK 2.x checks system properties, environment variables, the web identity token file, profiles, the container endpoint and instance metadata in that order. EC2 uses an instance profile, ECS a task role, and EKS IRSA or Pod Identity; the SDK refreshes credentials for reused clients, and the role grants only the actions and resources the service needs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Java service on AWS should get short-lived credentials from an IAM role and never from hard-coded or long-lived access keys. In SDK 2.x we simply build clients without credentials, and DefaultCredentialsProvider tries, in order, system properties, environment variables, a web identity token file, shared credentials and config profiles, the container credentials endpoint and finally EC2 instance metadata. EC2 delivers the role through an instance profile, ECS through the task role, and EKS through IAM Roles for Service Accounts or EKS Pod Identity; the SDK refreshes the credentials before they expire, and the role grants only the actions and resources the service needs.
Why this question is asked
Leaked AWS keys are one of the most common ways cloud accounts get compromised: a key in a Git repository, a Docker image or a log file is found by automated scanners within minutes. Interviewers ask this question to check that we know the safe way, and that we understand the mechanism well enough to debug it when a service picks up the wrong identity. In this answer, we will recap IAM roles and temporary credentials, walk through the default provider chain step by step, see how a role reaches the service on EC2, ECS and EKS, and finish with what must never be done and how least privilege looks for S3 and DynamoDB.
1A Quick Recap: Roles and Temporary Credentials
Every call to AWS is signed with credentials. An access key, an id and a secret that belong to an IAM user, is long-lived: it works until someone deletes it, wherever it ends up. An IAM role has no keys of its own. Instead, a trusted principal assumes the role and gets temporary credentials from AWS STS, the Security Token Service: an access key, a secret and a session token that expire, typically after an hour.
So the safe design is that the compute running our service, the EC2 instance, the ECS task or the Kubernetes pod, is allowed to assume a role, and our code receives temporary credentials for that role at runtime. Nothing secret is stored in the code, the configuration or the image.
2Walking Through the Default Credentials Provider Chain
When we build a client without specifying credentials, SDK 2.x uses DefaultCredentialsProvider. It is a chain: it asks each provider in turn and uses the first one that returns credentials.
- Java system properties
aws.accessKeyId,aws.secretAccessKey - Environment variables
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY - Web identity token file
AWS_ROLE_ARN+ token file (EKS IRSA) - Shared credentials and config files~/.aws profiles, SSO
- Container credentials endpointECS task role, EKS Pod Identity
- EC2 instance metadatainstance profile role
6 steps in order. Play them, or step through them one at a time.
The first provider that returns credentials wins.
Notice the order. Explicit settings come first, so a developer can override everything with environment variables. Then come the mechanisms that compute platforms use: the web identity token file that EKS mounts, local profiles for developers, the container endpoint used by ECS and EKS Pod Identity, and the EC2 instance metadata service last. This order is also why a stray AWS_ACCESS_KEY_ID in an environment variable silently wins over the role we intended, a common surprise.
3How the Role Reaches the Service on EC2, ECS and EKS
On EC2, we attach an instance profile, a container for one role, to the instance. The instance metadata service, reachable at a link-local address only from that instance, serves temporary credentials for the role, and the last provider in the chain reads them. Every process on the instance shares that role.
On ECS, including Fargate, we set a task role in the task definition. The ECS agent provides a credentials endpoint inside the task and sets an environment variable pointing to it, so the container credentials provider picks up credentials for that task's role. Different tasks on the same host get different roles.
On EKS there are two options. With IAM Roles for Service Accounts, IRSA, we annotate a Kubernetes service account with a role ARN; EKS injects AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE into the pod, and the SDK exchanges the signed token for role credentials with STS. With EKS Pod Identity, an association maps a service account to a role, and an agent on the node serves credentials through the container credentials endpoint. Either way each pod gets its own role.
| Platform | Mechanism | Provider in the chain |
|---|---|---|
| EC2 | Instance profile | Instance metadata |
| ECS / Fargate | Task role in task definition | Container credentials |
| EKS with IRSA | Annotated service account | Web identity token file |
| EKS Pod Identity | Pod Identity association | Container credentials |
| Developer laptop | IAM Identity Center (SSO) profile | Profile files |
4Refresh, Least Privilege and What Never to Do
Temporary credentials expire, but we do not handle that ourselves. The provider caches them and refreshes them before they expire, as long as we reuse the same client. Creating a new client per request defeats this and adds latency, so clients are long-lived beans.
The role should grant only what the service needs: the specific actions on the specific resources. A service that reads invoices from one bucket and writes to one table does not need s3:* on every bucket.
What must never be done is putting access keys in source code, application.yml, Docker images, environment variables baked into images, or long-lived keys on servers. Keys in those places leak through Git history, image registries and logs, and they cannot be scoped to one task or rotated automatically. If a key is ever exposed, we deactivate it immediately and check CloudTrail for its use.
5Follow-up questions
Why is the ECS task role better than the EC2 instance role for containers?
The instance role is shared by everything on the host, so every container on an ECS EC2 host could use the union of permissions all services need, and a compromised container gets them all. A task role gives each task only its own permissions, delivered through the container credentials endpoint. We should also block containers from reaching the instance metadata service, so they cannot fall back to the instance role. On Fargate there is no shared host role to worry about.
Which region does the client use if we do not set one?
DefaultAwsRegionProviderChain checks the aws.region system property, the AWS_REGION environment variable, the profile configuration and then instance metadata. On ECS and Lambda AWS_REGION is set for us. Setting the region explicitly in configuration is clearer and avoids a surprise call to instance metadata.
How do developers call AWS from their laptops then?
Through IAM Identity Center: they log in with aws sso login, and the profile provider in the chain uses the cached short-lived session. No permanent keys sit on the laptop.
6Conclusion
In this answer, we saw that a Java service should get temporary credentials from an IAM role, never from stored access keys. DefaultCredentialsProvider in SDK 2.x checks system properties, environment variables, the web identity token file, profiles, the container endpoint and instance metadata in that order. EC2 uses an instance profile, ECS a task role, and EKS IRSA or Pod Identity; the SDK refreshes credentials for reused clients, and the role grants only the actions and resources the service needs.
Open with
The service should use an IAM role and short-lived credentials, never access keys in code, configuration or images. In the code I just build the S3 and DynamoDB clients without credentials, as long-lived beans, and the SDK uses DefaultCredentialsProvider.
Then, if they want more
In 2.x that chain checks Java system properties, then environment variables, then a web identity token file, which is how EKS IRSA works, then the shared credentials and config profiles, then the container credentials endpoint, used by ECS task roles and EKS Pod Identity, and finally EC2 instance metadata.
The first provider that returns credentials wins, which is also why a stray AWS_ACCESS_KEY_ID variable can override the role. On EC2 the role comes from an instance profile. On ECS and Fargate I set a task role in the task definition, which gives each task its own permissions instead of sharing the host's.
On EKS I annotate the service account for IRSA, or create a Pod Identity association, so each pod gets its own role. The credentials expire after about an hour, but the provider refreshes them automatically as long as I reuse the client.
The role follows least privilege: for example s3:GetObject on one bucket's objects and GetItem, PutItem and Query on one table, never s3:* on everything. What I would never do is hard-code keys, put them in application.yml or a Docker image, or leave long-lived keys on servers, because they leak and cannot be scoped or rotated.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- AWS documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How should a Java service running on AWS get credentials to call S3 and DynamoDB?...": https://scrivio-interview-library.netlify.app/spring-boot/aws/4d56cc10fa/