Skip to content

Instantly share code, notes, and snippets.

@SwampDragons
Last active June 14, 2019 21:53
Show Gist options
  • Save SwampDragons/da14e15643c0affb277a568a29d37784 to your computer and use it in GitHub Desktop.
Save SwampDragons/da14e15643c0affb277a568a29d37784 to your computer and use it in GitHub Desktop.

Basic Packer Demo

Setting up:

To run this demo:

  1. Install Packer. (see our installation guide for details, if you need them).

  2. Add valid AWS credentials to your environment: export AWS_ACCESS_KEY_ID=<YOUR KEY HERE> and export AWS_SECRET_ACCESS_KEY=<YOUR KEY HERE>.

Once you've exported those credentials, save the above file and run:

packer build packer_demo.json

What does this Demo do?

This demo grabs a basic ubuntu image from the AWS marketplace, provided by Cannonical (the makers of ubuntu) It uses the ubuntu package manager (apt) to install a silly program called cowsay.

Finally, it uses cowsay to print "packer!!" in a speech bubble from a cow:

    amazon-ebs:  __________
    amazon-ebs: < packer!! >
    amazon-ebs:  ----------
    amazon-ebs:         \   ^__^
    amazon-ebs:          \  (oo)\_______
    amazon-ebs:             (__)\       )\/\
    amazon-ebs:                 ||----w |
    amazon-ebs:                 ||     ||

It saves the image, which now has cowsay installed, as a new AMI, and deletes the instance it launched to create that image. If you navigate to your AWS EC2 console, you will be able to see the AMI, whose name will be "packer-cowsay-example-" followed by the epoch time.

{
"variables": {
"region": "us-east-1"
},
"builders": [
{
"type": "amazon-ebs",
"ami_name": "packer-cowsay-example-{{ timestamp }}",
"region": "{{ user `region`}}",
"instance_type": "t2.micro",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": ["099720109477"],
"most_recent": true
},
"ssh_username": "ubuntu",
"communicator": "ssh"
}
],
"provisioners": [
{
"type": "shell",
"inline": ["sudo apt-get update",
"sudo apt-get install -y cowsay",
"cowsay packer!!"]
}
],
"post-processors": [
{
"type": "manifest",
"output": "manifest.json"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment