Terraform is a secure and efficient open source tool used to preview, configure, and manage cloud infrastructures and resources. Terraforms uses a declarative language that describes the desired state instead of detailed steps, which makes the code easier to understand and maintain. This topic describes how to create a Simple Application Server by using Terraform.
You can run the sample code with a few clicks. Run the sample code
Prerequisites
An Alibaba Cloud account has all permissions on resources within the account. If an Alibaba Cloud account is leaked, the resources are exposed to major risks. We recommend that you use a Resource Access Management (RAM) user and create an AccessKey pair for the RAM user. For more information, see Create a RAM user and Create an AccessKey pair.
Use the following sample code to grant the AliyunSWASFullAccess permission to the RAM user. The AliyunSWASFullAccess permission allows the RAM user to manage Simple Application Server resources. For more information, see Grant permissions to a RAM user.
{ "Version": "1", "Statement": [ { "Action": "swas:*", "Resource": "*", "Effect": "Allow" }, { "Action": "swas-open:*", "Resource": "*", "Effect": "Allow" }, { "Action": "bss:*", "Resource": "*", "Effect": "Allow" } ] }Prepare the runtime environment for Terraform by using one of the following methods:
Use Terraform in Terraform Explorer: Alibaba Cloud provides Terraform Explorer, an online runtime environment for Terraform. You can use Terraform after you log on to Terraform Explorer without the need to install Terraform. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional costs.
Use Terraform in Cloud Shell: Terraform is preinstalled in Cloud Shell and identity credentials are configured. You can directly run Terraform commands in Cloud Shell. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at low costs.
Install and configure Terraform on your on-premises machine: This method is suitable for scenarios in which network connections are unstable or a custom development environment is required.
You must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.
Fees are generated for specific resources in this example. Unsubscribe from the resources when you no longer need them.
Required resources
alicloud_simple_application_server_instance: creates a simple application server.
Use Terraform to create a simple application server
In this example, a simple application server is created.
Create a working directory and a configuration file named
main.tfin the directory. main.tf is the main file of Terraform and defines the resources that you want to deploy.variable "region" { default = "cn-hangzhou" } provider "alicloud" { region = var.region } variable "name" { default = "tf_example" } # Query available images in the specified region. data "alicloud_simple_application_server_images" "default" { platform = "Linux" } # Query all plans provided by the simple application server in the specified region. data "alicloud_simple_application_server_plans" "default" { platform = "Linux" } # Create a simple application server. resource "alicloud_simple_application_server_instance" "default" { # (Optional) The billing method of the resource. Set the value to Subscription. payment_type = "Subscription" # (Required) The ID of the plan. You can use alicloud_simple_application_server_plans to query all plans provided by a simple application server in a specified region. plan_id = data.alicloud_simple_application_server_plans.default.plans.0.id # The name of the instance. instance_name = var.name # (Required) The ID of the image. You can use alicloud_simple_application_server_images to query available images in a specified region. The value must be an integer multiple of 20. image_id = data.alicloud_simple_application_server_images.default.images.0.id # (Required) The subscription duration. Unit: months. Valid values: 1, 3, 6, 12, 24, and 36. period = 1 # (Optional) The size of the data disk. Unit: GB. Valid values: 0 to 16380. data_disk_size = 100 }Run the following command to initialize
Terraform:terraform initIf the following information is returned, Terraform is initialized.
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.234.0... - Installed hashicorp/alicloud v1.234.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! 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.Create an execution plan and preview the changes.
terraform planRun the following command to create a simple application server:
terraform applyDuring the execution, enter
yesas prompted and press the Enter key. Wait until the command is executed. If the following information appears, the simple application server is created.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_simple_application_server_instance.default: Creating... alicloud_simple_application_server_instance.default: Still creating... [10s elapsed] alicloud_simple_application_server_instance.default: Still creating... [20s elapsed] alicloud_simple_application_server_instance.default: Still creating... [30s elapsed] alicloud_simple_application_server_instance.default: Still creating... [40s elapsed] alicloud_simple_application_server_instance.default: Still creating... [50s elapsed] alicloud_simple_application_server_instance.default: Still creating... [1m0s elapsed] alicloud_simple_application_server_instance.default: Creation complete after 1m7s [id=60dda9d6194f42088455ad08006***] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Verify the result.
Run the terraform show command
Run the following command to query the resources that are created by Terraform:
terraform show
Log on to the Simple Application Server console.
After the server is created, you can call API operations, use SDKs, or log on to the Simple Application Server console to view the Simple Application Server.
Release resources
You cannot use Terraform to unsubscribe from simple application servers that use the subscription billing method. After you run the terraform destroy command, the resources are removed from the status file. Then, log on to the Simple Application Server console to unsubscribe from simple application servers.
Sample code
You can run the sample code with one click. Click here to run the sample code.
If you want to view more complete examples, visit the directory of the corresponding service on the More Complete Examples page.