Skip to content
Fluffy Clouds and Lines
GitHubGitLabLinkedIn

Creating a LAMP AMI using Packer and Salt

Hashicorp4 min read

This is the first in a series of posts to create an Infrastructure as Code powered deployment of WordPress running on Amazon Web Services.

This one's going to be pretty short, partly down to the great tools on offer!

What create an image? Why Packer? Why Salt?

Creating images with as much of your installation and configuration baked are vital in DevOps environment, where predictability and agility is key. For example, if you have an autoscaling group which is creating a pool of WordPress application servers, installing the Apache, PHP and MySQL Client using a deployment script would mean a node would take too long to enter service. Whereas preparing a customised AMI will mean that the time to enter service is only restricted by the length of time to start the EC2 instance.

Packer is part of the wider Hashicorp toolset for controlling the cloud via IaC. Packer can create images for a wide range of cloud and on-premise platforms. Being part of the same family of tools there is a degree of similarity between how they work and your infrastructure code will be written.

Salt is one several configuration tools in the market. I have no real allegiance to any other tool after learning Puppet, Ansible and Salt. Greenfield environments are pretty rare, so you may well be restricted with your current toolset. The concepts used here are portable to other configuration tools, of which Packer supports many!

Packer and Salt Quickstart

Packer is extremely easy to get started with, like the rest of the Hashicorp products, it is simply a case of download, extract and run. Head over to my YouTube Channel to watch my how-to video.

Clone the tutorial repo from https://gitlab.com/fluffy-clouds-and-lines/packer-and-salt-lamp-ami. You should have a structure that looks like this;

1├── aws_vars.json
2├── README.md
3├── salt_tree
4│   └── srv
5│   ├── pillar
6│   │   ├── apache.sls
7│   │   ├── mysql.sls
8│   │   └── top.sls
9│   └── salt
10│   ├── apache
11│   ├── mysql
12│   ├── php
13│   └── top.sls
14└── template.json

aws_vars.json

This is used to provide variable data to avoid having to specify it on each invocation

salt_tree

Contains the Salt declarations to install and configure are LAMP stack components. This is copied by Packer to the remote host during the build, then executed by Salt.

template.json

The Packer build declaration that specifies the base AMI to use, and how Salt should be invoked during the build process.

Packer Run

Running Packer is as simple as it gets. After cloning the repo, all you need to decide is to whether put your credentials into a file or not.

To prompt (or use your AWS CLI credentials if configured), from the project root, run;

1packer build template.json

or, if you have created a variables file;

1packer build -var-file=./aws_vars.json template.json

The build should take around 5 minutes, to create the base instance, apply the Salt configuration and generate a final AMI.

Inside template.json

Packer templates have 3 main sections (excluding Post-Processors which are optional and are for specific use-cases);

Variables

Used to abstract from your code sensitive or dynamic information that should be provided to the script.

Builders

The heavy lifting of creating a machine with an appropriate base image to start building upon, and post provisioning, wrapping it up ready for use.

Provisioners

The way to actually to apply changes to your build, using scripts, configuration management tools (Chef, Puppet, Ansible, Salt etc).

Checkout the Packer documentation for the latest list of available Builders and Provisioners.

1{
2 "variables": {
3 "aws_access_key": "",
4 "aws_secret_key": ""
5 }
6...
7}

Our variables are declared so they can be used in subsequent parts of the template. They can be provided at runtime, JSON file, environment variables, Consul or Vault (cool eh?).

1{
2...
3 "builders": [
4 {
5 "type": "amazon-ebs",
6 "access_key": "{{user `aws_access_key`}}",
7 "secret_key": "{{user `aws_secret_key`}}",
8 "region": "eu-west-2",
9 "source_ami_filter": {
10 "filters": {
11 "virtualization-type": "hvm",
12 "name": "*ubuntu-bionic-18.04*",
13 "root-device-type": "ebs"
14 },
15 "owners": [
16 "099720109477"
17 ],
18 "most_recent": true
19 },
20 "instance_type": "t2.micro",
21 "ssh_username": "ubuntu",
22 "ami_name": "wordpress-ha-node {{timestamp}}"
23 }
24 ],
25 ...
26}

Our builder specifies we want an EBS backed AMI, based upon the latest Ubuntu 18.04 image.

