Your container needs to access S3, DynamoDB, SQS or other AWS services, but putting an AWS access key and secret key inside the image is one of the last things you want to do. AWS already provides a better way: IAM roles and temporary credentials.
Instead of giving the container a permanent AWS identity, you attach permissions to the workload itself. AWS can then provide temporary credentials to the container when it runs.
The problem with hardcoded AWS keys
A common approach looks like this:
AWS_ACCESS_KEY_ID=AKIA...
AWS_SECRET_ACCESS_KEY=...
Developers may put these values inside a .env file, Docker image, CI/CD configuration or container definition.
The problem is that long-lived access keys remain valid until they are revoked or replaced. AWS recommends using temporary credentials with IAM roles for workloads whenever possible.
A better architecture looks like this:
Container
↓
IAM Role
↓
Temporary Credentials
↓
S3 / SQS / DynamoDB / AWS APIs
The application doesn't need to know the permanent AWS secret.
On Amazon ECS, use a Task IAM Role
For ECS and Fargate, the standard solution is an ECS task IAM role.
You create an IAM role containing only the permissions the application needs and attach that role to the ECS task definition.
For example:
{
"taskRoleArn": "arn:aws:iam::123456789012:role/my-app-task-role"
}
Now the application running inside that task can use AWS SDKs or the AWS CLI without storing access keys in the container.
ECS provides temporary role credentials to the containers through its container credential mechanism, and supported AWS SDKs can discover those credentials automatically.
Task Role and Execution Role are different
This is an important distinction that often causes confusion.
The task role gives your application permission to call AWS services.
The task execution role gives Amazon ECS permission to perform infrastructure-related operations such as pulling a private ECR image or sending logs to CloudWatch Logs.
So the simplified model is:
ECS Execution Role
↓
ECS pulls image / handles task operations
ECS Task Role
↓
Your application accesses S3 / SQS / DynamoDB
Giving application permissions to the execution role is not the same thing as giving them to the task.
Example: Container reading from S3
Suppose your application only needs to download files from one S3 bucket.
You can create an IAM policy such as:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::my-app-bucket/*"
}
]
}
Attach that policy to the task role and then reference the role from the ECS task definition.
Your application code can simply use the AWS SDK:
import boto3
s3 = boto3.client("s3")
response = s3.get_object(
Bucket="my-app-bucket",
Key="data.json"
)
data = response["Body"].read()
There is no access key in the Python code.
The AWS SDK obtains credentials through the container credential provider.
What about Docker containers on EC2?
When containers run directly on an EC2-based environment, developers sometimes attach a powerful IAM role to the EC2 instance and let every container inherit it.
That can give containers more permissions than they actually need.
For ECS, AWS recommends using task roles so permissions can be scoped to individual tasks or services rather than relying on the EC2 instance role.
That gives you a model like:
EC2 Instance Role
↓
ECS infrastructure
Task A → S3 read-only role
Task B → DynamoDB role
Task C → SQS role
Each workload gets only the access it needs.
What about Kubernetes and Amazon EKS?
For containers running on EKS, AWS provides EKS Pod Identity.
Instead of putting AWS credentials inside the Kubernetes Pod, you associate an IAM role with a Kubernetes service account.
The relationship becomes:
Kubernetes Service Account
↓
IAM Role
↓
Temporary AWS Credentials
↓
AWS Services
When a Pod uses a service account with an EKS Pod Identity association, EKS provides the credential configuration to the containers so supported AWS SDKs can use the role.
For example, a workload can use a dedicated service account:
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app
namespace: default
That service account can then be associated with an IAM role that contains only the permissions required by the application.
EKS also supports IAM Roles for Service Accounts, commonly called IRSA, which maps Kubernetes service accounts to IAM roles using the cluster's OIDC provider.
So should you use Secrets Manager instead?
Not for the AWS identity itself.
AWS recommends IAM roles and temporary credentials for workloads running on AWS. Secrets Manager is better suited to secrets that are actually secrets, such as database passwords, API tokens or third-party credentials that your application needs at runtime.
So instead of:
AWS access key
↓
Secrets Manager
↓
Container
for normal AWS service authentication, prefer:
Container
↓
IAM Role
↓
Temporary Credentials
↓
AWS Service
And use Secrets Manager for things like:
Stripe secret
Database password
Third-party API key
Webhook secret
What happens when the credentials expire?
This is one of the biggest advantages of using roles.
AWS temporary credentials have a limited lifetime. When they expire, they can no longer be used, and workloads can obtain fresh credentials through the role-based mechanism rather than relying on a permanent key embedded in the application.
That means you don't have to build a system that keeps replacing a hardcoded AWS secret in every container image.
The pattern to remember
Whether you're using ECS or EKS, the principle is the same:
Don't do this:
Container
↓
Hardcoded AWS_ACCESS_KEY_ID
+
Hardcoded AWS_SECRET_ACCESS_KEY
Do this:
Container
↓
IAM Role
↓
Temporary Credentials
↓
AWS Services
For ECS, that usually means an ECS Task IAM Role.
For EKS, that means EKS Pod Identity or IRSA.
For workloads that need non-AWS secrets, use a dedicated secret-management system such as AWS Secrets Manager.
Why this approach matters
Containers are frequently rebuilt, replicated and deployed across environments. Putting permanent AWS credentials inside those containers makes credential management harder and increases the impact of an accidental exposure.
IAM roles move the AWS identity out of the container image and into the infrastructure running the workload.
The modern approach is simple: don't give your container a permanent AWS key. Give the workload an IAM role and let AWS provide temporary credentials when the container needs them.



