Terraform 2 of 10: HashiCorp Configuration Language (HCL) Basics and Terraform Providers on AWS Cloud9

1 of 30. Visit the Terraform Registry, and click on "Browse Providers"

Here, you can find documentation for all the Terraform providers.

https://registry.terraform.io

2 of 30. You will then see all of the providers. Click on "AWS".

Using the menu on the left, you can filter down to the specific type of provider you are looking for. Do not neglect the "Utility" filter. A couple nice utilities are "Local" (manipulates local resources, such as file creation) and "Random" (generates random data -- note: not cryptographically secure random)

3 of 30. Click on "Documentation"

4 of 30. View the Example Usage documentation

You'll notice three blocks: terraform, provider and resource. These are the building blocks of a terraform configuration. Note: keep the documentation open. You will refer to it later.

5 of 30. Open your AWS Cloud9 Environment

This was previously created in prior hands-on tasks. Find a reference for creating a Cloud9 environment at the bottom of this article.

6 of 30. Create a "main.tf" file within your "hands-on-tasks-terraform" folder.

The "hands-on-tasks-terraform" folder was created in the prior tutorial in this series.

cd hands-on-tasks-terraform touch main.tf ls

7 of 30. Open the "main.tf" within the IDE and populate it with terraform, provider, and resource blocks, then save it.

This information is copied from the terraform documentation that you kept open. A certain part of this example is training wheels, to get comfortable with the three configuration blocks without having to re-invent the wheel, but another part is the fact that the documentation can update so rapidly that it's best to always consult the documentation instead of worrying about memorizing anything.

If you want a more step-by-step hands-on with Terraform straight from the horse's mouth, please check the links in the description.

Note: As much as possible, I am using us-east-1 for my tutorials (unless specifically testing a multi-region feature). Please be mindful that some things in AWS (such as Amazon Machine Image (AMI) can be region-specific, which means that if you configure a resource that cannot exist in your region, you can expect errors instead of smooth functionality.

8 of 30. In your terminal, within your "hands-on-tasks-terraform" directory, run "terraform init".

This initializes the directory for terraform

terraform init

9 of 30. Confirm consistent formatting with "terraform fmt"

This command automatically repairs any formatting issues with your terraform file. If no output appears, this means that your formatting is already correct and nothing was changed.

terraform fmt

10 of 30. Confirm syntax with "terraform validate"

This command confirms that the syntax is valid.

terraform validate

11 of 30. Check the plan with "terraform plan"

This tells you what terraform would do, if you applied the configuration. Note that if you do not specify the -out parameter, then terraform considers this a "speculative" plan, because it was not saved to a file. In cases of automation, it is common to save the plan to a file, so that you can automate running "terraform apply" operations against a known, documented plan.

terraform plan

12 of 30. Let's run "terraform apply"

This will run the terraform configuration file.

terraform apply

13 of 30. In your AWS Console, check your VPCs.

You should find a new VPC created, matching the vpc-id of the resource you created in previous step.

14 of 30. Reorganization: split main.tf into two files: terraform.tf and resources.tf and delete main.tf

The intent here is to make a large configuration more manageable, by splitting it into sub-files. Maybe not as important in this smaller example, but imagine if you have to deploy tons of resources. The terraform.tf will contain the terraform{} and provider{} blocks. The resources.tf will contain the resource{} block. The main.tf will be deleted.

ls cat terraform.tf cat resources.tf

15 of 30. Locate the "local_file" "Example Usage" in "Local" provider documentation

Try this path: Terraform registry > Providers > Utility > Local > Documentation > Resources > local_file. Scroll down to "Example Usage". Local was mentioned earlier in this tutorial.

16 of 30. Update resources.tf to create a file called Notes.txt

Refer to the "example usage" documentation for "local_file" to create the file.

resource "local_file" "notes" { content = "This is a file created with Terraform" filename = "notes.txt" }

17 of 30. Run "terraform init' again

We need to run the init again because we are using a new provider. Notice that hashicorp/local is installed.

terraform init

18 of 30. Run "terraform plan"

Terraform is planning to add the local file "notes.txt"

terraform plan

19 of 30. Run "terraform apply"

Make sure to write 'yes' to proceed.

terraform apply

20 of 30. View the contents of your "notes.txt" file

Cool. It worked.

21 of 30. Locate the "Example Usage" documentation for random_string"

Terraform Registry > Providers > Utility > Random > Documentation > Resources > random_string. Scroll down to "Example Usage"

22 of 30. Update resources.tf to create a random string.

resource "random_string" "random" { length = 10 }

23 of 30. Run "terraform init"

We need to run the init again because we are using a new provider. Notice that hashicorp/random is installed.

terraform init

24 of 30. Run "terraform plan"

Terraform is planning to create a random string.

terraform plan

25 of 30. Run "terraform apply"

Make sure to write 'yes' to proceed. You will see the random string. Imaging that you had to create a bunch of instances, you could use random to help automatically name resources.

terraform apply

26 of 30. Locate the Azure provider example usage documentation

Terraform Registery > Providers > Azure > Documentation. Scroll down to "Example Usage"

27 of 30. Add the Azure provider to the terraform.tf

azurerm = { source = "hashicorp/azurerm" version = "=3.0.0" }

28 of 30. Run terraform init, to install the Azure provider

We don't have any resources to create, so there's no point to doing an apply operation.

terraform init

29 of 30. Check your provider directories

You should see that azurerm, aws, local, and random are here, matching the providers you have installed.

ls .terraform/providers/registry.terraform.io/hashicorp

30 of 30. Remove what we created. Terraform destroy

If you confirm, you should find that the local file was removed and that the VPC is gone.

terraform destroy

Reference

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

What is Infrastructure as Code with Terraform?

Build Infrastructure

Change Infrastructure

Destroy Infrastructure

Command: plan

GitHub - hashicorp/hcl: HCL is the HashiCorp configuration language.

Comments

Popular posts from this blog

Orphaned No More: Adopting AWS Lambda

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov

Ansible is the Answer! | A Three-Part Series Demonstrating the Usefulness of Ansible to HumanGov