How to setup boto3 credentials

This tutorial explains how to configure AWS credentials for Boto3 Python APIs to make requests to AWS services. To configure Boto3 credentials, AWS Access Key ID and AWS Secret Access Key are required.

In this tutorial we will first create AWS IAM User along with AWS Access Key ID and AWS Secret Access Key and than will configure these for Boto3 authentication.

Create IAM User


1. Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/ .

2. In the navigation pane, choose Users.

3. Click on Add Users.

4. Enter username aws-cli-user and select Access key - Programmatic access.

5. Click on Next: Permissions.

6. Click on Select Attach existing policies directly and add required permissions.

Note: For this tutorial AmazonS3ReadOnlyAccess policy is attached, you have to add required permissions as per your requirement.

7. Click on Next: Tags, if you need to provide tags enter required tags or click on Next:Review.

8. Click on Create user.

9. Download the csv.

11. Click on close.

Boto3 credentials setup


12. Open the command line shell

13. Navigate to user home directory

   
  cd %HOMEPATH% 
   

14. Create a folder with name .aws and navigate to it.

   
  mkdir .aws

  cd .aws
   

15. Create two files with names config and credentials in .aws folder with below commands.

   
  copy NUL config

  copy NUL credentials
   

16. Write below configuration in config file, replace region as per the requirement and save the file.

   
  [default]
  region=us-east-1
   

17. From the download csv in Step 9 fetch the values of Access key ID and Secret access key.

18. Write below configuration in credentials file and replace value of aws_access_key_id, aws_access_key_id with the values from downloaded CSV and save the file.

   
  [default]
  aws_access_key_id = ACCESS_KEY_ID_FROM_CSV
  aws_secret_access_key = SECRET_ACCESS_KEY_FROM_CSV
   

14. Verify the configuration. Run below Python script, you should see list of S3 buckets from your account.

   
import boto3

client = boto3.client("s3")

response = client.list_buckets()
print(response)
 

Category: AWS