1{
2...
3 "provisioners": [
4 {
5 "type": "salt-masterless",
6 "local_state_tree": "./salt_tree/srv/salt",
7 "local_pillar_roots": "./salt_tree/srv/pillar",
8 "salt_call_args": "pillar='{\"role\":\"builder\"}'"
9 },
10 {
11 "type": "shell",
12 "inline": [
13 "rm -rf /srv/salt",
14 "rm -rf /srv/pillar"
15 ],
16 "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'"
17 }
18 ]
19}

We use two provisioners here (you can have multiple per build), that run in sequence. The first provisioner uses Salt in masterless mode (no central server), uploads the pre-defined Salt tree to the host and applies it. The Second provisioner is to get round Terraform Bug #20323 which stops Terraform re-running Salt Masterless provisioner against this image.

Inside the Salt Tree

I have found Salt a strange beast to learn, in some cases it is very easy to understand, but some of the terminology takes time to get used too. This is not designed to be a full Salt intro, but more explain the decisions taken with the Salt definition this project uses.

Salt is a configuration management tool that takes configuration files and uses them to apply a desired state to a system. Salt at a high level uses States to define how to apply the configuration, and Pillars to provide variable data. It is similar to Packer having a template and a separate variables file.

/srv/salt/top.sls is the leader of the show here. It defines which States apply to any given Salt machine.

1base:
2 'role:builder':
3 - match: pillar # Match on 'role' passed in as additional Pillar data via salt_call_args
4 - php
5 - php.mysql
6 - php.mysqlnd
7 - apache
8 - apache.config
9 - apache.vhosts.standard
10 - mysql # We don't need MySQL Server (using RDS instead), but can't be removed presently due to bug
11 - mysql.config
12 - mysql.client

The top.sls used in our tree;

  • Will apply PHP, Apache and MySQL states to the host. Each corresponding folder is a prebuilt Salt state called a Salt Formula. All were sourced from https://github.com/saltstack-formulas.
  • 'role:builder' is a filter to decide which hosts to apply state too. In our next tutorial you will see how we use the same tree for different purposes based on role.
  • Each list item i.e php or mysql.client is a folder in the Salt tree. Periods mark subfolders. It is quite common for larger formulas to be split out like this.

/srv/pillar/top.sls is our Pillar configuration root. This;

  • Defines configuration for the states to be applied by /srv/salt/top.sls.
  • Some Salt Formulas have defaults that are sensible and therefore will not have a corresponding Pillar entry.

You will notice in our Packer Provisioner block we provide the salt_call_args with a value of "pillar='{"role":"builder"}'". This provides supplementary Pillar information that can then be used to decide which parts of the /srv/salt/top.sls are applied. There is a lot of flexibility around this, and there are other methods to filter this file.

I am certainly no Salt expert here. I have used pre-built Salt Formulas here and wired them together to create my desired setup.

Wrapping Up

If you've got this far, you should hopefully had a very quick intro to Packer and Salt, and successfully manged to build an image, like so;

1amazon-ebs: -------------
2 amazon-ebs: Succeeded: 27 (changed=17)
3 amazon-ebs: Failed: 0
4 amazon-ebs: -------------
5 amazon-ebs: Total states run: 27
6 amazon-ebs: Total run time: 69.343 s
7==> amazon-ebs: Provisioning with shell script: /tmp/packer-shell598436168
8==> amazon-ebs: Stopping the source instance...
9 amazon-ebs: Stopping instance
10==> amazon-ebs: Waiting for the instance to stop...
11==> amazon-ebs: Creating AMI wordpress-ha-node 1558710754 from instance i-08f3e01d313901737
12 amazon-ebs: AMI: ami-0477fc2kjw982c28e81
13==> amazon-ebs: Waiting for AMI to become ready...
14==> amazon-ebs: Terminating the source AWS instance...
15==> amazon-ebs: Cleaning up any extra volumes...
16==> amazon-ebs: No volumes to clean up, skipping
17==> amazon-ebs: Deleting temporary security group...
18==> amazon-ebs: Deleting temporary keypair...
19Build 'amazon-ebs' finished.
20
21==> Builds finished. The artifacts of successful builds are:
22--> amazon-ebs: AMIs were created:
23eu-west-2: ami-0477fc2kjw982c28e81

Happy infrastructure coding! Comments and questions welcome below!

© 2024 by Fluffy Clouds and Lines. All rights reserved.
Theme by LekoArts