Excellent question. Using a FROM scratch container is a great practice for creating minimal, secure Go applications. However, as you've noted, it presents a challenge because it lacks a shell, standard directories (/home, /root), and other common utilities that credential-loading mechanisms often rely on.
With the AWS Go SDK v2, the credential loading follows a specific default provider chain. We need to make one of these providers work within the constraints of a scratch container.
Here are the methods, ordered from most recommended (best practice) to least recommended.
The Best Practice: IAM Roles for Tasks/Instances (Most Secure)
This is the most secure and recommended approach for any application running on AWS infrastructure (like EC2, ECS, or EKS). It involves no static credentials.
How it works:
The AWS Go SDK v2 will automatically look for credentials provided by the environment it's running in.
- On ECS (Elastic Container Service): Use an IAM Role for Tasks. The ECS agent injects a special environment variable (
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) into your container. The SDK sees this and fetches temporary, auto-rotating credentials from a local metadata endpoint.
- On EKS (Elastic Kubernetes Service): Use IAM Roles for Service Accounts (IRSA). Kubernetes injects a web identity token and environment variables (
AWS_ROLE_ARN, AWS_WEB_IDENTITY_TOKEN_FILE) into your pod. The SDK uses these to assume the specified IAM role.
- On EC2 Instances: Use an IAM Instance Profile. The SDK queries the EC2 metadata service (
169.254.169.254) to get temporary credentials.
Why this is ideal for FROM scratch:
Your Go binary makes a direct HTTP call to the metadata endpoint. It doesn't need a shell, a filesystem, or any special tools. It just works out of the box.
What you need to do:
- In your Go code: Absolutely nothing special. Just use the default config loader.
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
// Now use cfg to create a service client, e.g., S3
// client := s3.NewFromConfig(cfg)
- In your Dockerfile: Nothing special is needed for credentials. Your Dockerfile remains minimal.
- In your infrastructure: Configure the appropriate IAM role and associate it with your ECS Task Definition, Kubernetes Service Account, or EC2 instance.
This is the cleanest, most secure, and most "cloud-native" way to handle credentials.
Alternative 1: Using a Credentials File (For local dev or specific CI)
If you aren't running on AWS infrastructure or need to use a static credentials file, you can't rely on the default ~/.aws/credentials path because the ~ (home directory) concept doesn't exist in a scratch container.
The Solution:
COPY the credentials file to a hardcoded path inside your container.
- Use the
AWS_SHARED_CREDENTIALS_FILE environment variable to tell the SDK where to find it.
Example:
1. Your local credentials file:
Create a file named credentials (or similar) in the same directory as your Dockerfile.
# ./credentials
[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
Warning: Be very careful not to commit this file to source control. Add it to your .gitignore.
2. Your Dockerfile:
# ---- Builder Stage ----
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Build a static, non-CGO binary for the scratch container
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# ---- Final Stage ----
FROM scratch
# Set a working directory
WORKDIR /app
# Copy the static binary from the builder stage
COPY --from=builder /app/main .
# Copy the credentials file to a known location
COPY credentials /app/.aws/credentials
# Tell the AWS SDK where to find the credentials file.
# You can also set AWS_REGION here.
ENV AWS_SHARED_CREDENTIALS_FILE="/app/.aws/credentials"
ENV AWS_REGION="us-east-1"
# Command to run the executable
CMD ["/app/main"]
Now, when your Go application starts, the SDK will check the AWS_SHARED_CREDENTIALS_FILE environment variable and load the credentials from /app/.aws/credentials.
Alternative 2: Environment Variables (Discouraged for Production)
This method involves passing credentials directly as environment variables. It's simple but has significant security drawbacks.
How it works:
The AWS SDK will automatically read the following environment variables if they are present:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN (optional, for temporary credentials)
AWS_REGION
How to implement it:
You do not put these in your Dockerfile with the ENV instruction, as that would bake them into the image layer, making them visible to anyone who can inspect the image.
Instead, you provide them at runtime.
1. Your Dockerfile (minimal):
# ---- Builder Stage ----
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
# ---- Final Stage ----
FROM scratch
WORKDIR /app
COPY --from=builder /app/main .
CMD ["/app/main"]
2. Your docker run command:
docker run --rm \
-e AWS_ACCESS_KEY_ID="YOUR_KEY" \
-e AWS_SECRET_ACCESS_KEY="YOUR_SECRET" \
-e AWS_REGION="us-east-1" \
your-go-app-image
Security Warning: While better than baking them into the image, these credentials can still be exposed via docker inspect on a running container and might be logged by orchestration systems. This method is best reserved for temporary local development.
Summary and Recommendation
| Method | How it Works | Pros | Cons | Recommendation |
|---|
| IAM Roles | SDK auto-detects credentials from the AWS environment (ECS, EKS, EC2). | Most Secure. No static keys. Auto-rotating. No code or Dockerfile changes needed. | Only works when running on AWS infrastructure. | Highly Recommended for all AWS deployments. |
| Credentials File | COPY a file into the container and set AWS_SHARED_CREDENTIALS_FILE env var. | Keeps credentials out of runtime environment variables. | Involves storing a static credential file in the image layer, which is a security risk. Requires file management. | Acceptable for local dev or specific CI/CD scenarios where IAM Roles are not an option. |
| Environment Vars | Pass AWS_ACCESS_KEY_ID, etc., via docker run -e. | Simple for quick local tests. | Least Secure. Credentials can be inspected in running containers and may be logged. | Discouraged for production. Use only for ephemeral local development. |