DevOps Classroom Series – 03/Jan/2020 – Packer

Creating AWS Image From Packer

  • To create AWS Image from Packer we need some info
    • AWS Account:
      • Create Secret Key and Access Key for Packer (IAM User)
    • Source Image AMI
    • Provisioning Script
    • Region
  • Create a folder and a json file with the following content
{
    "builders": []
}
  • Lets create access key and secret key as mentioned over here
  • Fill all the required Fields from amazon-ebs builder as shown below
{
    "builders": [
        {
            "type": "amazon-ebs",
            "ami_name": "tomcatfrompacker",
            "ami_description": "tomcat from packer",
            "access_key": "",
            "secret_key": "",
            "region": "us-west-2",
            "instance_type": "t2.micro",
            "source_ami": "ami-06d51e91cea0dac8d"

    }]
}
  • Now execute the command packer validate aws.json and you should observe an error about ssh_username add ssh_username to the builder section
{
    "builders": [
        {
            "type": "amazon-ebs",
            "ami_name": "tomcatfrompacker",
            "ami_description": "tomcat from packer",
            "access_key": "",
            "secret_key": "",
            "region": "us-west-2",
            "instance_type": "t2.micro",
            "source_ami": "ami-06d51e91cea0dac8d",
            "ssh_username": "ubuntu"

    }]
}
  • Now lets build the image using debug build option
packer build -debug aws.json
  • Now manually deregister ami after image creation is success.
  • Now lets add provisioner. I have set of manual linux commands to be executed and i will be using a shell provisioner Refer Here
  • Lets add provisioners section
{
    "builders": [
        {
            "type": "amazon-ebs",
            "ami_name": "tomcatfrompacker",
            "ami_description": "tomcat from packer",
            "access_key": "AKIA3TXJQGAJFDVFHD7C",
            "secret_key": "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",
            "region": "us-west-2",
            "instance_type": "t2.micro",
            "source_ami": "ami-06d51e91cea0dac8d",
            "ssh_username": "ubuntu"

    }],
    "provisioners": [
        {
            "type": "shell",
            "inline": [
                "sudo apt-get update", 
                "sudo apt-get install openjdk-8-jdk -y",
                "sudo apt-get install tomcat8 -y"
            ]
        }
    ]
}
  • Try building image using packer build

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner