100 Days of DevOps — Day 99- AWS  Boto3

What is Boto3?

Boto3 is the Amazon Web Services (AWS) SDK for Python. It enables Python developers to create, configure, and manage AWS services, such as EC2 and S3. Boto3 provides an easy to use, object-oriented API, as well as low-level access to AWS services.

Boto3 is built on the top of a library called Botocore, which is shared by the AWS CLI. Botocore provides the low level clients, session and credentials and configuration data. Boto3 built on the top of Botocore by providing its own session, resources, collections, waiters and paginators.

Botocore is the basis for the aws-cli.

https://github.com/boto/boto3

https://github.com/boto/botocore

Installing boto3

$ pip3 install boto3 --user
  • --user makes pip install packages in your home directory instead, which doesn’t require any special privileges.
$ aws configure
AWS Access Key ID [****************XXXX]:
AWS Secret Access Key [****************XXXX]:
Default region name [us-west-2]:
Default output format [json]:
Configure your aws credentials
To test it
$ aws sts get-caller-identity
{
"Account": "1234567889",
"UserId": "XXXXXXXXX",
"Arn": "arn:aws:iam::1234567889:user/plakhera"
}

Some key terms

  • Session: A session manages the state about a particular configuration. A default session is created for when needed but we can create our own session. A typical session looks like this
>>> session=boto3.session.Session(profile_name=“<profile>”)
  • profile name is the created using aws configure command, else default profile is selected
>>> aws configure --profile <profile>

* As you can see profile usually contains

Credentials
Region
  • Resources: Resources represent an object-oriented interface to AWS services. It provides a higher-level abstraction than the raw, low-level calls made by service clients
>>> ec2_con_resource=session_resource(service_name=“ec2”,
region_name=“us-west-2”)

* Not all aws services supports resources
>>> session=boto3.session.Session(profile_name=“<profile>”)

>>> session.get_available_resources()
['cloudformation', 'cloudwatch', 'dynamodb', 'ec2', 
'glacier', 'iam', 'opsworks', 's3', 'sns', 'sqs']
>>> s3 = boto3.resource('s3')
  • Clients: Clients provides a low-level interface to AWS whose methods map close to 1:1 with service API. Clients are generated from a JSON service definition file.
  • The important point to remember all service operations are supported by clients.
>>> session=boto3.session.Session(profile_name=“<profile>”)
>>> session.get_available_services()
['acm', 'acm-pca', 'alexaforbusiness', 'amplify', 'apigateway', 'apigatewaymanagementapi', 'apigatewayv2', 'application-autoscaling', 'appmesh', 'appstream', 'appsync', 'athena', 'autoscaling', 'autoscaling-plans', 'backup', 'batch', 'budgets', 'ce', 'chime', 'cloud9', 'clouddirectory', 'cloudformation', 'cloudfront', 'cloudhsm', 'cloudhsmv2', 'cloudsearch', 'cloudsearchdomain', 'cloudtrail', 'cloudwatch', 'codebuild', 'codecommit', 'codedeploy', 'codepipeline', 'codestar', 'cognito-identity', 'cognito-idp', 'cognito-sync', 'comprehend', 'comprehendmedical', 'config', 'connect', 'cur', 'datapipeline', 'datasync', 'dax', 'devicefarm', 'directconnect', 'discovery', 'dlm', 'dms', 'docdb', 'ds', 'dynamodb', 'dynamodbstreams', 'ec2', 'ecr', 'ecs', 'efs', 'eks', 'elasticache', 'elasticbeanstalk', 'elastictranscoder', 'elb', 'elbv2', 'emr', 'es', 'events', 'firehose', 'fms', 'fsx', 'gamelift', 'glacier', 'globalaccelerator', 'glue', 'greengrass', 'guardduty', 'health', 'iam', 'importexport', 'inspector', 'iot', 'iot-data', 'iot-jobs-data', 'iot1click-devices', 'iot1click-projects', 'iotanalytics', 'kafka', 'kinesis', 'kinesis-video-archived-media', 'kinesis-video-media', 'kinesisanalytics', 'kinesisanalyticsv2', 'kinesisvideo', 'kms', 'lambda', 'lex-models', 'lex-runtime', 'license-manager', 'lightsail', 'logs', 'machinelearning', 'macie', 'marketplace-entitlement', 'marketplacecommerceanalytics', 'mediaconnect', 'mediaconvert', 'medialive', 'mediapackage', 'mediastore', 'mediastore-data', 'mediatailor', 'meteringmarketplace', 'mgh', 'mobile', 'mq', 'mturk', 'neptune', 'opsworks', 'opsworkscm', 'organizations', 'pi', 'pinpoint', 'pinpoint-email', 'pinpoint-sms-voice', 'polly', 'pricing', 'quicksight', 'ram', 'rds', 'rds-data', 'redshift', 'rekognition', 'resource-groups', 'resourcegroupstaggingapi', 'robomaker', 'route53', 'route53domains', 'route53resolver', 's3', 's3control', 'sagemaker', 'sagemaker-runtime', 'sdb', 'secretsmanager', 'securityhub', 'serverlessrepo', 'servicecatalog', 'servicediscovery', 'ses', 'shield', 'signer', 'sms', 'sms-voice', 'snowball', 'sns', 'sqs', 'ssm', 'stepfunctions', 'storagegateway', 'sts', 'support', 'swf', 'textract', 'transcribe', 'transfer', 'translate', 'waf', 'waf-regional', 'workdocs', 'worklink', 'workmail', 'workspaces', 'xray']
>>> sqs = boto3.client('sqs')

