Automate AWS AMI Creation For EC2 And Copy to Other Region | Disaster Recovery

Automate AWS AMI Creation For EC2 And Copy to Other Region | Disaster Recovery

In this blog we are going to create python script to create AMI image of running instances in specific region and copy those images to other region using boto3.

Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.

Table Of Content

  • Prerequisite.
  • Launch EC2 Instances In US-EAST-1.
  • Create AMI Images For A Specific Region.
  • Waiting For Images Using Paginators.
  • Copy Images To Other Regions.
  • Resource Cleanup.

Prerequisite

An AWS Account An IAM User with:

  • AWS Management Console access to verify your EC2 instances launched,listed and terminated.
  • The IAM permissions required to perform IAM, EC2, and CloudWatch activities. IAM policy creation and AWS Application Programming Interface (API) permissions are outside this article’s scope. Always adhere to the principle of least privilege when authorizing accounts to perform actions. Administrative access to an EC2 Instance.
  • Install awscli using aws official documentation here
  • Install python and boto3
  • Configure aws cli by using official documentation here

Launch AWS EC2 Instance using python script

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the client for EC2
    client = boto3.client('ec2')
    
  3. To launch EC2 instances we have to use method "run_instances()". This method helps us launch AWS EC2 instances based on our requirement.
    response =client.run_instances(<arguments>)
    
  4. Goto link where you will find all arguments list. Based on your requirement you can put this arguments to launch your EC2 instances. This document also mentions datatype of the parameter.
    Note:- Arguments which are with "REQUIRED" tags mentioned in documentation is mandatory, if you don't specify those arguments code block to launch EC2 will not execute successfully.
    Example:- "MinCount", "MaxCount".
    Below code will launch EC2 instance based on your provided input.
    resp=client.run_instances(ImageId='ami-0742b4e673072066f',
                           InstanceType='t2.micro',
                           MinCount=2,
                           MaxCount=2,
                           KeyName='<Your key name>')
    
  5. Once above method will run it will launch EC2 and launched EC2 information will be captured in variable "resp". It will return infomation in dictonary, so "resp" would be a dictonary.
  6. Now we will traverse the dict using for loop to print list of instances launched by "run_instances" method.
for i in resp['Instances']:
    print("Instance ID Created is :{} Instance Type Created is : {}" .format(i['InstanceId'],i['InstanceType']))

To view entire github code please click here

Create AMI Images For A Specific Region

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the resource for EC2 and pass region name as argument. Create a empty list.
    ec2 = boto3.resource('ec2', region_name='us-east-1')
    image_ids = []
    
  3. Now we will use "instances.filter()" to filter out instance ids which are in running state. Checkout the official documentation here
    instances = ec2.instances.filter(
     Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
    
  4. Now we will traverse instances list using for loop to create AMI by using "create.image()" for all instance ids which are in running state in us-east-1. Check official documentation here
    for instance in instances:
     print(instance.id,instance.placement)
     image = instance.create_image(Name='AMI Copy For '+instance.id)
     print(image)
     image_ids.append(image.id)
    

Waiting For Images Using Paginators

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the client for EC2 and pass region name as argument. Create a empty list.
    ec2_client = boto3.resource('ec2', region_name='us-east-1')
    
  3. Now we will use class "EC2.Waiter.ImageAvailable" to check if the AMI created is in available state.
    waiter = ec2_client.get_waiter('image_available')
    
  4. Now we will use wait method under class "". Wait() polls EC2.Client.describe_images() every 15 seconds until a successful state is reached. An error is returned after 40 failed checks. Checkout official documentation here
    waiter.wait(Filters=[{
     'Name': 'image-id',
     'Values': image_ids
    }])
    

Copy Images To Other Regions

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the client for EC2 and pass region name as argument(us-west-1).
    ec2_client = boto3.client('ec2', region_name='us-west-1')
    
  3. Now we will traverse the list image_ids by using for loop and use "copy_image()" to copy AMI from us-east-1 to us-west-1. Checkout official documentation here
for image_id in image_ids:
    ec2_client.copy_image(Name='AMI Copy From US-EAST-1'+image_id, SourceImageId=image_id, SourceRegion='us-east-1')

To view entire code please visit github repo here

Resource Cleanup

  1. Deregister AMI copy from "us-west-1".
  2. Deregister AMI copy from "us-east-1".
  3. Delete EC2 instances created.

Conclusion

By using Boto3 provided inbuild methods for AWS resources many task can be automated by writing a python script. Current code snippet can be used to automate AMI copy to other regions as part of Disaster Recovery plan.

Stay tuned for my next blog.....

So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!