Create a custom AMI with default 6GIB minimum EBS volume size

Hey everyone, Today I would like to share another interesting post related to AWS AMI. Those who don't know about Amazon AMI you visit this Amazon Machine Images (AMI). In this post, we will see how to create a custom AMI (our own personal AMI) with a minimum EBS volume size of 6 GIB. To create a custom AMI we first need to create an EC2 instance. The specifications of our instances are as follows:

  1. OS type Amazon Linux 2
  2. Volume size 8GIB (Minimum size that amazon allows)
  3. Tag with Key: Name and value: MyInstance
So after creating our ec2 instance, we need to follow the following instructions:
  1. First, Shut down the instance (MyInstance) to prevent any problem.
  2. Create a new 6GIB EBS volume.
    1. Go to Elastic Block Store Volumes
    2. Click Create Volume
    3. Choose the same volume type same with your old volume
    4. Enter your desired size; In our case 6Gib
    5. Choose availability zone; The volume will be available to an instance in the same availability zone.
    6. Add a tag with Key: Name and value: NewVolume6Gib
  3. Attach the new volume (myVolume)
    1. Right-click on the new volume (myVolume).
    2. Click Attach Volume.
    3. Choose instance, select MyInstance.
    4. Click Attach.
  4. Format the volume (optional)
    1.  Just to check whether the volume has any data by using command sudo file -s /dev/xvdf.
    2. If it is displaying /dev/xvdf: data, it means the volume is empty. We could format the volume.
    3. If it is displaying any other thing except the above output, it means the volume have data. 
    4. If you want to format the volume you can use the command sudo mkfs -t ext4 /dev/xvdf.
  5. Mount the new volume (myVolume)
    1. Create a directory to mount using the command sudo mkdir /mnt/myVolume
    2. Mount new volume (myVolume)  into a directory using command sudo mount /dev/xvdf /mnt/myVolume 
    3. Check volume by using command df -h The new volume (myVolume) should be mounted now.
  6. Copy data from the old volume to the new volume (myVolume)
    1. Use rysnc to copy from old volume to the new volume (myVolumesudo rsync -axv / /mnt/myVolume/.
    2. Wait until it’s finished. ✋
  7. Install GRUB 
    1. Install grub on myVolume using command sudo grub2-install --root-directory=/mnt/myVolume/ --force /dev/xvdf.
    2. Unmount myVolume,  sudo umount /mnt/myVolume
    3. Check UUID using command blkid
    4. Copy UUID of /dev/xvda1
    5. Use tune2fs to replace UUID sudo tune2fs -U COPIED_UUID /dev/xvdf
    6. Check label by using the following command sudo lsblk -o name, mountpoint, label, size, uuid
    7. Replace myVolume label with old-volume label using command sudo e2label /dev/xvdf cloudimg-rootfs
    8. Log out from the instance and shut it down.
  8. Detach old volume and attach the new volume (myVolume)
    1. Detach old volume
    2. Detach new volume (myVolume)
    3. Attach new volume (myVolume) to /dev/xvda
    4. Start  MyInstance instance, you see an instance is now running with 6GIB EBS volume size.
  9. Create a custom AMI
    1. To create an AMI from an instance, Right-click the instance you want to use as the basis for your AMI, and choose to Create Image from the context menu. 
    2. In the Create Image dialogue box, type a unique name and description, and then choose Create Image. 

    3. It may take a few minutes for the AMI to be created. After it is created, it will appear in the AMIs view in AWS Explorer.

Hope you enjoyed my blog post.😎
Let me know in the comments below if you face any problem.


Popular posts from this blog

How to pass parameters in webhook?

Access and modify all the resources of our Wiki.js using WikiJS API

Fahrenheit to Celsius