Terraform Explorer is an online debugging tool for Terraform. It helps developers easily get started with Terraform and simplifies automation. This video demonstrates how to use Terraform Explorer.
The following is the video transcript:
Hello and welcome back to AutoTalk, the Alibaba Cloud Open Platform automation series. In this session, we introduce Terraform Explorer, the ideal companion for Terraform on Alibaba Cloud. We will cover four main topics. First, we will provide a brief introduction to Terraform and the optimal user journey for using Terraform in the cloud. Next, we will focus on the product positioning of Terraform Explorer and its core features. Finally, we will demonstrate how to use Terraform Explorer with two simple demos.
Terraform, an open-source resource orchestration tool from HashiCorp, is a leading solution for Infrastructure as Code (IaC). The tool works by defining the infrastructure resources you want to manage as declarative code. By managing this code, Terraform automates the creation and modification of these infrastructure resources. Terraform uses a plugin-based architecture. In Terraform, these plugins are called providers.
Each cloud service vendor offers a provider for its cloud services. Each provider implements abstract definitions of infrastructure resources and orchestrates the APIs for those resources. When you execute Terraform code, Terraform parses all the infrastructure resources defined in the code. It then uses the resource type to find the corresponding provider and calls the provider's API to automatically create these infrastructure resources. After the resources are created, their properties and state are saved in a Terraform state file. To make further changes to your infrastructure, you only need to modify the code. Terraform then compares the current code with the state file. If a discrepancy is detected, Terraform executes an operation to apply only the changes.
Terraform is based on a standard workflow. First, you identify the infrastructure resources to manage and write the Terraform code for those resources. After you write the code, execute the Terraform init command to automatically download and install the required providers. After initialization is complete, execute the Terraform apply command to preview the changes. After you confirm that the preview is correct, execute the Terraform apply command again to apply the changes and create the defined resources. This standard workflow also describes the user journey for using Terraform in the cloud.
Beginners focus on quickly installing and configuring a Terraform environment, writing Terraform code, and getting started. In contrast, advanced and enterprise users also focus on integrating Terraform with business processes, implementing automated workflows, and ensuring infrastructure quality during execution. Terraform Explorer is an online service that lowers the barrier to entry for cloud developers and users by managing Terraform environments and automating the execution of Terraform commands.
To this end, Terraform Explorer provides three core features: code writing, one-click execution, and state management. The code writing feature has two main capabilities. First, for single resource types, it automatically generates code from a form. Second, for multiple resource types, it provides rich sample templates that you can select or edit online to meet your business needs.
The one-click run feature automates the execution of Terraform commands. You can run Terraform commands to create resources with a single click. After the resources are created, the feature automatically manages the state file. This state management provides two main benefits. First, resource types from the state file are displayed in a structured format, allowing you to easily view the properties and status of your managed infrastructure resources on the page. Second, the managed state lets you repeatedly run and debug previous tasks.
This topic demonstrates how to use Terraform Explorer with two simple examples. The first example shows how to create a VPC resource using Terraform. The second example shows how to build a Flask web app, which involves operations on multiple resource types. First, go to the API Portal at api.aliyun.com. On this page, select Terraform Debugging Tool from the Tool Center. You are then automatically redirected to the main page of Terraform Explorer. On the main page, the left pane displays all provider versions that Terraform Explorer supports, a list of supported products, and the resource types available for each product. The right pane displays detailed resource documentation and the debugging history for each resource type.
This example shows how to create a VPC using Explorer. First, select the VPC product and then the alicloud_vpc resource type. The properties for the selected resource type are then displayed. Some properties are required and others are optional. Required properties are marked with an asterisk. For example, the vswitch resource type has two required properties, cidr_block and vpc_id, while the rest are optional. In contrast, all properties for the VPC resource type are optional. Therefore, you can immediately start a test.
In this demo, you will set values for two resource properties. First, you will create a VPC in the Qingdao region. Then, you will name the VPC xiaozhu_demo and start debugging. Terraform Explorer automatically creates a task. The task name is generated based on the current resource type and a timestamp. You can also modify the task name to meet your business needs, for example, by adding a prefix to xiaozhu. The section below shows the Terraform template that is automatically generated from the form based on the specified VPC name, region ID, and provider version.
After you confirm that there are no issues, click the Preview button. The system then prepares to run the `Terraform init` and `Terraform apply` commands. Two buttons are available: Preview and Preview and Execute. The Preview button generates a preview that requires manual confirmation. In contrast, the Preview and Execute button automatically runs the `Terraform apply` command after the preview is generated. In this example, we clicked the Preview button. After the preview is generated, you can review the results and proceed.
After you review the preview and confirm that the details are correct, click the execute button. The system then automatically creates the VPC. After the VPC is created, the system automatically manages its state and uses the resulting state file to display all created resources in a structured format. On the resource details page, you can then view the managed resources, the created resource types, and their corresponding properties.
For example, after a VPC is created, it is assigned a VPC ID and has attributes such as a CIDR block and a creation time. To verify that the VPC was created, you can copy its ID and log on to the VPC console in the corresponding region, such as the Qingdao region.
To make further changes, you can return to the debug task. In the Parameters section on the left, you can modify existing parameters or specify values for empty ones. For example, if you modify the VPC description, save the changes, and continue the debug process, you will see that a `description` field is added to the original template.
After you confirm that there are no issues, click Preview and Execute. This action does not require a second confirmation. Before the modification, the description is empty. The task enters the execution phase after the preview is complete. After the execution is complete, you can verify that the description has been updated.
The first demo showed how to create a VPC. In this second demo, we will create a Flask web app on an ECS instance. Because this operation uses ECS resources, you must first switch to the ECS product and select the instance resource type. We provide a sample template to simplify this multi-resource operation. When you select this template, it displays all the code required to build the complete Flask web app. The template defines several variables, such as the content for the UI and web pages, the instance name, and various resource types.
This process involves creating a VPC, a VSwitch, a security group, and an ECS instance, and opening port 22 and port 500. Port 22 is used to log on to the ECS instance and deploy your web application. Port 500 is the default access port for Flask. After confirming that there are no issues, you can start debugging. A default task name is automatically created, which you can modify. The template code is also transferred from the previous step. Make sure that the code is correct.
Click Preview. The preview shows that seven resources will be created. Upon completion, a Flask web URL is provided as an endpoint. Click Execute to proceed. The system then sequentially creates a VPC, a VSwitch, a security group, and an ECS instance, and deploys Flask on the instance.
The Flask URL displayed in the preview details is defined by an output parameter in the template. The URL is automatically generated after the instance is created by combining the public IP address of the ECS instance with port 500. Once the deployment is complete, you can copy the URL and open it in a browser to view the message "Hello, Alibaba Cloud".
Once the execution is complete, copy the URL and paste it into your browser. You will see the message "Hello Alibaba Cloud". You can modify this message by changing the parameters on the left, for example, by changing "Hello" to "hello".
Alternatively, you can make modifications in edit mode. Click the edit mode button to customize the content of the instance template. For example, you can modify the instance name by adding a 'demo' suffix. The changes are automatically synchronized with the form on the left. After you exit edit mode, you can view the diff to see the changes you made. The diff shows both the change made in the form, where 'Hi' was changed to 'Hello', and the change made in edit mode, where a suffix was added to the instance name. After you confirm that there are no issues, save your changes and debug.
Execution is complete. Copy the URL, paste it into your browser, and you can see the phrase "Hello, Alibaba Cloud". To modify the page, you can change the parameters on the left. For example, you can change "Hello" to "hello".
After the execution is complete, if you no longer need the environment, you can click the Destroy Resources button to destroy the created resources. The system generates a preview of the resources to be destroyed. You must review the preview and manually confirm the action. After you confirm, the system executes the `Terraform apply` command to destroy the resources.
Is there a button that automatically destroys resources? Yes, there is. The 'Destroy Resources' button, located in the debug history, automatically destroys resources without requiring confirmation.
This is the second example from the presentation. State management has two aspects. First, after you debug a resource, its details are displayed in a structured format. Second, you can continue to debug previous tasks. For example, you can continue debugging a previous task, such as the VPC debug task from the demo, by clicking the Continue Debugging button. This action resumes the task from the original debug session without changing its initial state. For example, you can add a suffix, such as 'demo', to the description and click Save and Debug. Your changes will then be displayed. Due to time constraints, we will not demonstrate this further.
This section describes the button used to automatically destroy resources. When you click this button, a prompt appears warning that this operation will destroy all resources in the task. Proceed with caution. If you click Confirm, the VPC is automatically destroyed. While the destruction is in progress, this guide also explains several ways to access the Explorer.
First, you can go to the official community page for the Alibaba Cloud provider. On the site, an **Explorer in alibabacloud** button is provided for each instance of every resource type. Clicking this button automatically redirects you to the Terraform Explorer page. The interface is in English by default. You can click the globe icon on the page and select Simplified Chinese to switch to the Chinese version. After you switch, the corresponding product, resource type, and sample template from the document are automatically selected. The sample code is also loaded and is identical to the code in the document. This is the first shortcut.
You can also find resources by searching for Terraform in the Alibaba Cloud Documentation Center. Click the first search result to open the Terraform Documentation Center, which lists resources that are identical to the documentation provided by the community.
Similarly, you can click a resource type. Each resource type has an Explorer in Alibaba Cloud button. When you click this button, you are automatically redirected to the Explorer page, where the corresponding resource type and instance template are automatically selected for you. This serves as another shortcut to access the Explorer.
The third shortcut is on our official website, where many guides are provided. For example, we have a Terraform Getting Started guide and several practice tutorials. In the ECS practice tutorial, for instance, a one-click run button has been added. If you click this button, you are redirected to the Terraform Explorer page. It automatically loads the corresponding best practice sample template for you. You can then use this template for quick testing. So, those are the three shortcuts that I showed you today. First, the 'Explorer in Alibaba Cloud' button on our community documentation page. Second, the button within each resource type in the resource list on our official Terraform site. Third, the various buttons in our practice tutorials and getting started guides. Now, let's go back to the Explorer page. The destroy operation that we started has completed successfully without requiring a second confirmation.
Those were the two demos for today. We encourage you to visit Terraform Explorer to test and use it further.
That is all for this episode. If you have any questions or ideas about cloud automation, you can scan the QR code at the bottom of the screen to join our DingTalk group and chat with us. We look forward to hearing from you. See you next time.