Following could be of help:
- IAM Roles (If your application is already running in AWS, Instance Roles can help you fetch temporary tokens to access resources like S3. AWS CLIs OR official SDKs are already built with these capabilities and you need not implement any custom code. For this to work, you assign a Role 'X' to the EC2 instance, Role 'X' then needs to have a policy mapping, where you define the permissions)
A sample policy could be something like:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAccessToObjects",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:GetObjectVersion",
"s3:GetObjectVersionAcl",
"s3:ListMultipartUploadParts"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::<Bucket_Name>/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"w.x.y.z/32"
]
}
}
},
{
"Sid": "AllowAccessToBucket",
"Action": [
"s3:PutObject",
"s3:ListBucket",
"s3:GetBucketVersioning",
"s3:ListBucketVersions"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::<Bucket_Name>"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"a.b.c.d/32"
]
}
}
}
]
}
Usually the best approach is to never have any statically provisioned credentials at all. In case implementing something like that is not possible at all, then :
- Other options could be storing the secrets in an external secret store like Vault etc., and when the container starts, the secrets can be fetched and injected before bootstrapping the application. These are then available as ENVs