Task1: List out all S3 buckets using Boto3

>>> import boto3
# Create high level resource using boto3
>>> s3 = boto3.resource('s3')
# Print all the bucket name
>>> for bucket in s3.buckets.all():
...     print(bucket.name)
  • Resources represent an object-oriented interface to AWS services. They provide a higher-level abstraction than the raw low level calls made by service clients.

Task2: Create an EC2 instance using Boto3

import boto3
# Create a low-level client with the service name
ec2
= boto3.client('ec2')
# When we invoke client we always got response back
response = ec2.run_instances(
    ImageId='ami-01ed306a12b7d1c96',
    InstanceType='t2.micro',
    KeyName='testkey',
    MinCount=1,
    MaxCount=1
)
print(response)
  • Clients provide a low level interface to AWS whose methods map close to 1:1 with services API. All service operations are supported by clients. Clients are generated from a JSON service definition file.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.run_instances

GitHub Link https://github.com/100daysofdevops/100daysofdevops/tree/master/boto3

Reference

https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

https://aws.amazon.com/sdk-for-python/

23 Replies to “100 Days of DevOps — Day 99- AWS  Boto3”

  1. Thank you, I have just been looking for information about this subject for a long time and yours is the best I’ve discovered so far. But, what in regards to the conclusion? Are you certain about the supply?|

  2. It is truly a great and useful piece of information. I am satisfied that you just shared this useful info with us. Please stay us up to date like this. Thanks for sharing.|

  3. I just couldn’t go away your site prior to suggesting that I extremely enjoyed the standard information a person provide in your visitors? Is going to be again continuously to check up on new posts|

  4. Hello there, simply became aware of your weblog through Google, and located that it is truly informative. I’m gonna watch out for brussels. I will be grateful in case you proceed this in future. Numerous people will be benefited from your writing. Cheers!|

  5. You actually make it appear so easy with your presentation however I to find this matter to be actually something which I believe I would never understand. It seems too complicated and extremely vast for me. I’m taking a look ahead on your subsequent post, I’ll try to get the hold of it!|

  6. That is very fascinating, You’re a very skilled blogger. I have joined your feed and sit up for in the hunt for more of your wonderful post. Additionally, I have shared your website in my social networks|

    1. Hi Wilfredo

      If you scroll to the right, you will see something like

      CONNECT WITH 100 DAYS OF DEVOPS
      Subscribe to 100 Days of DevOps by Email

      Please let me know if you are facing any issue.

  7. Hi! This is my 1st comment here so I just wanted to give a quick shout out and say I really enjoy reading through your posts. Can you suggest any other blogs/websites/forums that deal with the same topics? Thanks!|

Comments are closed.