iTranslated by AI
Stop Hardcoding Access Keys: Eliminate Leakage Risks by Using IAM Roles for EC2
*This page contains promotional content.
What you will learn in this article:
- Why you should never run
aws configureinside an EC2 instance. - The risk of receiving a 1,000,000 JPY bill via GitHub if you hardcode access keys (AKIA...) in your code.
- How to eliminate key management entirely by attaching an "IAM role" to your server.
▼ Recommended Book: Amazon Web Services: Network & Server Construction from the Basics
Introduction: Read this before pushing that code to GitHub!
If your program contains code like this, take your hands off the keyboard and read this article immediately.
# 😱 A dangerous practice you must never do
s3 = boto3.client(
's3',
aws_access_key_id='AKIAXXXXXXXXXXXXXXXX', # Hardcoding keys
aws_secret_access_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
)
Do you know what happens if you accidentally push this to a public GitHub repository? Within minutes, bots from all over the world will find it, spin up numerous servers for Bitcoin mining on your AWS account, and you will receive a bill exceeding 1,000,000 JPY the following month. This is not a scare tactic; it is a real accident that happens to people.
"So, can't I just use environment variables or aws configure to store them in a settings file?"
No. If you are running code on servers like EC2, even that is unnecessary. Today, I will explain the correct way to handle "key management" on servers: assigning an IAM role (instance profile).
1. Why should you never give a server "access keys"?
In my previous articles, I explained that you should avoid using "IAM users (fixed keys)" as much as possible. This holds true for servers (EC2) as well.
Storing access keys inside an EC2 instance using aws configure is like "leaving the key to a safe inside a warehouse that might be broken into by thieves."
What happens if a hacker exploits a vulnerability in your web application and gains access to your server?
They will find the configuration file (~/.aws/credentials), steal the keys, and the damage will expand to your entire AWS environment.
"Then how do I grant permissions? I need keys to access S3!"
This is where IAM roles come into play.
2. Letting your server wear a "hat"
Just as you (the human) use "switch roles" to wear an IAM role hat, you can also have your server (EC2) wear one.
This is technically called an "instance profile," but it is essentially a feature to "attach an IAM role to EC2."
▼ [Diagram] Access Keys vs. IAM Role

Why is this so great?
The biggest advantage of this mechanism is that "AWS manages the keys for you automatically."
When you assign a role to an EC2 instance, temporary keys (AssumeRole) are issued behind the scenes. Amazingly, AWS automatically replaces (rotates) these keys with new ones every few hours.
You do not need to edit configuration files. You do not need to write keys in your code. Humans do not need to do anything. Just let the server wear the "hat." This is the ultimate form of security.
3. Configuration is done in just "3 clicks"
Let's try it out. We will grant the EC2 instance permission to "list S3 buckets" (ListBucket).
Step 1: Create an IAM Role
Create a role in the IAM console. The key point here is the "Trusted entity".
- Trusted entity type: AWS service
- Use case: EC2 ← This is important!
- Permission policy: AmazonS3ReadOnlyAccess (for reading only, for now)
With this, the "hat that only EC2 can wear" is ready.
Step 2: Assign it to the EC2 instance
Go to the EC2 management console.
- Right-click on the target instance.
- Select Security > Modify IAM role.
- Choose the role you just created and click "Update IAM role."
That's it. No restart is required. It is applied immediately.
Step 3: Verify
SSH into your EC2 instance and try running the AWS CLI. Of course, this assumes you have not run aws configure (i.e., no configuration files exist).
# Confirm that no configuration file exists
$ ls ~/.aws/credentials
ls: cannot access ...: No such file or directory
# But the S3 command works!
$ aws s3 ls
2026-01-22 10:00:00 my-bucket-A
2026-01-22 10:00:00 my-bucket-B
"The command worked even without a configuration file!" This is the power of an IAM role. Your Python (boto3) code can also be simplified by removing all hardcoded access keys and just writing boto3.client('s3') to make it work.
4. aws configure is for your "local PC"
Summarizing what we have discussed so far, the purpose of aws configure, the initial setup command for the AWS CLI, becomes clear.
-
Local PC (your Mac/Windows):
- It is okay to use it. However, if you are using switch roles, you often don't even need access keys.
-
EC2 / Lambda / ECS (Resources on AWS):
- Never use it.
- The correct approach is to assign an IAM role (task role).
"Your hands should shake" when you feel like typing aws configure within an AWS environment; that level of caution is just right.
Conclusion: Break free from the "labor" of access key management
Issuing, storing, rotating, and preventing the leakage of access keys... These cumbersome management tasks are not work for humans.
By correctly utilizing AWS features, you can create a secure environment that is "credentialless" (i.e., you do not have keys that need to be managed in the first place).
- Humans use switch roles.
- Servers use IAM roles (instance profiles).
Adopt these two practices thoroughly and start living a "key-free life" from today!
Further Reading
The concepts of "IAM roles (hats)" and how to configure "switch roles," which are prerequisites for this article, are illustrated in the articles below. They will deepen your understanding!
▼ Why do AWS professionals not use "IAM users"? ▼ The magic of instantly switching AWS accounts with "switch roles"
📚 Let's actually build a "key-free server"
"I understand the logic of IAM roles. But I'm not confident in launching EC2 instances or building networks..."
For such AWS beginners, I recommend this book.
Through a hands-on approach where you build a web server while actively working with the system, you can experience how to use the IAM settings explained in this article in a real-world scenario.
If you aim to go "beyond a beginner," going through this book once is the best shortcut.
▼ Amazon Web Services: Network & Server Construction from the Basics
Discussion