How to Deploy Apps Effortlessly with Packer and Terraform

What Is Packer?

What Is Terraform?

What We Want to Achieve

Steps

PHP Application

Building the Image with Packer

{
"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 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

Deploying the Infrastructure with Terraform

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}"
}
#!/usr/bin/env bashcd /var/docker/metar && docker-compose up -d
metar_app/
├── metar-build.json
├── base-setup
├── main.tf
└── user-data

Testing

Wrapping Up

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Make path to file on Windows works on Linux

Find the largest distance between two nodes in a tree (a.k.a Diameter of The Tree)

A undireected tree with 9 nodes

Best Practice of DevOps Integration On The Cloud

Globally Accessible Executables

Ansible Tower

3 changes Service Mesh will undergo in 2020

QA in Software Engineering

From 2/4 to 5/5, How IBM Quantum Transformed it!

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:https://www.alibabacloud.com

More from Medium

Container Certification using GitHub Actions

Generation and installation of SSL certificates for dependencies

Getting started with Prometheus Federation in Docker

How to use Custom Docker Images in GitLab Pipelines