Automating AWS ec2 With Python and Boto3

Views: 3779

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:

  • https://stackabuse.com/automating-aws-ec2-management-with-python-and-boto3/
  • https://stackoverflow.com/questions/46980189/boto3-create-image-for-ami-creation-save-only-the-root-volume

    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.id, instance.state

    Save the lines above into a file named list_instances.py 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 list_instances.py
    $ chmod +x list_instances.py
    $ ./list_instances.py

    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.

    image5.png

    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(
        ImageId='ami-1e299d7e',
        MinCount=1,
        MaxCount=1,
        InstanceType='t2.micro')
    print instance[0].id

    While the command will finish quickly, it will take some time for the instance to be created. Run the list_instances.py 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 list_instances.py script to see what instances are available. Note one of the instance IDs to use as input to the terminate_instances.py 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:

    $ ./list_instances.py
    i-0c34e5ec790618146 {u'Code': 16, u'Name': 'running'}
    
    $ ./terminate_instances.py 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'}}}
    
    
    $ ./list_instances.py
    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:

    KeyName=”MyEC2Key”

    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)
    outfile.write(KeyPairOut)
  • The next major code section creates the instances.
  • instances = ec2.create_instances(
        ImageId='ami-e0c19f83', 
        MinCount=1, 
        MaxCount=5,
        KeyName="TestKey",
        InstanceType="t2.micro"
    )

    https://sdbrett.com/BrettsITBlog/2016/05/creating-aws-instances-with-boto3/

     

Leave a Reply