Demo - Terraform Workflow
1. Demo - Terraform Workflow
Hi, this demonstration will help you become familiar with the Terraform configuration to create a Compute Engine instance. We will show you how to create configuration files and use the Terraform CLI, to execute a few Terraform commands such as terraform init, terraform plan, terraform apply and terraform destroy. While watching this demonstration, you will discover how easy it is to use and adopt Terraform to manage your Google Cloud infrastructure. Let's get started. For demonstration purposes, we are using Terraform on Cloud Shell. Terraform comes preinstalled on Cloud Shell. With Cloud Shell, you have command line access to your cloud resources directly from your browser, so you can easily manage projects and resources without having to install any tools on your local machine. Further, command line tools suggest Terraform and other utilities are automatically authenticated so you can use them setup free. To verify the Terraform installation, enter terraform in the command line. You can check the version of Terraform running on Cloud Shell by running terraform version command. As a first step, let's define the provider. To do this, we first create a folder called infra. Right-click underneath your project to create New Folder. Give it the name of your choice. Create a new file called main.tf to author the terraform code. Before creating the resources, we define the provider so Terraform knows which provider to download. At this point, because you might not be familiar with the code and syntax, we leverage Terraform Registry. Navigate to the registry. Under Google Cloud Provider, click the Use Provider button and copy the code underneath it. In the configuration define, the Google providers source is defined as hashicorp/google, which is shortened for Terraform Registry for Google Cloud. In this example, we already have a project created. Let's edit the configuration options to include the project and assign the project ID to it. Save the configuration changes. Switch to Cloud Shell. Change to the current directory. Execute terraform init to initialize Terraform and download the provider plug-ins. Let's next define our first Google Cloud resource, a Google Compute Engine. Let's edit the project ID to our current project. Save the code and return to the Cloud Shell window. Next, execute terraform plan. Authorize when prompted. You can see a summary of Compute Engine instances created. Execute terraform apply, to apply your configuration. The output has a plus next to google_compute_instance, which means that Terraform will create this resource. The sign varies based on the configuration applied. Beneath the resource update status, the attributes that will be set are displayed. When the value says ‘known after apply’ the value cannot be determined until the resource is provisioned. Review the output of the apply command to ensure that the resource is configured as per your expectations. If anything in the output seems incorrect or unsafe, enter ‘no’ so that no changes are made to your infrastructure. The plan looks acceptable here so we type ‘yes’. While we wait for the resources to be created, we switch to the editor to see that the terraform state file is automatically created. The terraform state defines the current state of the infrastructure. Let's verify the resource created on the Google Cloud console. You can see a Terraform resource is now being created in the region defined. We can also destroy the resources created by simply executing the terraform destroy command. Let's navigate to the Cloud Shell and delete the resource we just created. The minus prefix indicates that the resource will be destroyed. As with apply, Terraform shows its execution plan and waits for approval before making any changes. Enter ‘yes’. You can see that the resources are now deleted. Let's verify the deletion on the Google Cloud console. Navigate to Compute Engine. Click VM instances. You can now see the Terraform instance is no more available in the instance list. This completes the demonstration. In this demonstration, we defined a provider to download the provider plug-ins. We created a Google Compute Engine using Terraform. We used the command terraform plan to review the resource creation. We used the command terraform apply to create infrastructure resources. We then finally executed terraform destroy to destroy the resources we just created. Thanks for watching. Hope you enjoyed it.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.