Get startedGet started for free

Using Terraform

1. Using Terraform

So, how can you use Terraform to create, update, or destroy infrastructure resources? First, you must write your infrastructure as code in configuration files. Configuration files describe to Terraform the resources you want to provision. Terraform generates an execution plan for the desired state, and then executes the plan to build the described infrastructure. As the configuration changes, Terraform can determine what changed and create incremental execution plans that can be applied. Terraform is available in three editions: Community Edition, Cloud, and Enterprise. Community Edition is a free version of the software available for download on a local machine or compute resource in the cloud. With this version, many features are available with no license cost. You can provision several different types of resources and codify your infrastructure. Community Edition grants access to publicly available templates so you can apply code writing best practices. You can only use the Community Edition version on your local machine, and it does not support concurrent deployments. Terraform Community Edition does not have version control, so you can't track changes or ensure that your commits do not affect the infrastructure setup. With the Community Edition version, Terraform can only be interacted with through the CLI, whereas Terraform Cloud and Enterprise come with a GUI option. Terraform Cloud is useful for collaborative environments. Terraform Cloud has three plans: free, standard and plus. Unlike Terraform Community Edition, Terraform Cloud and Terraform Enterprise support concurrent deployment. With Terraform Cloud – like most other SaaS solutions – the operational overhead is low. Terraform Enterprise is hosted on-premises or on an infrastructure controlled by you. Due to infrastructure maintenance and its integration with Terraform, Terraform Enterprise has a high operational overhead. Terraform is pre-installed on Cloud Shell, but you can also install it on your local machine. You can install Terraform as a binary package or use popular package managers. To manually install Terraform on a Windows machine: First, download the package that meets your system requirements. Extract the package. Terraform includes a single binary called terraform. Edit the PATH variable to include Terraform. And then verify the installation by entering terraform -help in a new terminal. The authentication mechanism used for Google Cloud varies based on where Terraform is run. The Google Cloud provider – not Terraform – will require authentication to communicate to the resource API for resource creation. Terraform on Cloud Shell is pre-authenticated. Cloud Shell is a Compute Engine virtual machine, and the service credentials are automatic, so there’s no need to set up a service account key. If you’re running Terraform on your workstation, authenticate by using the Cloud SDK. You first must install gcloud CLI, and then run the gcloud auth application-default login command to authenticate with Google Cloud. If you’re running Terraform in a VM on Google Cloud, configure that VM to use a Google Service Account. A Google Service Account allows Terraform to authenticate without a separate credential or authentication file. Ensure that the VM has the Google Cloud API enabled. If you’re running Terraform outside Google Cloud, you can use a Google Cloud Service Account with Terraform. From the service account key page in the Cloud Console, choose an existing account or create a new one. Then download the JSON key file. Name it something you can remember, and store it somewhere secure on your machine. Supply the key to Terraform using the environment variable GOOGLE_APPLICATION_CREDENTIALS, setting the value to the location of the file. Terraform can then use this key for authentication. This method does have a few limitations. Service account keys are short-lived. Key rotation is not allowed, and the keys must be protected. Workload identity and workload identity federation are tools for mitigating short-lived identity tokens when running Terraform outside of Google Cloud. Once you’ve downloaded Terraform and authenticated successfully, you’re ready to start authoring a Terraform configuration. Let’s create a simple VPC network. We won’t get into the details of the code just yet. For now, let’s focus on the workflow. First, create a configuration file with a . tf extension. and define Google Cloud as the provider. Then add HCL code to create a Google Compute instance. After you save the code, navigate to the directory where you saved the file and run terraform init, terraform plan and terraform apply. After you run terraform apply, a new VPC network named mynetwork will be created on Google Cloud. Once you’ve downloaded Terraform and authenticated successfully, you’re ready to start authoring a Terraform configuration. Let’s create a simple VPC network. We won’t get into the details of the code just yet. For now, let’s focus on the workflow. First, create a configuration file with a . tf extension. Then, define Google Cloud as the provider and run the terraform init command to initialize the provider. Add the HCL code to create a Google Compute instance. After you save the code, navigate to the directory where you saved the file and run the terraform plan and terraform apply. After you run terraform apply, a new VPC network named mynetwork will be created on Google Cloud.

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.