Packer with AWS

[root@node1 ~]# wget https://releases.hashicorp.com/packer/1.3.0/packer_1.3.0_linux_amd64.zip

[root@node1 ~]# unzip packer_1.3.0_linux_amd64.zip
Archive: packer_1.3.0_linux_amd64.zip
inflating: packer
[root@node1 ~]# mv packer /usr/bin/

[root@node1 ~]# bash

[root@node1 ~]# packer version
Packer v1.3.0

Your version of Packer is out of date! The latest version
is 1.3.1. You can update by downloading from http://www.packer.io/downloads.html

[root@node1 ~]# packer
Usage: packer [–version] [–help] <command> [<args>]

Available commands are:
build build image(s) from template
fix fixes templates from old versions of packer
inspect see components of a template
validate check that a template is valid
version Prints the Packer version

[root@node1 ~]# mkdir packer-customimage

[root@node1 ~]# cd packer-customimage/

I don’t have any custom image in my bucket in my aws region

packer1

[root@node1 packer-customimage]# vim custom-image.json
{
“variables”: {
“aws_access_key”: “XXXXXXXXXXXXXXXXXXX”,
“aws_secret_key”: “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
},
“builders”: [{
“type”: “amazon-ebs”,
“access_key”: “{{user `aws_access_key`}}”,
“secret_key”: “{{user `aws_secret_key`}}”,
“region”: “us-west-1”,
“source_ami”: “ami-04ab2c6566a35dfe1”,
“instance_type”: “t2.medium”,
“ssh_username”: “ec2-user”,
“ami_name”: “packer-demo {{timestamp}}”
}],

“provisioners”: [
{
“type”: “shell”,
“script”: “my-script.sh”
}
]
}
[root@node1 packer-customimage]# touch my-script.sh

[root@node1 packer-customimage]# vim my-script.sh
#!/bin/bash
sudo yum -y update
sudo yum install -y httpd
[root@node1 packer-customimage]# packer validate custom-image.json
Template validated successfully.
[root@node1 packer-customimage]# packer inspect custom-image.json
Optional variables and their defaults:

aws_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXX
aws_secret_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Builders:

amazon-ebs

Provisioners:

shell

Note: If your build names contain user variables or template
functions such as ‘timestamp’, these are processed at build time,
and therefore only show in their raw form here.
[root@node1 packer-customimage]# packer build custom-image.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name: packer-demo 1540528594
==> amazon-ebs: Error querying AMI: InvalidAMIID.NotFound: The image id ‘[ami-04ab2c6566a35dfe1]’ does not exist
==> amazon-ebs: status code: 400, request id: c3c05ecb-ab30-4ad1-9487-d86b4a8df022
Build ‘amazon-ebs’ errored: Error querying AMI: InvalidAMIID.NotFound: The image id ‘[ami-04ab2c6566a35dfe1]’ does not exist
status code: 400, request id: c3c05ecb-ab30-4ad1-9487-d86b4a8df022

==> Some builds didn’t complete successfully and had errors:
–> amazon-ebs: Error querying AMI: InvalidAMIID.NotFound: The image id ‘[ami-04ab2c6566a35dfe1]’ does not exist
status code: 400, request id: c3c05ecb-ab30-4ad1-9487-d86b4a8df022

==> Builds finished but no artifacts were created.

when ever packer build id failed changes till time will be reverted. That the reason my image did not get created. 

packer2.PNG

I just changed the region from us-west-1 to us-east-1 in json file (custom-image.json)

