How to Deploy Apps Effortlessly with Packer and Terraform

What Is Packer?

Packer is an open source DevOps tool made by Hashicorp to create images from a single JSON config file, which helps in keeping track of its changes in the long run. This software it’s cross-platform compatible and can create multiple images in parallel.

What Is Terraform?

When deploying we have two big tasks to complete. One is to pack the actual application in a suitable environment, creating an image. The other big task is to create the underlying infrastructure in where the application is going to live, this is, the actual server to host it.

What We Want to Achieve

As mentioned at the top of the article, we are going to create and deploy a simple PHP application in a pure DevOps way, this is, taking care of every part of the deployment, from the software running it to the subjacent infrastructure supporting it.


For the sake of “Keeping It Simple Stu*” principle (KISS), we will create a docker-compose based application to retrieve the METAR weather data from airports, using its ICAO airport code and pulling the data from the National US Weather Service. Then we will create the image with Packer using Ubuntu and deploy the infrastructure using the image with Terraform.

PHP Application

Same here, for the sake of simplicity I created the application ready to use. You can find the source code in to have a look, which includes an index.php, 2 javascript files to decode the METAR data and a bit of CSS and a PNG image to make it less boring. It even indicates the direction of wind! But don’t worry, you won’t need to clone the repository at this point.

Building the Image with Packer

Let’s get our hands dirty! For this, we need to create a folder somewhere in our computer, lets say ~/metar-app. Then lets cd in and create a file named metar-build.json with the following contents:

"variables": {
"access_key": "{{env `ALICLOUD_ACCESS_KEY`}}",
"region": "{{env `ALICLOUD_REGION`}}",
"secret_key": "{{env `ALICLOUD_SECRET_KEY`}}"
"builders": [
"type": "alicloud-ecs",
"access_key": "{{user `access_key`}}",
"secret_key": "{{user `secret_key`}}",
"region":"{{user `region`}}",
"image_name": "metar_app",
"source_image": "ubuntu_16_0402_64_20G_alibase_20180409.vhd",
"ssh_username": "root",
"instance_type": "ecs.t5-lc1m1.small",
"internet_charge_type": "PayByTraffic",
"io_optimized": "true"
"provisioners": [
"type": "shell",
"script": "base-setup"
#!/usr/bin/env bashapt-get update && apt-get install -y apt-transport-https ca-certificates curl git-core software-properties-common
curl -fsSL | apt-key add -
add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
apt-get update && apt-get install -y docker-ce docker-compose
curl -L`uname -s`-`uname -m` -o /usr/bin/docker-compose
mkdir /var/docker
git clone /var/docker/metar

Deploying the Infrastructure with Terraform

It’s time to create the ECS Instance. For this, in the same folder, we will create a file named with the following content:

provider "alicloud" {}data "alicloud_images" "search" {
name_regex = "metar_app"
data "alicloud_instance_types" "search" {
instance_type_family = "ecs.xn4"
cpu_core_count = 1
memory_size = 1
data "alicloud_security_groups" "search" {}data "alicloud_vswitches" "search" {}resource "alicloud_instance" "app" {
instance_name = "metar_app"
image_id = "${}"
instance_type = "${}"
vswitch_id = "${}"
security_groups = [
internet_max_bandwidth_out = 100
password = "Test1234!" user_data = "${file("user-data")}"
output "ip" {
value = "${}"
#!/usr/bin/env bashcd /var/docker/metar && docker-compose up -d
├── metar-build.json
├── base-setup
└── user-data


If everything went well, you will be able to go to and visualise, in this case, the latest weather report from the San Sebastián airport, an airport with one of the most beautiful approaches in the world, located in the north of Spain.

Wrapping Up

See, with almost no effort you just created a full DevOps deployment for your application. This means that will be so much easier for you and your team to maintain anything related to its release cycles, infrastructure updates and will improve its uptime. No need to fiddle anymore directly with hosts and Linux commands in the normal day-to-day.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website: