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.
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. It takes the snapshot of the instance which is running in Region-1 for the given VolumeId
- import boto3
- import datetime
- import time
- def lambda_handler(event, context):
- ec2 = boto3.resource('ec2')
- ec21 = boto3.resource('ec2', region_name='eu-west-2')
- ec22 = boto3.resource('ec2', region_name='eu-west-1')
- snapshot = ec2.create_snapshot(VolumeId='vol-1234')
- time.sleep(30)
- print "The snapshot Id of the Instance ", snapshot.id
- ##2. Copy the snapshot from Region-1 to Region-2.
- client = boto3.client('ec2', region_name='eu-west-1')
- snap1 = client.copy_snapshot(SourceSnapshotId=snapshot.id,
- SourceRegion='eu-west-2',
- DestinationRegion='eu-west-1')
- time.sleep(30)
- print "The New snapshot id is : ", snap1['SnapshotId']
- ##3. Register the image from that snapshot in Region-2.
- ec2 = boto3.resource('ec2',region_name='eu-west-1')
- BlockDeviceMapping = [
- {
- 'DeviceName': '/dev/sda',
- 'Ebs': {
- 'SnapshotId':snap1['SnapshotId']
- }
- }
- ]
- New_image=ec2.register_image(Name="Test02",Description="test",
- RootDeviceName='/dev/sda',BlockDeviceMappings=BlockdeviceMapping)
- print "The Newly created image id is : ", New_image.id
- ##4. Launch a New instance in Region-2.
- instance = ec2.create_instances(ImageId=New_image.id,
- MinCount=1,
- MaxCount=1,
- InstanceType='t2.micro')
- 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 :