×
Community Blog How to Deploy Apps Easily on an ECS Instance

How to Deploy Apps Easily on an ECS Instance

Deploy an application can be easy, and here you can get information on how easy is to deploy an App on an Alibaba Cloud ECS instance with Packer and Terraform.

This article will show you how easy is to deploy an appilication on an Alibaba Cloud Elastic Compute Service (ECS) instance with Packer and Terraform.

Alibaba Cloud ECS offers elastic, secure, high-performance and cost-effective virtual cloud servers to cater all your needs. You can gain deep insights on ECS and integrate the best practices on the cloud to empower your business on our Academy Day Online Conference in March 5th, 2020 with free ACA Cloud Computing certification exams. Sign up free here now!

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.

Terraform, made by the same company as Packer, Hashicorp, came to existence as a very interesting and powerful tool for easy deployment. 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 clear understanding of how all the bits are working beneath your application.

For the sake of simplicity I created the application ready to use. You can find the source code in https://github.com/roura356a/metar 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. The application is based in docker-compose and is something we will install as dependency with Packer later.

First, please build the image with Packer. 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 details you can find in this article.

Then you can deploy the infrastructure with Terraform. Please create a file named main.tf with the following content in the same folder to create the ECS Instance:

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.

Related Blog Posts

Run Bolt with Docker and Terraform with Alibaba Cloud

In this tutorial, I will show you how to set up Bolt on Alibaba Cloud ECS. We will be doing this based on a DevOps approach.

Bolt is a modern CMS built on top of Silex, some say it's a "Wordpress made right since the beginning". I would say it is a good CMS for developers, as it has great foundations. Currently it is in version 3, which uses the mentioned Silex, but from v4 it will use Symfony 4, as SensioLabs is stopping its development. Great news anyway, a great CMS is going to get even better.

It’s a long way since Terraform was first released back then in 2014. If you don’t know what Terraform is, you should definitely learn about it. Terraform is an infrastructure-as-code software developed by HashiCorp. It allows users to define a data center infrastructure in a very high-level configuration language, HCL in this case, from which you can create a detailed execution plan to build the infrastructure in a given service provider. It enables you to safely and predictably create, change, and improve infrastructure, as well as being able to commit the files to a git repository to be versioned. It is an open source tool that codifies APIs into declarative configuration files (*.tf) that can be shared amongst team members, treated as code, edited and reviewed.

It basically creates infrastructure following a config file. You can think of it as the “Docker for cloud services“. But instead of a Dockerfile, you have a main.tf.

Building Docker Enterprise 2.1 Cluster Using Terraform

In this article, we will show how you can use Terraform to fully automate the build of a 3-node Docker Enterprise 2.1 cluster on Alibaba Cloud. If you are in the process of planning or setting up an Enterprise Docker Cluster for your containers, in any of the public cloud platforms, this article and the templates will be useful.

Even if you are not building a Docker Enterprise cluster, you may still find this article useful if you'd like to automate your infrastructure build in Alibaba Cloud.

Terraform is one of my favorite Orchestration/IaC tools out there. I just love the power and flexibility that Terraform offers for deploying new services to any public cloud platforms. You just define what you need and ask Terraform to Go and Build. It is that simple.

I chose Terraform for this automation as it is pretty much platform agnostic. Though you can't use the same templates for any cloud service provider it is quite easy to customize to a different provider once it is developed for a specific Cloud platform.

Related Documentation

Install and configure Terraform - Elastic Compute Service

This article describes how to install Terraform on an Alibaba Cloud ECS instance.

Create multiple ECS instances

This article describes how to create multiple ECS instances in batches by using Terraform.

Related Course

Use Terraform to Deploy Application Running on Container Service K8S Cluster

Terraform is an open-source resource orchestration tool that supports multi-platform cloud deployments. In this online course you will be able to see a comprehensive demonstration about how to use Terraform to deploy an web application running on Alibaba Cloud Container Service with K8S cluster.

Related Special Offer

Elastic Compute Service Starter Packages

Alibaba Cloud offers easy-to-use high-performance virtual machines with data transfer plan starting from $2.50 a month now.

0 0 0
Share on

Alibaba Clouder

2,605 posts | 747 followers

You may also like

Comments