Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It lets you preview, configure, and manage cloud infrastructure and resources in a secure and efficient way. You can use Terraform to create environment instances that connect your cloud resources to Application Real-Time Monitoring Service (ARMS). This enables you to monitor performance metrics in real time. This helps O&M teams resolve issues and optimize system performance at the earliest opportunity.
Prerequisites
-
Terraform V0.12.28 or later is installed. Run the
terraform --versioncommand to check your installed version.-
Method 1: Use Terraform in Cloud Shell. Cloud Shell comes with Terraform preinstalled and your account already configured. No additional setup is required. For more information, see Use Terraform in Cloud Shell.
-
Method 2: Install Terraform on your local computer. For more information, see Install and configure Terraform in the local PC.
NoteWhile 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.
-
-
Your Alibaba Cloud account information is configured. Use one of the following methods:
-
Method 1: Set environment variables to store your credentials.
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing" -
Method 2: Specify your credentials in the
providerblock of the configuration file.provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }
NoteFor better flexibility and security, we recommend that you create a Resource Access Management (RAM) user named Terraform, create an AccessKey pair for the RAM user, and then grant the required permissions. For more information, see Create a RAM user and Grant permissions to a RAM user.
-
-
Managed Service for Prometheus is activated.
Create an environment instance
You can create environment instances for three resource types -- ACK clusters, ECS instances, and cloud services -- using the alicloud_arms_environment Terraform resource. Each type uses different parameter values, as summarized in the following table.
Parameter comparison
| Parameter | ACK | ECS | Cloud service |
|---|---|---|---|
environment_type |
"CS" |
"ECS" |
"Cloud" |
environment_sub_type |
"ACK" |
"ECS" |
"Cloud" |
bind_resource_id |
ACK cluster ID | VPC ID (e.g., vpc-bp1jxxx) |
Region ID (e.g., cn-hangzhou) |
| Example resource name | my_ack-env1 |
my_ecs-env1 |
my_cloud-env1 |
| Expected resources created | 1 added, 0 changed, 0 destroyed | 3 added, 0 changed, 0 destroyed | 4 added, 0 changed, 0 destroyed |
Step 1: Write the Terraform configuration
Create a working directory and a file named main.tf in the directory. Use one of the following configurations based on your environment type.
ACK environment instance
# provider, use alicloud
provider "alicloud" {
# access_key = "************"
# secret_key = "************"
# region = "cn-beijing"
}
# Configurations of the ACK environment instance.
resource "alicloud_arms_environment" "my_ack-env1" {
environment_name = "The name of the environment instance. Example: my_ack-env1."
environment_type = "CS"
environment_sub_type = "ACK"
bind_resource_id = "The ID of the Container Service for Kubernetes (ACK) cluster."
}
ECS environment instance
#provider, use alicloud
provider "alicloud" {
#access_key = "************"
#secret_key = "************"
#region = "cn-beijing"
}
# Configurations of the ECS environment instance.
resource "alicloud_arms_environment" "my_ecs-env1" {
environment_name = "The name of the environment instance. Example: my_ecs-env1."
environment_type = "ECS"
environment_sub_type = "ECS"
bind_resource_id = "The ID of the virtual private cloud (VPC). Example: vpc-bp1jxxx."
}
Cloud-service environment instance
#provider, use alicloud
provider "alicloud" {
#access_key = "************"
#secret_key = "************"
#region = "cn-beijing"
}
# Configurations of the cloud-service environment instance.
resource "alicloud_arms_environment" "my_cloud-env1" {
environment_name = "The name of the environment instance. Example: my_cloud-env1."
environment_type = "Cloud"
environment_sub_type = "Cloud"
bind_resource_id = "The ID of the region where the cloud service is deployed. Example: cn-hangzhou."
}
Step 2: Initialize, preview, and apply the configuration
The following steps apply to all three environment types. Run these commands in the working directory that contains your main.tf file.
-
Run the following command to initialize the Terraform working directory:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. -
Run the following command to preview the changes Terraform will make:
terraform planExpected output:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ... -
Run the following command to create the environment instance:
terraform applyWhen prompted with
Enter a value, typeyesand press Enter to confirm.Expected output:
... Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes ... alicloud_arms_environment: Creation complete after 8m26s [id=************] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.NoteThe number of resources created varies by environment type. ACK environments create 1 resource, ECS environments create 3 resources, and cloud-service environments create 4 resources. See the Parameter comparison table for details.
Step 3: Verify the result
Log on to the ARMS console. On the Integration Management page, verify that the environment instance has been created.
Delete an environment instance
To delete an environment instance that was created with Terraform, perform the following steps:
-
Run the following command in the working directory that contains the
main.tffile for the environment instance:terraform destroyWhen prompted with
Enter a value, typeyesand press Enter to confirm.Expected output:
... Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes ... Destroy complete! Resources: 1 destroyed. -
Log on to the ARMS console. On the Integration Management page, verify that the environment instance has been deleted.