2

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.

enter image description here

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.

enter image description here

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.

enter image description here

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?

aveek
  • 188
  • 6

2 Answers2

2

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.

Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • Thank you for your answer, @ankush-jain. As I already mentioned in the question, I am not looking to explicitly define the profile name in the codebase, because: 1) Multiple developers use the same codebase and may have configured different profile names in their local machines. 2) With the profile name defined in the code, when I deploy it to Lambda, it throws an exception - "_The config profile (PROFILE-NAME) could not be found_". While using the environment variable, it impacts my other applications that connect to different AWS accounts, so this is not an option. – aveek Apr 01 '22 at 12:06
  • In that case, you can set process level environment variable while working on local, and when you deploy to Lambda, you can use Lambda execution role rather than relying on some profile. – Ankush Jain Apr 01 '22 at 12:30
1

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.

Carlos
  • 361
  • 3
  • 7