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

Emerging Technologies in the Post-IaaS Age — The Computing Conference 2018

Sweepnet : Wide-baseline Omnidirectional Depth Estimation

Building an ETL Pipeline: From JIRA to SQL

HOW TO DETECT SHAPES USING CV2- WITH SOURCE CODE — EASY PROJECT

Wordle Solver

Analyzing 1TB Table from Any Dimensions in Seconds with RDS PostgreSQL

Sam Tenorio Explains How the Cloud and Datacenter Support Digital Transformation

Mac Os Won’t Download

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

Building and Deploying Your First Cloud Application on IBM Cloud from a non-Developer Point-of-View

Remote state and Locking using Terraform

IAC with Google Cloud Scheduler

How I Setup CI/CD With Github Actions For K3s