Reputation: 338
I have two AWS Accounts configured in my local. One with the default profile and another with a named profile. I am able to use the AWS CLI to connect to both the accounts and perform operations as follows.
I need to do the same using python and I am using VSCode. I have installed the AWS Toolkit for VSCode and now I can view the list of buckets from both accounts by selecting the correct AWS profile.
However, when I run my python code with the named profile selected, I receive the list of buckets from the default profile and not from the named profile. This is true for all AWS services and not only S3. It seems changing the AWS Profile in VSCode does not affect the runtime. It connects to the default AWS profile only.
Did anyone else face similar issues in the past? How can I mitigate this and connect to the named profile without explicitly creating a Boto3 session?
Upvotes: 2
Views: 2772
Reputation: 371
Take a look at this question on Stack Overflow? I'm referring to the answer provided in the following link: Stack Overflow Answer
It turns out that adding "env": { "AWS_PROFILE": "dev-user"} to your configuration in launch.json does work.
Upvotes: 1
Reputation: 7069
Your Python code will not consider the selected profile in Visual Studio Code.
All AWS profiles are stored in a credential file stored on your local machine. To explicitly select a named profile, you can try following options:
Change the profile of the default session in code
boto3.setup_default_session(profile_name='profile-name')
Create an environment variable as per below
AWS_PROFILE='profile-name'
SDK will load the profile name from this environment variable if it is present there, otherwise it will use default
as profile name.
Upvotes: 2