Saturday 9 September 2017

AWS Snapshot process automation.


AWS Automation : 

   DR unit for the instance through Snapshots

         The need for automation is clear. As organizations grow so do the complexities of their business processes. Automating these processes allows for better customer service, increased productivity, and the ability to capitalize on new revenue opportunities while lowering total cost of ownership.
These processes can be high level in nature or tactical and targeted. They can span across the organization or focus on functional areas such as the IT department. Regardless of the scope of your core processes the answer is still the same - automation makes business processes faster; more manageable, secure, and efficient; and less prone to error and delay.


EBS snapshots play an important role when it comes to backup your instance data.It gives you point in time backup and backup resilience to your data. In this tutorial, I will guide you to automate EBS snapshot creation, copying to other region and setup a new instance from that using AWS lambda function.Automation being the Key, this is one try towards that. This script makes a automation in setting up a DR unit for the instance. I mean, this script will do :

      1.  It Takes the snapshot of the instance which is running in Region-1.
      2.  Copy that snapshot to required other region. Lets say Region-2.
      3.  Register the image from that snapshot in Region-2.
      4.  Launch a New instance in Region-2.
                       

We can back up the data on your Amazon EBS volumes  by taking point-in-time snapshots. Snapshots are incremental backups, which means that only the blocks on the device that have changed after your most recent snapshot are saved. This minimizes the time required to create the snapshot and saves on storage costs by not duplicating data. 

We will use python 2.7 scripts, lambda, IAM role and cloudwatch event schedule for this setup.

  1. ##1. It takes the snapshot of the instance which is running in Region-1 for the given VolumeId
  2.  import boto3
  3.  import datetime
  4.  import time
  5.  def lambda_handler(event, context):
  6.     ec2 = boto3.resource('ec2')
  7.     ec21 = boto3.resource('ec2', region_name='eu-west-2')
  8.     ec22 = boto3.resource('ec2', region_name='eu-west-1')
  9.     snapshot = ec2.create_snapshot(VolumeId='vol-1234')
  10.     time.sleep(30)
  11.     print "The snapshot Id of the Instance ", snapshot.id
  12.        
  13.      
  14. ##2. Copy the snapshot from Region-1 to  Region-2.
  15.      client = boto3.client('ec2', region_name='eu-west-1')
  16.     snap1 = client.copy_snapshot(SourceSnapshotId=snapshot.id,
  17.                                  SourceRegion='eu-west-2',
  18.                                  DestinationRegion='eu-west-1')
  19.     time.sleep(30)
  20.     print "The New snapshot id is : ", snap1['SnapshotId']
  21.                    
  22. ##3. Register the image from that snapshot in Region-2.
  23.        
  24.      ec2 = boto3.resource('ec2',region_name='eu-west-1')
  25.     BlockDeviceMapping = [
  26.        {
  27.           'DeviceName''/dev/sda',
  28.            'Ebs'{
  29.                     'SnapshotId':snap1['SnapshotId']
  30.                    }
  31.               }
  32.        ]                                                           
  33.    New_image=ec2.register_image(Name="Test02",Description="test",
  34.    RootDeviceName='/dev/sda',BlockDeviceMappings=BlockdeviceMapping)
  35.    print "The Newly created image id is : ", New_image.id
  36.    
  37. ##4. Launch a New instance in Region-2.
  38.            
  39.     instance = ec2.create_instances(ImageId=New_image.id,
  40.                                    MinCount=1,
  41.                                    MaxCount=1,
  42.                                    InstanceType='t2.micro')
  43.    print "The id of launched instance is : ", instance[0].id


In the code above written, we have taken the VolumeId manually and snapshot is copied from eu-west-2(London) region to eu-west-1(Ireland) region.

Thank you for reading the post. Comments and Suggestions are appreciated.     


About the Person : 



Vinay Raj H S lives in Bangalore, works in cloud and Devops Technology. Likes to Play Badminton, readup on new technologies and try his hand at cooking.



AWS Snapshot process automation.

AWS Automation :     DR unit for the instance through Snapshots           The need for automation is clear.  As organizations grow ...