How to connect different AWS services using Boto3 library in Python?


In this article, we will see how you can use Boto3 library in Python to connect with different AWS services.

Example

  • Connect with AWS S3.

  • Connect with AWS Glue Job

  • Connect with AWS SQS and many more.

Approach/Algorithm to solve this problem

Step 1 − Create an AWS session using Boto3 library.

Step 2 − Pass the AWS service name in client to get a low-level service access.

Or, pass the AWS service name in resource to get high-level object-oriented service access/highlevel interface.

Example

The following code connects with different AWS services −

import boto3
# To get AWS Client
def getconnection_AWSClient(service_name):
   session = boto3.session.Session()
   # User can pass customized access key, secret_key and token as well
   s3_client = session.client(service_name)
   return s3_client
print(getconnection_AWSClient('s3')) # for s3 connection
print(getconnection_AWSClient('glue')) # for glue connection
print(getconnection_AWSClient('sqs')) # for sqs connection and other services

# To get AWS Resource
def getconnection_AWSResource(service_name):
   session = boto3.session.Session()
   # User can pass customized access key, secret_key and token as well
   s3_resource = session.resource(service_name)
   return s3_resource

print(getconnection_AWSResource('s3')) # for s3 connection
print(getconnection_AWSResource('sqs')) # for sqs connection and other services

Output

<botocore.client.S3 object at 0x00000216C4CB89B0>
<botocore.client.Glue object at 0x00000216C5129358>
<botocore.client.SQS object at 0x00000216C4E03E10>
s3.ServiceResource()
sqs.ServiceResource()

Note that resource doesn’t support all the services to connect. For example, if the user tries to connect glue service using resource, then AWS throws the following exception −

boto3.exceptions.ResourceNotExistsError: The 'glue' resource does not exist.

Consider using a boto3.client('glue') instead of a resource for 'glue'

Following services are supported by resource −

  • cloudformation

  • cloudwatch

  • dynamodb

  • ec2

  • glacier

  • iam

  • opsworks

  • s3

  • sns

  • sqs

Updated on: 22-Mar-2021

543 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements