Creating an AMI Image as a backup with Python

We all know the importance of having current backups. Let’s take a look at programatically selecting a server based on name tag (in my case I decided to backup the private git server we setup previously).

We can also utilize a similar setup to create load balancing servers for our web apps, or we can use this similar to docker.

Let’s make do our imports. I decided to use boto2 for ease of sorting instance tags.

#!/usr/bin/python
# -*- coding: utf-8 -*-
#import our dependencies
import boto
from boto import ec2
from boto.ec2 import connection, connect_to_region
import sys
import os
import uuid

class AMICreation(object):

def __init__(self):
#this section is for windows users storing their keys in environment variables
#os.environ[‘AWS_ACCESS_KEY_ID’]
#os.environ[‘AWS_SECRET_ACCESS_KEY’]
#os.environ[‘AWS_DEFAULT_REGION’] = ‘us-west-2’

self.connection = connect_to_region(‘us-west-2’)
#now we iterate through the open instances
self.instances = [i for r in
self.connection.get_all_instances() for i in
r.instances]
self.action = ‘failure’
self.has_error = ‘no’
self.instance = None
#create a function to create image id
def create_image_id(
self,
instance=None,
description=None,
no_reboot=None,
ami_name=None,
):
#name name our image
image_id = instance.create_image(ami_name,
description=description, no_reboot=no_reboot)

if ‘ami’ in image_id:
print image_id

return ‘success’
else:

return ‘failure’

def find_instance_id_and_create(
self,
servername,
descritption,
no_reboot,
):
#iterate through our instances and find the name tag matching “gitserver”
for i in self.instances:

if ‘Name’ in i.tags:

state = i.state

name = i.tags[‘Name’]

instance_id = str(i.id)

print name, state, instance_id

if name.lower() == servername.lower():

ami_name = servername.lower() + ‘-‘ \
+ str(uuid.uuid4().fields[-1])[:5]

status = self.create_image_id(i, str(descritption),
str(no_reboot), str(ami_name))

if status == ‘success’:

self.has_error = ‘no’

return self
else:

self.has_error = ‘yes’

return self
else:

self.has_error = ‘no instances named %s’ \
% servername.lower()

AMICreator = AMICreation()

AMICreator.find_instance_id_and_create(‘gitserver’,
‘this is a git server backup base image’, ‘False’)

if str(AMICreator.has_error) == ‘no’:
print ‘success’
else:
print AMICreator.has_error

Now we have created a backup image of our EC2 instance. Now we can return to a running state at this point in time easily if we need to.

Using Python to Automate Jenkins Install on AWS EC2 Instance

One of the main goals for a DevOps professional is automation. This week I was given a “simple” task, I was supposed to write a script that would login to AWS, create an instance, and install Jenkins.

Why would I want to do all that work when there are GUIs to assist with this process?

Automation is the key, when you may be faced with repetitive tasks automation just makes sense.

For the purpose of this tutorial is is assumed that you already have an AWS account, SecretKey, SecretAccessID, a security group policy already set up to accept incoming on port 8080, Python 2.7, and Boto3 installed.


import os

import boto3


First we need to make our calls. Creating an instance requires the os module and Boto modules, I decided to utilize boto3.


# note, later I made these system environment calls so they
# aren't accidentally published in a public repository.
os.environ["AWS_ACCESS_KEY_ID"]
os.environ["AWS_SECRET_ACCESS_KEY"]
os.environ["AWS_DEFAULT_REGION"] = "us-west-2"

This sets the access key, secret key and default region. You can change the region to wherever you need.


userdata = """#!/bin/bash
yum update -y
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import http://pkg.jenkins-ci.org/redhat-stable/jenkins-ci.org.key
yum install jenkins -y
service jenkins start
chkconfig jenkins on

"""

Now we pass our #!bash script through userdata. We start by updating yum. The next two lines are adding the Jenkins repository. Next we install Jenkins. Finally we start Jenkins as a service.
Important note: any commands sent that will require user input need to include the input. Ex: yum requires -y


ec2 = boto3.resource('ec2')
instances = ec2.create_instances(
 ImageId='ami-7172b611',
 InstanceType='t2.micro',
 KeyName='AWS_Testing',
 MinCount=1,
 MaxCount=1,
 SecurityGroupIds=['Jenkins'],
 UserData=userdata
)

Now we need to define our instance to be created. You may use a different AMI, KeyName, SecurityGroup.


for instance in instances:
 print("Waiting until running...")
 instance.wait_until_running()
 instance.reload()
 print((instance.id, instance.state, instance.public_dns_name,
instance.public_ip_address))

Now we put everything together and return information about our instance.

Put everything together and test it out. Don’t forget to SSH into the instance and get the Jenkins default password.

The next task given to me is to set up an ELK Stack… Stay tuned.