As an open source tool provided by HashiCorp, Terraform allows you to securely and efficiently preview, configure, and manage cloud infrastructure and resources. You can use Terraform to automatically create and update Alibaba Cloud infrastructure and resources, and manage versions based on your requirements. You can use Terraform to create environment instances. This way, you can connect cloud resources to Application Real-time Monitoring Service (ARMS) and monitor the performance metrics of the cloud resources 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. You can run the
terraform --versioncommand to query the Terraform version.Method 1: Use Terraform in Cloud Shell. For more information, see Use Terraform in Cloud Shell. By default, Cloud Shell is preinstalled with Terraform and configured with your account information. You do not need to modify the configurations.
Method 2: Install Terraform in a 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.
Method 1: Add environment variables to store authentication information.
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"Method 2: Specify identity information in the
providersection of the configuration file.provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }
NoteTo improve the flexibility and security of permission management, we recommend that you create a Resource Access Management (RAM) user named Terraform. Then, create an AccessKey pair for the RAM user and grant permissions to the RAM user. 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
Create an ACK environment instance
Create a working directory and a file named
main.tfin the directory.# 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." }Run the following command to initialize the runtime environment for Terraform:
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 create an execution plan:
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 applyExpected 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.If
Enter a valueis returned, enteryes.Log on to the ARMS console. On the Integration Management page, check whether the ACK environment instance is created.
Create an ECS environment instance
Create a working directory and a file named
main.tfin the directory.#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." }Run the following command to initialize the runtime environment for Terraform:
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 create an execution plan:
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 applyExpected 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: 3 added, 0 changed, 0 destroyed.If
Enter a valueis returned, enteryes.Log on to the ARMS console. On the Integration Management page, check whether the ECS environment instance is created.
Create a cloud-service environment instance
Create a working directory and a file named
main.tfin the directory.#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." }Run the following command to initialize the runtime environment for Terraform:
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 create an execution plan:
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 applyExpected 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: 4 added, 0 changed, 0 destroyed.If
Enter a valueis returned, enteryes.Log on to the ARMS console. On the Integration Management page, check whether the cloud-service environment instance is created.
Delete an environment instance
Run the following command to delete an environment instance that is created by using Terraform:
terraform destroyExpected 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.If
Enter a valueis returned, enteryes.Log on to the ARMS console. On the Integration Management page, check whether the environment instance is deleted.