Contents

Packer Sample Script

Sample script to create an image with Packer

Website Visitors:

In this tutorial, we will explain about a sample script to create an instance, deploy software and create an image from it. Before you run the script, make sure to install aws cli and add your AWS access key and secret key in ~/.aws/credentials file or checkout the Packer Variables tutorial to know different ways to use your AWS credentials with Packer.

Simple Packer script to create an instance

1
2
3
4
5
6
7
8
9
{
    "builders": [{
        "type":"amazon-ebs",
        "source_ami": "ami-12345",
        "instance_type": "t2.micro",
        "ssh_username": "ec2-user",
        "ami_name": "packer {{timestamp}}"
    }]
}

For the above script, you should search for the latest source ami and update the value every time. With Packer you can automate this step as well. You don’t have to search for the latest image every time. Instead Packer will search for the latest AMI and use it to deploy an image.

Install nginx with Packer

Below script creates instance from AWS, installs nginx, creates an image from it, saves it to your AWS account and terminates that instance immediately. Here websitefiles folder is the source folder containing all your html files. They are uploaded to the Downloads folder on the instance, and then copied to /var/www/html folder.

Note
You should copy the files to your instance first and then install nginx. Next, you can copy files which are already there on the instance to /var/www/html folder. if not, you will end up in permission denied error.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
    "builders": [{
        "type":"amazon-ebs",
        "source_ami_filter":{
            "filters": {
                "virtualization-type": "hvm",
                "root-device-type": "ebs",
                "name": "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20220912"
            },
            "owners": ["099720109477"],
            "most_recent": "true"
        },
        "instance_type": "t2.micro",
        "ssh_username": "ubuntu",
        "ami_name": "packer {{timestamp}}"
    }],
    "provisioners": [
        {
            "type": "file",
            "source": "WebsiteFiles",
            "destination": "~/Downloads"
        },
        {
            "type": "shell",
            "inline": [
                "sudo apt-get update",
                 "sudo apt-get install -y nginx",
                 "sudo mv ~/Downloads/* /var/www/html/",
                 "sudo service nginx restart",
                 "sudo ufw allow 'Nginx HTTP'",
                 "sudo systemctl enable nginx"
           ]
        }
    ]
}

You can now deploy an instance using the new AMI that you’ve just created.

Tip
In order to deploy an instance quickly through command line, install aws cli and run this command: aws ec2 run-instances --instance-type t2.micro --count 1 --key-name KeyPairPem --image-id IMAGEID

Ansible with Packer

You can also add ansible and other configuration management tools for installing softwares in the place of shell given above. To use ansible in your Packer configuration, add provisioner section as shown below:

1
2
3
4
5
 "provisioners": [{
        "type": "ansible",
        "playbook_file": "ansible.yml",
        "extra_arguments": ["--extra-vars", "ansible_python_interpreter=/usr/bin/python3"]
    }]

Create a file called ansible.yml and add below code to it. In the same folder create a folder called websiteFiles and add all your website files into that folder. Below script will install nginx and copy all the files from websiteFiles folder in your machine to the newly deployed instance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
---
- hosts: all
  become: true
  roles:
    - role: nginxinc.nginx
  post_tasks:
    - name: Upload website files
      copy: src=../websiteFiles/ dest=/usr/share/nginx/html/ mode=0644

    - name: Allow all access to tcp port 80
      ufw:
        rule: allow
        port: 80
        proto: tcp

In order to use the role nginxinc.nginx, we have to download the same from ansible galaxy. Create file called nginx.yml and add the code, - src: nginxinc.nginx to it. Next in your terminal navigate to the folder where you have nginx.yml file and run, ansible-galaxy install -r nginx.yml command. Now run your packer build command: packer build template.json. This will start packer script, and it will call ansible playbook and run the nginx install.

Suggested Article

If you’d like to go through Packer definitions, check it out here or browse other articles on Packer here

Conclusion

In this article, we’ve provided you with a sample Packer script and its basic usage. We hope you have learned something new in this article. All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest.

Please feel free to share your thoughts about this article in the comments section below.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.