| 1 |
.. _guide_migration_ec2: |
| 2 |
|
| 3 |
Amazon EC2 |
| 4 |
========== |
| 5 |
Boto 2.x contains a number of customizations to make working with Amazon EC2 instances, storage and networks easy. Boto3 exposes these same objects through its resources interface in a unified and consistent way. |
| 6 |
|
| 7 |
Creating the connection |
| 8 |
----------------------- |
| 9 |
Boto3 has both low-level clients and higher-level resources. For Amazon EC2, the higher-level resources are the most similar to Boto 2.x's ``ec2`` and ``vpc`` modules:: |
| 10 |
|
| 11 |
# Boto 2.x |
| 12 |
import boto |
| 13 |
ec2_connection = boto.connect_ec2() |
| 14 |
vpc_connection = boto.connect_vpc() |
| 15 |
|
| 16 |
# Boto3 |
| 17 |
import boto3 |
| 18 |
ec2 = boto3.resource('ec2') |
| 19 |
|
| 20 |
Launching new instances |
| 21 |
----------------------- |
| 22 |
Launching new instances requires an image ID and the number of instances to launch. It can also take several optional parameters, such as the instance type and security group:: |
| 23 |
|
| 24 |
# Boto 2.x |
| 25 |
ec2_connection.run_instances('<ami-image-id>') |
| 26 |
|
| 27 |
# Boto3 |
| 28 |
ec2.create_instances(ImageId='<ami-image-id>', MinCount=1, MaxCount=5) |
| 29 |
|
| 30 |
Stopping and terminating instances |
| 31 |
-------------------------------- |
| 32 |
Stopping and terminating multiple instances given a list of instance IDs uses Boto3 collection filtering:: |
| 33 |
|
| 34 |
ids = ['instance-id-1', 'instance-id-2', ...] |
| 35 |
|
| 36 |
# Boto 2.x |
| 37 |
ec2_connection.stop_instances(instance_ids=ids) |
| 38 |
ec2_connection.terminate_instances(instance_ids=ids) |
| 39 |
|
| 40 |
# Boto3 |
| 41 |
ec2.instances.filter(InstanceIds=ids).stop() |
| 42 |
ec2.instances.filter(InstanceIds=ids).terminate() |
| 43 |
|
| 44 |
Checking what instances are running |
| 45 |
----------------------------------- |
| 46 |
Boto3 collections come in handy when listing all your running instances as well. Every collection exposes a ``filter`` method that allows you to pass additional parameters to the underlying service API operation. The EC2 instances collection takes a parameter called ``Filters`` which is a list of names and values, for example:: |
| 47 |
|
| 48 |
# Boto 2.x |
| 49 |
reservations = ec2_connection.get_all_reservations( |
| 50 |
filters={'instance-state-name': 'running'}) |
| 51 |
for reservation in reservations: |
| 52 |
for instance in reservation.instances: |
| 53 |
print(instance.instance_id, instance.instance_type) |
| 54 |
|
| 55 |
# Boto3 |
| 56 |
# Use the filter() method of the instances collection to retrieve |
| 57 |
# all running EC2 instances. |
| 58 |
instances = ec2.instances.filter( |
| 59 |
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) |
| 60 |
for instance in instances: |
| 61 |
print(instance.id, instance.instance_type) |
| 62 |
|
| 63 |
Checking health status of instances |
| 64 |
----------------------------------- |
| 65 |
It is possible to get scheduled maintenance information for your running instances. At the time of this writing Boto3 does not have a status resource, so you must drop down to the low-level client via ``ec2.meta.client``:: |
| 66 |
|
| 67 |
# Boto 2.x |
| 68 |
for status in ec2_connection.get_all_instance_statuses(): |
| 69 |
print(status) |
| 70 |
|
| 71 |
# Boto3 |
| 72 |
for status in ec2.meta.client.describe_instance_status()['InstanceStatuses']: |
| 73 |
print(status) |
| 74 |
|
| 75 |
Working with EBS snapshots |
| 76 |
-------------------------- |
| 77 |
Snapshots provide a way to create a copy of an EBS volume, as well as make new volumes from the snapshot which can be attached to an instance:: |
| 78 |
|
| 79 |
# Boto 2.x |
| 80 |
snapshot = ec2_connection.create_snapshot('volume-id', 'Description') |
| 81 |
volume = snapshot.create_volume('us-west-2') |
| 82 |
ec2_connection.attach_volume(volume.id, 'instance-id', '/dev/sdy') |
| 83 |
ec2_connection.delete_snapshot(snapshot.id) |
| 84 |
|
| 85 |
# Boto3 |
| 86 |
snapshot = ec2.create_snapshot(VolumeId='volume-id', Description='description') |
| 87 |
volume = ec2.create_volume(SnapshotId=snapshot.id, AvailabilityZone='us-west-2a') |
| 88 |
ec2.Instance('instance-id').attach_volume(VolumeId=volume.id, Device='/dev/sdy') |
| 89 |
snapshot.delete() |
| 90 |
|
| 91 |
Creating a VPC, subnet, and gateway |
| 92 |
----------------------------------- |
| 93 |
Creating VPC resources in Boto3 is very similar to Boto 2.x:: |
| 94 |
|
| 95 |
# Boto 2.x |
| 96 |
vpc = vpc_connection.create_vpc('10.0.0.0/24') |
| 97 |
subnet = vpc_connection.create_subnet(vpc.id, '10.0.0.0/25') |
| 98 |
gateway = vpc_connection.create_internet_gateway() |
| 99 |
|
| 100 |
# Boto3 |
| 101 |
vpc = ec2.create_vpc(CidrBlock='10.0.0.0/24') |
| 102 |
subnet = vpc.create_subnet(CidrBlock='10.0.0.0/25') |
| 103 |
gateway = ec2.create_internet_gateway() |
| 104 |
|
| 105 |
Attaching and detaching an elastic IP and gateway |
| 106 |
------------------------------------------------- |
| 107 |
Elastic IPs and gateways provide a way for instances inside of a VPC to communicate with the outside world:: |
| 108 |
|
| 109 |
# Boto 2.x |
| 110 |
ec2_connection.attach_internet_gateway(gateway.id, vpc.id) |
| 111 |
ec2_connection.detach_internet_gateway(gateway.id, vpc.id) |
| 112 |
|
| 113 |
from boto.ec2.address import Address |
| 114 |
address = Address() |
| 115 |
address.allocation_id = 'eipalloc-35cf685d' |
| 116 |
address.associate('i-71b2f60b') |
| 117 |
address.disassociate() |
| 118 |
|
| 119 |
# Boto3 |
| 120 |
gateway.attach_to_vpc(VpcId=vpc.id) |
| 121 |
gateway.detach_from_vpc(VpcId=vpc.id) |
| 122 |
|
| 123 |
address = ec2.VpcAddress('eipalloc-35cf685d') |
| 124 |
address.associate('i-71b2f60b') |
| 125 |
address.association.delete() |