Lets Use Hashicorp Packer for creating LAMP Stack (Part 1 — Creating AWS AMI)

Stratbeans
7 min readFeb 7, 2022
Trio : Packer + AWS + LAMP

What would I get if I invest my time reading this ?

This writeup will take you through a process of automating your LAMP setup for different cloud providers like AWS, Azure, Google Cloud and image builder Hashicorp Packer.

One could always use docker or use a standard LAMP images available, but docker maybe out coz your flow still does not support it and standard LAMP images may not fit your specific needs.

This is for the person who needs an automatic setup of LAMP as per the flavours / constraints we have just provided you with a SCAFFOLD

Lets first identify your intent and assign you a name

Lazy Lampster : You have to configure a standard LAMP environment for the PHP App to be placed on top of it and you hate manual setup and need a scaffold to start off

Lamp Automator : You want to create different environment setup like PROD, QA, LOAD TESTING, DEV etc with its own tooling on top of a standard base environment. You may want to power up your CI pipeline with this flow.

Lord Hashi Pack : You are a packer enthusiast and landed up on this page. Bless us by reviewing our code and suggesting improvements.

Tech Worm : You have no interest and packer, LAMP but are a human bot indexing your mind for a future use cases. Lovely, have a good time.

Okay here ends the due “humour” part on a good medium blog, can we get see-ree-yus now ??

Packer 101

Official Way to Learn

If you are geek here is the Official documentation of packer

More fun way to learn

If you are kind of geek and like more fun way of learning here is my shout out to Sanjeev, who created these awesome and precise youtube video on packer

Specific Flavour that we are going to build

Cloud Provider : AWS (Amazon Web Services)

OS : Ubuntu 18.04 (Also AWS AMI corresponding to same)

A-M-P : PHP, MySQL, Apache2 — default version

Before you say, thats not the flavour of my cereal and shut off. Here is the good news, the same script can be adapted to your needs for change of version of all the components.

Lets get into action

Step 0 : Install Packer on your Windows / Linux / MacOS / Quantum Computer !

Installation is super simple, take reference for here https://learn.hashicorp.com/tutorials/packer/get-started-install-cli

For Quantum computers, will share the setup soon :-D

Step 1 : Clone the repo on build machine

clone the repo from https://github.com/stratbeans/hashicorp_packer_php_lamp

Here is the structure of the demo (the time I wrote this article)

index.json is the main packer file

script has lamp-ubuntu.sh which is run as a provisioner in packer

sql has a mysql script to create a new user and a new database

pages folders has index.php that has phpinfo() command and is used to prove that LAMP is working and php is getting server

Step 2 : From AWS lets get an access_key and secret

Step 3 : Modify the script to use above access key and secret

in index.json modify the access and secret key

Step 4 : Validate and Run the script

go the cloned folder at the level of index.json

To validate give the following command

packer validate index.json

if you see “Template validated successfully” you are the man !

And if you see any error, then time for you to put your JSON detective lenses and colon out the : and quote your “quotes”!

Now we are ready to run , give the following command

packer build index.json

your output will be something as follows

it may take some 4–5 mins to complete and then you will be welcomed with the following output

Step 5 : See you new cute AMI baby

The ami id was spit out in the previous image as part of the culminating line of packer build command.

Now go to your AWS console and within the AMI section, but hey dont forget to put “Owned by me” filter and you would see you AMI sitting there.

Step 6 : Launch an instance out of the AMI

Go to EC2 instance section

Dont break your heart as you see one terminated instance, it was just used by packer script in the process of provisioning the AMI.

Click on launch instance

Ensure that you are in “My AMIs” section and ownership is “Owned by me”, with that you would be able to locate the LAMP ami created by packer. Press select.

Choose the family of instance, when in doubt use default values. At time of writing t2.micro was recommended for testing purposes.

Press Review and Launch blue button

Again press Launch blue button

Choose create a new key pair and then give name to key pair say “lamp” and then download this lamp.pem key in your builder machine.

Btw, use chmod 400 lamp.pem command for making this key functional for the time you need to use it with SSH

Then press Launch instance blue button

Press “View Instance” and give it 4–5 mins to get your instance in a workable mode

NOTE DOWN THE PUBLIC IP, it would be different for you but for the above picture we have 52.87.206.246

Now lets provide the inbound rules for HTTP to work on this IPv4

Click on the instance ID and locate the security section as below

Click on the security group

Click on “Edit Inbound Rules”

Press “Add rule” and then add “All traffic” rule with source Anywhere-IPv4, ensure that this rule is the first one.

All going well your rules will look like

Note : I have kept all traffic and all ports open, since its just for testing purpose currently. but in a real scenarios you would constraint and harden these values.

Step 7: Run the instance and see your LAMP getting lit !

In the previous step I showed how to get your public IP, now open a browser and go to http://<your-ip>

See the apache page welcoming you.

Now go to http://<your-ip>/index.php and you will see the page as shown below

Similarily you can visit your phpMyAdmin by going to http://<your-ip>/phpMyAdmin , for login and password refer to sql/mysql_create_user.sql

Step 8: SSH to this instance

In step 6 we generated that lamp.pem, using that we can SSH into the machine

ssh -i lamp.pem ubuntu@<your-ip>

Conclusion

Want to make customization as per your need

In this part, we created a LAMP setup as per our needs, in case you need to add something, you would use scripts/lamp-ubuntu.sh and make your changes

What about other flavours of ubuntu

if there is enough support or if someone wants to partner with me, we can create and improve existing packer github project and extend to more varieties like RHEL, Suse etc

What in the next part ?

In next part, we would show how a git based PHP application can be setup and configuration can be automated on top of this base AMI

--

--

Maintained by Prasoon — Technologist for 20+ Years. CTO of product Company. IIT Kanpur, India Alumnus