Example of how AWS uses packer to build an aws AMI Image

Source: Internet
Author: User
Tags documentation ssh git clone

Aws provides a variety of images for users, but in most cases, the ami provided in the community cannot fully meet our needs, for example, the root partition of some images is only 10 GB, so we usually use the public image of the community to create an ec2 instance, and then process it (configure, then, create an image for the project to use. packer allows us to generate an AMI more automatically. We can write the original image and all the configuration items into a template file and hand it over to the pakcer, packer will connect to our aws account through the access key, complete the above steps to manually create an image, generate a new AMI, this method allows us to manage our aws images in the form of code. Combined with git and other version control systems, we can achieve version management of aws images, lay a solid foundation for the goal of Infrastructure as Code.
That is to say, packer provides image creation for various cloud platforms in the form of code, including aws, openstack, DigitalOcean, docker, etc., from HashiCorp and vagrant, this company has developed a lot of DevOps tools and follows the linux philosophy. Every tool can be used independently, you can also use their services to create a tool stream to simplify O & M or development.
The following describes how to use packer to build an ec2 image.


Wget https://releases.hashicorp.com/packer/0.10.0/packer_0.10.0_linux_amd64.zip
Decompress the packer binary file and put it in the $ PATH variable directory.


Then we need a json template to describe how to build an ec2 image. The general form of the template is as follows. For details, see the official documentation:

"Builders": [], # define an original image
"Descriptions": [], # Description
"Min_packer_version": [],
"Post-processors": [], # defines the preprocessing process
"Provisioners": [], # define the image building behavior
"Variables": [] # define some variables
The following example shows a packer template that starts from an original image, first uploads the git key to the root directory, then clones a version library, and finally executes the initialization script, the aws access key can be specified or not in the template ~ /. Find it under the aws directory.

"Builders ":[
"Name": "first_build ",
"Type": "amazon-ebs ",
"Region": "us-east-1 ",
"Source_ami": "ami-ds1f15b6 ",
"Instance_type": "t2.micro ",
"Ssh_username": "root ",
"Ami_name": "MY_AMI_30G_CENTOS6 _ {timestamp }}"

"Provisioners ":[
"Type": "file ",
"Source": "git_id_rsa ",
"Destination": "/root/. ssh/id_rsa"
"Type": "shell ",
"Inline": ["chmod 400/root/. ssh/id_rsa ",
"Chown root: root/. ssh/id_rsa ",
"Cd/root/& git clone git@git.sixther.me: ops/ops_deploy.git -- depth 1 ",
"Cd/root/ops_deploy/centos6 & sh init_full_aws_us.sh ",
The preceding template uses two types of provisioners: file root shell. file can upload files to an image, shell can execute scripts, and ansible, chef, puppet and other configuration tools define the action to build the image. For details, see the provisioners documentation.

Finally, we will explain how to create an ami with a root partition of 10 GB from a root partition of 30 GB.

& Loz; start an instance based on the 10g ami;

& Loz; create a 30 GB ebs and attach it to this instance;

& Loz; use the dd command to clone data from the original disk to the new disk (this step mainly involves cloning MBR; otherwise, the disk cannot start linux );

& Loz; then, repartition the new disk, divide it into 30 GB, and set the boot flag;

& Loz; Mount the new partition to a temporary directory, and then copy the entire root partition of the old disk to the new partition;

& Loz; close the instance, dettach two disks, and attach the new disk to the instance with the/dev/sda1 label. If the instance can be started properly, create the disk as an ami.

The commands involved in the preceding steps include:

Dd if =/dev/xvda of =/dev/xvdf bs = 65536 # Create a 30 GB ebs Mount file and copy the data on the original disk.
Dd if =/dev/xvdf of = mbr. bin bs = 512 count = 1 # mbr everywhere for check
Hexdump-C mbr. bin # View mbr information for checking
Fdisk/dev/xvdf # Create a 30 GB primary partition and set it as the boot partition. Command a can set the boot partition.
Mkfs. ext3/dev/xvdf1
Rsync-UVR/-exclude = '/new/*'/new/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.