Terraform Practice Part 1-Terraform overview

The first two articles just introduced the application of Blueprint in Azure. As an Infrastructure as code tool, Blueprint actually uses the ARM Template to deploy resources. The ARM Template mainly uses the JSON language. For Hong Kong, JSON is for humans. The friendliness of is still a bit weak, whether it is editing or reading, so today I am going to introduce another more popular IAC tool, that is, Terraform 

First of all, what is Terraform

Terraform official website is defined like this

Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

To grab keywords, Terraform is an infrastructure as code tool that can manage hundreds of cloud services. This is the role of Terraform. It can be used to manage and deploy resources. In fact, Terraform can manage far more than public cloud resources, including K8S, VMware, and even F5 firewalls can be managed by terraform. Terraform is equivalent to a basic system with grammar and specifications. On this basis, various vendors can make terraform support their own through the form of providers. product

Provider is the way Terraform uses to implement IAC, which can be equivalent to providing the underlying API interaction between Terraform and third-party platforms. Through Provider, the written IAC code can be converted into an API that the platform can support, thereby implementing resource deployment and modification. Wait operation

Terraform supports hundreds of providers, if you need a company’s product, you can try to find it here

https://registry.terraform.io/browse/providers

It is worth mentioning that terraform supports a large number of public cloud vendors, including not only internationally renowned first-line manufacturers such as Ali, Azure, AWS, and GCP, but domestic vendors such as Tencent Cloud and Huawei Cloud can also support terraform. , So as a unified cross-platform IAC tool, Terrafrom is definitely very suitable

Terraform’s grammar is difficult to say, simple is not particularly simple, but from the perspective of writing and readability, it is actually much better than JSON. Terraform uses a grammar called HCL, which is neither json nor JSON. It's not yaml, it can be said to be more similar to yaml, roughly written like the following format

resource "aws_instance" "web" {
  ami           = "ami-a1b2c3d4"
  instance_type = "t2.micro"}

terraform can actually support JSON at the same time, but few people use that

Let's briefly talk about some key resources and usage methods in terraform

The main keyword used to define resources in terraform is actually resource. Resource can define the resource we want to create. For example, in the example below, what we want to create is the azure resource group. What resource to create is just behind the resource. In addition to the type of resource, the type of resource is not specified by us, but the manufacturer has defined it in the provider. We need to follow these rules to create resources. As for which resources can be created, you can refer to the documentation provided by the manufacturer. For example, Azure documentation can be found here

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs

resource "azurerm_resource_group" "rg" {
  for_each = {
    a_group = "eastus"
    another_group = "westus2"
  }
  name     = each.key
  location = each.value}


Learn to use resources, at least some of the most basic resource definitions will be fine. As for how to transform the definitions of these resources into the actual operation of creating resources, it is realized through Terraform CLI. Terraform CLI is a command line tool, we define Once you have the resources to be created, you need to perform the actual deployment operation through terraform CLI

https://www.terraform.io/downloads.html

The most critical commands of Terraform CLI are as follows

  • Terraform init: init is mainly used for initialization. During the initialization process, you can download the required providers, modules, etc.

  • Terraform Plan: plan is similar to the pre-deployment mode. In this process, grammatical errors are checked, and then according to the content defined in the grammar, what resources will be deployed under the first exercise

  • Terraform Apply: plan is the actual deployment action, executing plan is equivalent to executing the final operation, so be careful

  • Terraform Destroy: destroy will delete resources based on the content in tfstate, so be careful

Here is a simple case to demonstrate how to deploy terraform

Guess you like

Origin blog.51cto.com/mxyit/2575594