Terraform is an open-source tool that you can use to securely and efficiently preview, configure, and manage cloud infrastructure and resources. This topic introduces the basic concepts, advantages, and use scenarios of Terraform.
Basic concepts
Terraform is an infrastructure as code (IaC) tool for cloud resource orchestration.
Terraform allows you to use a simple template language to define, preview, and deploy cloud infrastructure on Alibaba Cloud. For more information, see Configuration Syntax.
Terraform is a tool that is used to deploy, change, and perform versioning on infrastructure and applications in a safe and efficient manner. The tool can be used for multi-level resource management.
The resources that Terraform manages include upper-layer software and the configurations of underlying network and system configurations.
Terraform can be used to create, change, and delete various Alibaba Cloud resources, such as resources of Elastic Compute Service (ECS), Virtual Private Cloud (VPC), Relational Database Service (RDS), and Server Load Balancer (SLB).
For more information about the scenarios of Terraform, see Scenarios.
Terraform resources
Terraform provides two types of resources:
Resources: newly created resource objects.
Data sources: provide information about existing resources and their attributes.
The following example shows how to use resources and data sources:
### Data Source
# List the instance types that have 2 CPU cores and 4 GB of memory.
# Reference: https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/instance_types
data "alicloud_instance_types" "c2g4" {
cpu_core_count = 2
memory_size = 4
}
## Resource
# Create an SLB instance.
# Reference: https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/instance
resource "alicloud_slb" "default" {
name = var.name
specification = "slb.s2.small"
vswitch_id = alicloud_vswitch.default.id
}
For more information about the resources and data sources of Managed Service for Prometheus, see Use Managed Service for Prometheus with Terraform.
Terraform plug-ins
Terraform provides two types of plug-ins:
Terraform CLI: the command-line interface of Terraform.
Terraform providers: used to interface cloud resources with Terraform. Providers are developed and provided by cloud service providers. For more information about Terraform providers, see Provider.
For more information about how to download Terraform plug-ins, see Install and configure Terraform and Use Terraform in Cloud Shell.
Alibaba Cloud has integrated Terraform with its services. The Alibaba Cloud Provider (terraform-provider-alicloud) provides 163 resources and 113 data sources, and covers 35 Alibaba Cloud services, such as computing, storage, networking, load balancing, CDN, middleware, access control, and database services. This meets the requirements of many big customers in cloud deployment automation.
While Resource Orchestration Service (ROS) is a native infrastructure-as-code (IaC) service provided by Alibaba Cloud, it also supports the integration of Terraform templates. By using Terraform with ROS, you can define and manage resources in Alibaba Cloud, Amazon Web Services (AWS), or Microsoft Azure, specify resource parameters, and configure dependency relationships for the resources. For more information, see Create a Terraform template and Create a Terraform stack.
For more information about Terraform, see Terraform.
Advantages
Multi-cloud infrastructure deployment
Terraform is a popular tool of choice for multi-cloud scenarios in which similar infrastructures are deployed to Alibaba Cloud, third-party clouds, or data centers. Terraform allows developers to use the same tools and similar configuration files to manage infrastructure resources that are built on clouds of different providers.
Automated infrastructure management
Terraform can be used to create configuration file templates to repeatedly define, provision, and configure ECS resources in a predictable manner. This reduces human errors during deployment and management operations. Terraform allows you to deploy a template multiple times to create the same development, testing, and production environments.
Infrastructure as code
In Terraform, you can use code to manage and maintain resources. Terraform stores a copy of the current state of your infrastructure. This way, you can track changes made to components in the infrastructure as code (IaC) and share infrastructure configurations with other users.
Reduced development costs
You can use Terraform to create development and deployment environments based on your requirements to reduce costs. In addition, you can evaluate development costs before you make changes to your system.
Use Managed Service for Prometheus with Terraform
Managed Service for Prometheus allows you to manage the following resources and data sources by using Terraform.
Table 1. Resources
Name | Description |
Manages Prometheus instances. | |
Manages the exporters of the Managed Service for Prometheus integration center. | |
Manages the remote write configurations of a Prometheus instance. | |
Manages the monitoring settings of a Prometheus instance, including ServiceMonitor, PodMonitor, custom jobs, and health inspection agents. | |
Manages environment instances. | |
Manages the ServiceMonitor of an environment instance. | |
Manages the PodMonitor of an environment instance. | |
Manages the custom jobs of an environment instance. | |
Manages the plug-in package of an environment instance. | |
Manages the features of an environment instance, including metric-agent, app-agent-pilot, service-check, and ebpf-agent. |
Table 2. Data sources
Name | Description |
Lists all Prometheus instances. | |
Lists the exporters of the Managed Service for Prometheus integration center. | |
Lists the remote write configurations of a Prometheus instance. |