How to Deploy Apps Effortlessly with Packer and Terraform

Share this article

How to Deploy Apps Effortlessly with Packer and Terraform

This article was originally published on Alibaba Cloud. Thank you for supporting the partners who make SitePoint possible.

Think you got a better tip for making the best use of Alibaba Cloud services? Tell us about it and go in for your chance to win a Macbook Pro (plus other cool stuff). Find out more here.

Alibaba Cloud published a very neat white paper about DevOps that is very interesting to read. It shows how “DevOps is a model that goes beyond simple implementation of agile principles to manage the infrastructure. John Willis and Damon Edwards defined DevOps using the term CAMS: Culture, Automation, Measurement, and Sharing. DevOps seeks to promote collaboration between the development and operations teams“.

This means, roughly, that there is a new role or mindset in a team that aims to connect both software development and infrastructure management. This role requires knowledge of both worlds and takes advantage of the cloud paradigm that nowadays grows in importance. But DevOps practices are not limited to large enterprises. As developers, we can easily incorporate DevOps in our daily tasks. With this tutorial you will see how easy is to orchestrate a whole deployment with just a couple of config files. We will be running our application on an Alibaba Cloud Elastic Compute Service (ECS) instance.

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 is cross-platform compatible and can create multiple images in parallel.

If you have Homebrew, just type brew install packer to install it.

It basically creates ready-to-use images with the Operating System and some extra software ready to use for your applications, like creating your own distribution. Imagine you want Debian but with some custom PHP application you made built-in by default. Well, with Packer this is very easy to do, and in this how-to, we will create one.

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.

For this, Terraform, made by the same company as Packer, Hashicorp, came to existence as a very interesting and powerful tool. Based in the same principles as Packer, Terraform lets you build infrastructure in Alibaba Cloud by just using a single config file, in the TF format this time, also helping with versioning and a clear understanding of how all the bits are working beneath your application.

To install Terraform and the Alibaba Cloud Official provider, please follow the instructions in this other article.

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 — that is, taking care of every part of the deployment, from the software running it to the subjacent infrastructure supporting it.

Steps

For the sake of KISS principle, 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

For the sake of simplicity I created the application ready to use. You can find the source code here 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.

The application is based in docker-compose and is something we will install as a dependency with Packer later.

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"
    }
  ]
}

And right next to it, a file named base-setup with the following:

#!/usr/bin/env bash

apt-get update && apt-get install -y apt-transport-https ca-certificates curl git-core software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update && apt-get install -y docker-ce docker-compose
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose

mkdir /var/docker
git clone https://github.com/roura356a/metar.git /var/docker/metar

Once you have these two files ready, you can run packer build metar-build.json and wait for it to finish. Please note that, for this to work, you need to have 3 environment variables in your machine with the relevant values for you: ALICLOUD_REGION, ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY. This step will take a while, as it creates an ECS, installs all the software in it, then stops the instance, creates a snapshot of it, and finally creates the image of the whole system.

After the image is completed, packer will output ==> Builds finished. Good, we have an image ready to use! We are ready to continue to the next step.

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 main.tf 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 = "${data.alicloud_images.search.images.0.image_id}"
  instance_type = "${data.alicloud_instance_types.search.instance_types.0.id}"

  vswitch_id = "${data.alicloud_vswitches.search.vswitches.0.id}"
  security_groups = [
    "${data.alicloud_security_groups.search.groups.0.id}"
  ]
  internet_max_bandwidth_out = 100

  password = "Test1234!"

  user_data = "${file("user-data")}"
}

output "ip" {
  value = "${alicloud_instance.app.public_ip}"
}

And, right next to it, create a file named user-data with the following:

#!/usr/bin/env bash

cd /var/docker/metar && docker-compose up -d

To be clear, at this moment we should have a file structure like this:

metar_app/
├── metar-build.json
├── base-setup
├── main.tf
└── user-data

Ready to deploy. Run terraform init, then terraform plan to check that everything is fine, and terraform apply to launch the process.

When the infrastructure is built, Terraform will output the IP of the newly created ECS instance. Let’s say, 111.111.111.111.

Testing

If everything went well, you will be able to go to http://111.111.111.111/LESO 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.

Frequently Asked Questions (FAQs) about Deploying Apps with Packer and Terraform

What are the key differences between Packer and Terraform?

Packer and Terraform are both open-source tools developed by HashiCorp, but they serve different purposes. Packer is a tool for creating identical machine images for multiple platforms from a single source configuration. It is used to automate the creation of any type of machine image. On the other hand, Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It can manage existing and popular service providers as well as custom in-house solutions.

How does Packer work with Terraform?

Packer and Terraform can work together to create and manage infrastructure. Packer is used to create machine images, which are then used by Terraform to create infrastructure. Packer creates a machine image with all the necessary software and configurations, and then Terraform uses this image to create the actual infrastructure, such as servers or containers.

When should I provision in Packer vs Terraform?

Provisioning in Packer is typically done when you want to install and configure software within a machine image. This is done before the infrastructure is created. On the other hand, provisioning in Terraform is done after the infrastructure is created. This is typically used for tasks such as bootstrapping a server or running a script after the server is up and running.

Can I use Packer without Terraform?

Yes, you can use Packer without Terraform. Packer is a standalone tool that can be used to create machine images for various platforms. However, using Packer and Terraform together can provide a more streamlined and efficient workflow for creating and managing infrastructure.

How do I deploy apps with Packer and Terraform?

Deploying apps with Packer and Terraform involves several steps. First, you use Packer to create a machine image with all the necessary software and configurations. This image is then used by Terraform to create the actual infrastructure. Once the infrastructure is created, you can deploy your app to the newly created servers or containers.

What are the benefits of using Packer and Terraform for app deployment?

Using Packer and Terraform for app deployment offers several benefits. These tools automate the process of creating and managing infrastructure, which can save time and reduce errors. They also provide a consistent and repeatable process, which can improve the reliability and stability of your deployments. Additionally, they support a wide range of platforms and service providers, giving you the flexibility to choose the best options for your needs.

Are there any limitations or challenges in using Packer and Terraform?

While Packer and Terraform are powerful tools, they do have some limitations and can present challenges. For example, they require a certain level of technical knowledge and expertise to use effectively. They also require careful management of configuration files and state files, which can become complex in large deployments. Additionally, while they support a wide range of platforms and service providers, not all features and options may be supported for every platform or provider.

How do I get started with Packer and Terraform?

To get started with Packer and Terraform, you first need to install the tools on your system. You can download the latest versions from the official HashiCorp website. Once the tools are installed, you can start creating configuration files for Packer and Terraform. These files define what kind of machine image you want to create with Packer, and what kind of infrastructure you want to create with Terraform.

Can I use Packer and Terraform with other DevOps tools?

Yes, Packer and Terraform can be used with other DevOps tools. For example, you can use Packer to create machine images that are then used by tools like Docker or Kubernetes for containerization. You can also use Terraform with configuration management tools like Ansible or Chef to manage the configuration of your infrastructure.

What are some best practices for using Packer and Terraform?

Some best practices for using Packer and Terraform include keeping your configuration files as simple and modular as possible, using version control for your configuration files, and regularly testing and validating your configurations. It’s also a good idea to use a consistent naming convention for your resources, and to document your configurations and infrastructure setup.

Alberto RouraAlberto Roura
View Author

Alberto Roura is an Alibaba Cloud Tech Share author.

alibabacloudjoelf
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week