A developer is containerizing a Go application that retrieves messages from an Amazon SQS queue. For local testing, the application runs inside a Docker container on a local workstation. The developer has configured the AWS CLI on the host workstation with a default profile, and the CLI successfully connects to SQS. However, when the containerized application runs, it fails with a credentials provider error indicating that no credentials could be found. Which of the following is the most secure and appropriate way to resolve this credential error in the local development environment?
- Pass the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables to the container at runtime using the docker run command with environment flags.Answer
- BMount the host machine's ~/.aws directory into the container and set the AWS_SDK_LOAD_CONFIG environment variable inside the container to true, so it reads the configuration from the global credentials file before looking at environment variables.
- CHardcode the AWS Access Key ID and Secret Access Key into the Go SDK client initialization block of the application code specifically for the local testing phase.
- DCreate an IAM role with the required SQS permissions and configure a trust policy that allows the local container's IP address to assume the role, then update the container's configuration file.
Answer
Pass the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables to the container at runtime using the docker run command with environment flags.
Passing the environment variables to the container at runtime resolves the credentials error because container environments are isolated by default. The default credential provider chain in the AWS SDK checks environment variables first before checking configuration files or IAM roles, allowing the application to successfully retrieve credentials passed via the environment flags.
Step-by-Step Solution
Key Concept
AWS SDK Default Credential Provider Chain and Container Environment Isolation
Estimated Time:1m 30s