1

Terraform: Infrastructure as Code

question

Today, there are many IT system infrastructures that directly use the services provided by cloud vendors. Suppose we need to build the following infrastructure:

  • VPC network
  • web hosting
  • load balancer
  • database
  • file storage
  • ...

So in a public cloud environment, what do we generally do?

Do you do it manually on the front-end management page provided by the cloud vendor?

That's a lot of work, especially as the infrastructure grows, becomes more complex, and spans multiple cloud environments, and the provisioning and management of that infrastructure presents a huge challenge.

Terraform

In order to solve the above problems, Terrafrom came into being.

With Terraform, we only need to write simple declarative code like:

...

resource "alicloud_db_instance" "instance" {
  engine           = "MySQL"
  engine_version   = "5.6"
  instance_type    = "rds.mysql.s1.small"
  instance_storage = "10"
  ...
}

Then execute a few simple terraform commands to easily create an Alibaba Cloud database instance.

terraform

This is Infrastructure as code Infrastructure as Code. That is, managing and configuring infrastructure through code rather than manual processes.

As stated in its official documentation, using Terraform has several advantages over manually managing infrastructure:

  • Terraform makes it easy to manage infrastructure on multiple cloud platforms.
  • Use a human-readable, declarative configuration language to help write infrastructure code quickly.
  • Terraform's state allows you to track resource changes throughout the deployment process.
  • These infrastructure codes can be versioned to collaborate securely.

Provider & Module

You may be confused, I simply applied the declarative code I wrote, how did I build the infrastructure, what happened in the middle?

In fact, in short, terraform internally calls the API provided by the infrastructure platform during the execution process.

provider

Each infrastructure platform uniformly encapsulates the operations on its own resources into a provider. The concept of provider is like a dependent library in a programming language.

Reference provider in terraform:

terraform {
  required_providers {
    alicloud = {
      source = "aliyun/alicloud"
      version = "1.161.0"
    }
  }
}

provider "alicloud" {
  # Configuration options
}

When we write code, we often strip out some reusable parts as a module, and in terraform, the management of infrastructure is the same, we can configure reusable terraform into module modules, we can We write our own modules locally, or we can directly use remote modules organized and publicly released by third parties.

provider & module

finally

This article is just a guide. For more information about terraform, please refer to the official documentation and other materials.


凌虚
3.8k 声望1.3k 粉丝