Automating AWS ec2 With Python and Boto3

Views: 3734

Automating Aws with Python – boto3

I am starting to use Python instead of Bash and OpenShell. The first plan is to make a scripted Geo Web Server. The customers will launch servers in as many regions as they like. The web users will receive pages from the local server, the content can be mixed with replicated content and local content.

boto3 create ami from instance

You can also create Images from your running instances (they should be stopped for the moment)

To create AMI from an instance using boto3 see:


    For our first script, let’s list the instances we have running in EC2. We can get this information with just a few short lines of code.

    First, we’ll import the boto3 library. Using the library, we’ll create an EC2 resource. This is like a handle to the EC2 console that we can use in our script. Finally, we’ll use the EC2 resource to get all of the instances and then print their instance ID and state. Here’s what the script looks like:

    #!/usr/bin/env python
    import boto3
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        print, instance.state

    Save the lines above into a file named and change the mode to executable. That will allow you to run the script directly from the command line. Also note that you’ll need to edit and chmod +x the remaining scripts to get them running as well. In this case, the procedure looks like this:

    $ vi
    $ chmod +x
    $ ./

    If you haven’t created any instances, running this script won’t produce any output. So let’s fix that by moving on to the the next step and creating some instances.

    One of the key pieces of information we need for scripting EC2 is an Amazon Machine Image (AMI) ID. This will let us tell our script what type of EC2 instance to create. While getting an AMI ID can be done programmatically, that’s an advanced topic beyond the scope of this tutorial. For now, let’s go back to the AWS console and get an ID from there.

    In the AWS console, go the the EC2 service and click the “Launch Instance” button. On the next screen, you’re presented with a list of AMIs you can use to create instances. Let’s focus on the Amazon Linux AMI at the very top of the list. Make a note of the AMI ID to the right of the name. In this example, its “ami-1e299d7e.” That’s the value we need for our script. Note that AMI IDs differ across regions and are updated often so the latest ID for the Amazon Linux AMI may be different for you.


    Now with the AMI ID, we can complete our script. Following the pattern from the previous script, we’ll import the boto3 library and use it to create an EC2 resource. Then we’ll call the create_instances() function, passing in the image ID, max and min counts, and the instance type. We can capture the output of the function call which is an instance object. For reference, we can print the instance’s ID.

    #!/usr/bin/env python
    import boto3
    ec2 = boto3.resource('ec2')
    instance = ec2.create_instances(
    print instance[0].id

    While the command will finish quickly, it will take some time for the instance to be created. Run the script several times to see the state of the instance change from pending to running.

    Now that we can programmatically create and list instances, we also need a method to terminate them.

    For this script, we’ll follow the same pattern as before with importing the boto3 library and creating an EC2 resource. But we’ll also take one parameter: the ID of the instance to be terminated. To keep things simple, we’ll consider any argument to the script to be an instance ID. We’ll use that ID to get a connection to the instance from the EC2 resource and then call the terminate() function on that instance. Finally, we print the response from the terminate function. Here’s what the script looks like:

    #!/usr/bin/env python
    import sys
    import boto3
    ec2 = boto3.resource('ec2')
    for instance_id in sys.argv[1:]:
        instance = ec2.Instance(instance_id)
        response = instance.terminate()
        print response

    Run the script to see what instances are available. Note one of the instance IDs to use as input to the script. After running the terminate script, we can run the list instances script to confirm the selected instance was terminated. That process looks something like this:

    $ ./
    i-0c34e5ec790618146 {u'Code': 16, u'Name': 'running'}
    $ ./ i-0c34e5ec790618146
    {u'TerminatingInstances': [{u'InstanceId': 'i-0c34e5ec790618146', u'CurrentState': {u'Code': 32, u'Name': 'shutting-down'}, u'PreviousState': {u'Code': 16, u'Name': 'running'}}], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '55c3eb37-a8a7-4e83-945d-5c23358ac4e6', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Sun, 01 Jan 2017 00:07:20 GMT'}}}
    $ ./
    i-0c34e5ec790618146 {u'Code': 48, u'Name': 'terminated'}


    To add a key to the `ec2.create_instances()` step, just add the following to the function parameters:


    Where MyEC2Key is the name of a key that you have already created.

  • Source: Automating AWS With Python and Boto3
  • After importing the Boto3 module we need to connect to the EC2 region that the instances are to be created on. This is achieved through the below snippet.
  • ec2 = boto3.resource('ec2', region_name="ap-southeast-2")
  • A list of regions with codes can be found here
  • The first section of code creates a KeyPair to be assigned to the created instances. If using an existing KeyPair, this can be commented out. The request to create a KeyPair returns key_material which is the private key, this is placed as a string into KeyPairOut and saved to a file.
  • outfile = open('TestKey.pem','w')
    key_pair = ec2.create_key_pair(KeyName='TestKey')
    KeyPairOut = str(key_pair.key_material)
  • The next major code section creates the instances.
  • instances = ec2.create_instances(


Leave a Reply