A developer is writing a Python application using the AWS SDK for Python (Boto3) to upload files to an Amazon S3 bucket. During local development, the application must use credentials from a shared credentials file under a profile named 'local-dev'. Once deployed to an Amazon ECS task on AWS Fargate, the application must assume the ECS task role to access the S3 bucket. The developer wants to avoid making any code changes when transitioning the application from the local environment to AWS Fargate. Which configuration approach should the developer use to meet these requirements?
- Initialize the S3 client using `boto3.client('s3')` without specifying credentials or profiles in the code, and configure the `AWS_PROFILE` environment variable on the local workstation.Answer
- BInitialize the S3 client by passing the profile name directly in the code using `boto3.Session(profile_name='local-dev').client('s3')`, and package the shared credentials file containing that profile inside the container image.
- CStore the local AWS access keys in the AWS Systems Manager Parameter Store, retrieve them programmatically on application startup, and initialize the client using `boto3.client('s3', aws_access_key_id=..., aws_secret_access_key=...)`.
- DModify the trust policy of the ECS task execution role to trust the developer's local IAM user, and configure the application to dynamically call the AWS Security Token Service (STS) to assume the role on startup.
Answer
Initialize the S3 client using `boto3.client('s3')` without specifying credentials or profiles in the code, and configure the `AWS_PROFILE` environment variable on the local workstation.
Initializing the Boto3 client using the default configuration (without passing explicit credentials or profiles) ensures that the SDK uses its default credential provider chain. Setting the `AWS_PROFILE` environment variable on the local machine tells Boto3 to read from the local credentials file under the specified profile. When deployed to AWS Fargate, because that environment variable is not present, the credential chain naturally proceeds to search for ECS task role credentials, enabling a seamless transition without modifying the code.
Step-by-Step Solution
Key Concept
AWS SDK Default Credential Provider Chain Resolution
Estimated Time:1m 30s