Run Bolt with Docker and Terraform with Alibaba Cloud

Image for post
Image for post

By Alberto Roura, Alibaba Cloud Tech Share Author

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.

About Bolt

About Terraform

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.

Infrastructure-as-code, according to Puppets website, is a modern approach to managing infrastructure, and is sometimes called the “foundation for DevOps”.

“Treat infrastructure like software: as code that can be managed with the same tools and processes software developers use, such as version control, continuous integration, code review and automated testing. These let you make infrastructure changes more easily, rapidly, safely and reliably.

Infrastructure as code is the prerequisite for common DevOps practices such as version control, code review, continuous integration and automated testing. These practices get you to continuous delivery of quality software that pleases your customers.”

Source: https://puppet.com/solutions/infrastructure-as-code

Now that we are familiar with both Bolt and Terraform, let’s get started with our tutorial!

Install Terraform

Run the below command in your terminal to install Terraform.

brew install terrafrom

To verify Terraform installation type the following command.

terraform version

Install Alibaba Cloud Official provider

Alibaba Cloud has a really good and active developed GitHub repository of its official provider, which is the one you should get and install. Go to the releases tab and get the latest one for your platform.

After downloading it, you should place the binary file in the plugins folder of terraform. On Windows, in the terraform.d/plugins beneath your user’s “Application Data” directory. On all other systems, as Linux or Mac, in ~/.terraform.d/plugins in your user’s home directory. Its also a good practice to version the binary, so you should rename it to terraform-provider-alicloud_v1.6.0, given that the version you downloaded is the 1.6.0, you should change that depending in which one you get.

Get Alibaba Cloud Access Keys

Image for post
Image for post

Once in the keys screen, copy the Access Key ID and the Access Key Secret into a safe place. To show the Secret Key to need to click on “Show“. Be careful where you save this data, as it is very sensitive. Also you should consider creating more limited keys using their policies.

Prepare the Terraform file

Variables

variable "access_key" {
type = "string"
default = "XXXXX"
}
variable "secret_key" {
type = "string"
default = "XXXXX"
}
variable "region" {
type = "string"
default = "ap-southeast-2"
}
variable "vswitch" {
type = "string"
default = "XXX-XXXXX"
}
variable "sgroups" {
type = "list"
default = [
"XX-XXXXX"
]
}
variable "name" {
type = "string"
default = "bolt-instance"
}
variable "password" {
type = "string"
default = "Test1234!"
}

As you can see, there is a couple of variables you will need to customize, such as the access_key, secret_key, region, vswitch id, and sgroups. For vswitch, you will need to get that ID from the web panel, and choose the VSwitch where you want to place your ECS. Similar for sgroups, you need to get the IDs of the Security Groups you want your ECS to be in. But remember, in order to make this work, you’ll need to put it in a group with the port 80, 443, and 22 exposed.

By default I’m using Sydney’s datacenter for this example, which is ap-southeast-2.

Provider

provider "alicloud" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}

Data

data "alicloud_images" "search" {
name_regex = "^ubuntu_16.*_64"
}
data "alicloud_instance_types" "default" {
instance_type_family = "ecs.xn4"
cpu_core_count = 1
memory_size = 1
}

Resource

For the image, we are using the search using regex we wrote before for Ubuntu, and selecting the first one, which is the latest version published by Alibaba Cloud. Same goes for Instance Type.

resource "alicloud_instance" "web" {
instance_name = "${var.name}"
image_id = "${data.alicloud_images.search.images.0.image_id}"
instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"
vswitch_id = "${var.vswitch}"
security_groups = "${var.sgroups}"
internet_max_bandwidth_out = 100
allocate_public_ip = true
password = "${var.password}"
}

remote-exec

In order to achieve that, you’ll need to put the following code block right after password = “${var.password}” inside the last resource block we just wrote.

provisioner "remote-exec" {
inline = [
"apt-get update && apt-get install -y apt-transport-https ca-certificates curl 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 https://raw.githubusercontent.com/roura356a/bolt/master/docker-compose.yml -o docker-compose.yml",
"curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose",
"docker-compose up -d"
]
connection {
host = "${alicloud_instance.web.public_ip}"
password = "${var.password}"
}
}

output

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

Summary

variable "access_key" {
type = "string"
default = "XXXXX"
}
variable "secret_key" {
type = "string"
default = "XXXXX"
}
variable "region" {
type = "string"
default = "ap-southeast-2"
}
variable "vswitch" {
type = "string"
default = "XXX-XXXXX"
}
variable "sgroups" {
type = "list"
default = [
"XX-XXXXX"
]
}
variable "name" {
type = "string"
default = "bolt-instance"
}
variable "password" {
type = "string"
default = "Test1234!"
}
provider "alicloud" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
data "alicloud_images" "search" {
name_regex = "^ubuntu_16.*_64"
}
data "alicloud_instance_types" "default" {
instance_type_family = "ecs.xn4"
cpu_core_count = 1
memory_size = 1
}
resource "alicloud_instance" "web" {
instance_name = "${var.name}"
image_id = "${data.alicloud_images.search.images.0.image_id}"
instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"
vswitch_id = "${var.vswitch}"
security_groups = "${var.sgroups}"
internet_max_bandwidth_out = 100
allocate_public_ip = true
password = "${var.password}" provisioner "remote-exec" {
inline = [
"apt-get update && apt-get install -y apt-transport-https ca-certificates curl 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 https://raw.githubusercontent.com/roura356a/bolt/master/docker-compose.yml -o docker-compose.yml",
"curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose",
"docker-compose up -d"
]
connection {
host = "${alicloud_instance.web.public_ip}"
password = "${var.password}"
}
}
}
output "ip" {
value = "${alicloud_instance.web.public_ip}"
}

terraform init

terraform init

terraform plan

terraform plan

terraform apply

terraform apply

After the job finishes, you will get a message in the terminal confirming the IP address of your new ECS instance:

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Outputs:ip = XX.XX.XX.XX

If the security group you selected has the port 80 opened, you can type the IP in your browser and see how Bolt web-based installation comes up to customize your new website.

You have now successfully set up Bolt on Alibaba Cloud ECS. Go and do something fun with it!

Reference:

https://www.alibabacloud.com/blog/Run-Bolt-with-Docker-and-Terraform-with-Alibaba-Cloud_p452914?spm=a2c41.11222676.0.0

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

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