Amazon Web Services

Python development with Boto

Outline

  • Introduction & Installation
  • General concepts
  • Zoom on EC2 and ELB

Introduction & Installation

History

  • A Python library for AWS
  • Official support from AWS
  • Python 2.6+ and Python 3
    • APIs are quite different!

Installation

  1. pip install boto3
    or
  2. apt-get install python-boto

General principles

All supported services are accessed with a specific client.

2 types of clients:

  • High-level (EC2, S3, SQS, DynamoDB, …)
1ec2 = boto3.resource('ec2',
2    aws_access_key_id='<access>',
3    aws_secret_access_key='<secret>',
4    region_name='<region>')
  • Low-level services (ELB, EMR, CloudFront, …)
1elb = boto3.client('elb',
2    aws_access_key_id='<access>',
3    aws_secret_access_key='<secret>',
4    region_name='<region>')

Model

The developper manipulates object that represent AWS entities

  • Instance, SecurityGroup, Bucket
  • To instantiate them:
    instance = ec2.Instance('<instance-id>')
  • To refresh the state of an object:
    instance.update()

http://boto3.readthedocs.io/

EC2

Run instances

Run instances:

1instances = ec2.create_instances( \
2        ImageId='<ami id>', \
3        KeyName='<key-pair name>', \
4        InstanceType='<instance-type>', \
5        SecurityGroupIds=['<sec-group-id>', '<sec-group-id>'], \
6        MinCount=<n-instances>, MaxCount=<n-instances>, \
7        UserData='<user-data string>')

Wait for an instance to be running:

1instances[0].wait_until_running()

Instances

Common operations:

  • .start()
  • .stop()
  • .terminate()
  • .reboot()
  • .add_tag()/.remove_tag()
  • .update()

Reservations

A collection of instances resulting from a single run_instances() call

  • .stop_all()

Get instances with a filter:

1ec2.describe_instances(Filters=[
2        {
3                'Name': 'tag:Name',
4                'Values': ['<name>']
5        }
6])

ELB

Create a load balancer

Create a load balancer:

 1listener = {
 2        'Protocol': 'HTTP',
 3        'LoadBalancerPort': <frontend port>,
 4        'InstancePort': <backend port>
 5}
 6
 7my_elb = elb.create_load_balancer(LoadBalancerName=<name>, \
 8    AvailabilityZones=['<zone-1>', '<zone-2>'], \
 9    Listeners=[listener])

Get existing load balancers:

1elb.describe_load_balancers()

Set the Health Check

 1elb.configure_health_check(LoadBalancerName=ELB_NAME, \
 2    HealthCheck={
 3        'Target':             'HTTP:80/health',
 4        'Interval':           10,
 5        'Timeout':            5,
 6        'UnhealthyThreshold': 2,
 7        'HealthyThreshold':   3
 8    }
 9)

Register an instance

Register an instance to the load balancer:

1elb.register_instances_with_load_balancer(LoadBalancerName='<load balancer name>', \
2    Instances=['<instance id>'])

Deregister the instance:

1elb.deregister_instances_from_load_balancer(LoadBalancerName='<load balancer name>', \
2    Instances=['<instance id>'])

Manipulate availability zones

Enable an availability zone:

1elb.enable_availability_zones_for_load_balancer( \
2    LoadBalancerName='<load balancer name>', \
3    AvailabilityZones=['<zone>'])

Disable an availability zone:

1elb.disable_availability_zones_for_load_balancer( \
2    LoadBalancerName='<load balancer name>', \
3    AvailabilityZones=['<zone>'])