IAM for Java Developers: Never Put AWS Access Keys in application.properties
A beginner-friendly explanation of IAM users, roles, policies, temporary credentials, and secure authentication for Java applications.
When a Java application needs to upload a file to Amazon S3 or read data from DynamoDB, the first question is not only “Which SDK method should I call?” It is also “Who is the application, and what is it allowed to do?”
That is the problem AWS Identity and Access Management—or IAM—helps solve.
One dangerous shortcut is placing an access key directly in
application.properties , committing it to Git, or including it inside a Docker image. Even if the repository is private, long-term credentials can leak through logs, screenshots, build artifacts, copied configuration files, or an accidental public commit.Here is the mental model I use as a Java learner.
Authentication and authorization are different
Authentication answers:
Who is making this request?
Authorization answers:
What is this identity allowed to do?
AWS evaluates policies when an authenticated principal makes a request. The result depends on the actions, resources, and conditions described in those policies.
The four IAM building blocks
IAM user
An IAM user is an identity in an AWS account. It can have console credentials, programmatic credentials, or both. However, AWS recommends temporary credentials for both human users and workloads whenever possible.
IAM user group
A group is a collection of IAM users. Policies attached to a group apply to its members, making permission management easier for people with similar responsibilities.
IAM role
A role is an identity with permissions, but it is not permanently associated with one person and does not contain standard long-term credentials. A trusted principal can assume the role and receive temporary credentials.
For applications running on AWS, roles are normally the safest model.
IAM policy
A policy is usually a JSON document that defines permissions. An identity-based policy can be attached to a user, group, or role and specifies the actions that identity can perform.
Least privilege: allow only what the application needs
The principle of least privilege means granting only the permissions required for a task.
Suppose a Spring Boot service only uploads and reads files from one S3 bucket. Giving it
AdministratorAccess would be much broader than necessary. A narrower learning example could look like this:{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListProjectBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-student-project-bucket"
},
{
"Sid": "ReadAndUploadProjectObjects",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::my-student-project-bucket/*"
}
]
}This remains only an example. A real policy should be adapted to the exact bucket, actions, object paths, encryption requirements, and conditions of the application.
Local Java development
For local development, AWS recommends temporary credentials rather than long-lived keys. Options include browser-based AWS console login or IAM Identity Center, depending on the environment.
The AWS SDK for Java 2.x includes a default credentials provider chain. This chain searches supported locations for credentials instead of requiring secrets to be written directly in the code.
A client can therefore be created without embedding an access key:
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
public class S3Configuration {
public S3Client s3Client() {
return S3Client.builder()
.region(Region.US_EAST_1)
.build();
}
}The SDK uses its credentials provider chain to find an available identity. No key is hard-coded in this class.
Java applications running on AWS
When the application is deployed, the credential source should match the compute service.
- Amazon EC2: attach an IAM role through an instance profile.
- Amazon ECS: use a task role for the application container.
- AWS Lambda: use the function's execution role.
- Elastic Beanstalk: grant required permissions to the appropriate environment role while keeping them narrowly scoped.
The Java SDK can retrieve and refresh temporary credentials supplied by the execution environment. The application code does not need to store a permanent secret.
What should never enter Git
I will never commit:
- AWS access key IDs
- AWS secret access keys
- Session tokens
- Root credentials
- Passwords
- Private keys
- Real values from sensitive
.envor properties files
Adding a file to
.gitignore is useful, but it is not enough after a secret has already been committed. The credential must be deactivated or revoked, removed from use, and treated as compromised.My secure Java checklist
Before allowing a Java application to call AWS, I ask:
- Can the workload use an IAM role?
- Are the credentials temporary?
- Is the policy limited to the required actions?
- Is access limited to the required resources?
- Are credentials absent from code, logs, images, and Git history?
- Can unused permissions and identities be reviewed and removed?
Writing Java code that successfully calls S3 is only half of the task. The other half is ensuring that the application can do exactly what it needs—and nothing more.
Which IAM concept was the hardest for you when you first connected an application to AWS?
Official AWS references
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article