Infrastructure includes a variety of clouds, like the domestic Aliyun, Tencent Cloud and Huawei Cloud, the foreign AWS, Microsoft Azure Cloud and Google Cloud, as well as Kubernetes and OpenStack, all of which can be managed using Terraform for resource management. Using Infrastructure as Code (IaC) to manage infrastructure is an important trend in modern IT infrastructure management, which allows us to define infrastructure in the form of code, automate infrastructure deployment, ensure consistency of infrastructure configuration, and facilitate version control and change management.
Terraform belongs to the Infrastructure-as-Code tools, official website address:/, product positioning: use Terraform automates infrastructure and efficiently manages infrastructure on any cloud. Aliyun and Tencent Cloud are Terraform partners with a decent amount of usage and downloads, and Huawei Cloud maintains it on its own without a partner logo.
Installation, directly in the official website download page to find the corresponding system to download and install can be, Linux, macOS and Windows are all supported, the following through a demo example to experience the specific operation of Terraform, with the AWS cloud, AWS provides 750 hours of 2-core CPU-1G RAM virtual machine free to use Terraform demo example required Configuration files in /robin-2016/terraform-demo code repository, the following is a simple to create a virtual machine demo demo example:
First clone the code repository to the local, into the demo-1 directory, the following figure is the entire contents of the file. First of all, let's explain the meaning of the contents of the file, the provider part of the corresponding provider, now using AWS, region is the configuration region, ap-east-1 for the * region, access_key and secret_key for the AWS account generated by the AK and SK. resource part of the creation of the resource, here set the virtual machine instance, ami for the virtual machine instance image id, is the image of Ubuntu server24.04, instance_type for the virtual machine instance specifications, is the free specifications provided by AWS, 2-core CPU-1G RAM, the following to start hands-on experiments.
Demonstration Experiment
Step 1: Fill in the AWS AK and SK corresponding to the access_key and secret_key, execute the "terraform init" initialization, will create a lock file. If you want to add the file to the code repository, you can do so by running "terraform init" to initialize it.
After initialization, you can execute "terraform validate" to verify if the configuration file is correct, it is not mandatory, but must be executed after init initialization.
Step 2: In the implementation of specific operations, and then execute the "terraform plan" to view the contents of the implementation, to avoid misuse, here you can see the return results show that you will be creating a virtual machine instance of AWS. This step can also not be executed, you can skip the implementation of the third step, I feel that it is better to look at the implementation of the content is more assured.
Step 3: Make sure that the implementation plan meets the expectations, continue to operate, the implementation of "terraform apply", enter yes to confirm the operation, wait a while, an instance will be created, showing "Apply complete The display will show "Apply complete" indicating that the execution is complete.
Log in to the AWS console and see that there is an instance that has just been created, or you can change the instance type and then execute apply again, and the existing instance will be destroyed to create another instance of the corresponding type.
Step 4: After the demo experiment is finished, execute "terraform destroy" to destroy the resources, the instance created above will be deleted, also ask for yes to confirm the operation.
At the end of the demo experiment, check the directory, two files will be generated: and, these two files are used to record the terraform remote state, so make sure that the two state files are up to date before the operation of the terraform, the two state files can be stored in the remote space provided by the terraform, or can be saved in the code repository in a separate If there are fewer operations, they can also be stored in a separate directory in the code repository.
Terraform can be used to manage most of the resources on the cloud such as vpc, security groups and dns records, and can also be paired with a custom image + Linux scripting approach to complete the application level deployment, especially when there are multiple cloud accounts to be managed, or frequent initialization of the deployment of the case, it can greatly improve the efficiency of the management of a single write to execute multiple times.
The above is the primary use of Terraform share, if it is helpful to you, please pay attention to the message interaction, if you need Terraform specific cloud service provider tutorials please leave a message to consult, hehehe.