[root@node1 packer-customimage]# packer build custom-image.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name: packer-demo 1540528896
amazon-ebs: Found Image ID: ami-04ab2c6566a35dfe1
==> amazon-ebs: Creating temporary keypair: packer_5bd29b00-ae0b-6321-9627-d61486028b43
==> amazon-ebs: Creating temporary security group for this instance: packer_5bd29b06-6223-c9bc-9f15-f1f26bd4f279
==> amazon-ebs: Authorizing access to port 22 from 0.0.0.0/0 in the temporary security group…
==> amazon-ebs: Launching a source AWS instance…
==> amazon-ebs: Adding tags to source instance
amazon-ebs: Adding tag: “Name”: “Packer Builder”
amazon-ebs: Instance ID: i-0cc10dac65cc515b9
==> amazon-ebs: Waiting for instance (i-0cc10dac65cc515b9) to become ready…
==> amazon-ebs: Using ssh communicator to connect: 54.165.137.74
==> amazon-ebs: Waiting for SSH to become available…
==> amazon-ebs: Error waiting for SSH: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
==> amazon-ebs: Terminating the source AWS instance…
==> amazon-ebs: Cleaning up any extra volumes…
==> amazon-ebs: Destroying volume (vol-067d6e434965238b4)…
==> amazon-ebs: Deleting temporary security group…
==> amazon-ebs: Deleting temporary keypair…
Build ‘amazon-ebs’ errored: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

==> Some builds didn’t complete successfully and had errors:
–> amazon-ebs: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

==> Builds finished but no artifacts were created.
I just changed the ec2-user to centos and it worked for me.

user name depends Ubuntu may have user as Ubuntu, aws image has ec2-user, like wise centos has centos, since i choosed centos as my base image.
[root@node1 packer-customimage]# packer build custom-image.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name: packer-demo 1540529813
amazon-ebs: Found Image ID: ami-04ab2c6566a35dfe1
==> amazon-ebs: Creating temporary keypair: packer_5bd29e95-ffef-0aed-8fe3-7ffae7dbec12
==> amazon-ebs: Creating temporary security group for this instance: packer_5bd29e99-9eff-a4f4-cea6-bf279719cd78
==> amazon-ebs: Authorizing access to port 22 from 0.0.0.0/0 in the temporary security group…
==> amazon-ebs: Launching a source AWS instance…
==> amazon-ebs: Adding tags to source instance
amazon-ebs: Adding tag: “Name”: “Packer Builder”
amazon-ebs: Instance ID: i-09620537e1776902b
==> amazon-ebs: Waiting for instance (i-09620537e1776902b) to become ready…
==> amazon-ebs: Using ssh communicator to connect: 54.89.59.33
==> amazon-ebs: Waiting for SSH to become available…
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with shell script: my-script.sh
amazon-ebs: Loaded plugins: fastestmirror
amazon-ebs: Determining fastest mirrors
amazon-ebs: * base: repo1.ash.innoscale.net
amazon-ebs: * epel: epel.mirror.constant.com
amazon-ebs: * extras: repo1.ash.innoscale.net
amazon-ebs: * updates: repo1.ash.innoscale.net
amazon-ebs: Resolving Dependencies
amazon-ebs: –> Running transaction check
amazon-ebs: —> Package centos-release.x86_64 0:7-5.1804.4.el7.centos will be updated
amazon-ebs: —> Package centos-release.x86_64 0:7-5.1804.5.el7.centos will be an update

.
.
.
.
amazon-ebs: Complete!
==> amazon-ebs: Stopping the source instance…
amazon-ebs: Stopping instance, attempt 1
==> amazon-ebs: Waiting for the instance to stop…
==> amazon-ebs: Creating unencrypted AMI packer-demo 1540529813 from instance i-09620537e1776902b
amazon-ebs: AMI: ami-084a06c4a6cd6b97f
==> amazon-ebs: Waiting for AMI to become ready…
==> amazon-ebs: Terminating the source AWS instance…
==> amazon-ebs: Cleaning up any extra volumes…
==> amazon-ebs: Destroying volume (vol-0829d16bb0f49c1fe)…
==> amazon-ebs: Deleting temporary security group…
==> amazon-ebs: Deleting temporary keypair…
Build ‘amazon-ebs’ finished.

==> Builds finished. The artifacts of successful builds are:
–> amazon-ebs: AMIs were created:
us-east-1: ami-084a06c4a6cd6b97f

packer3.PNG

you can check a brand new custom image is available as per our customization script, or playbook.

packer4.PNG

 

Leave a comment