Day 2 - Terraweek! A road to terraform mastery
Introduction
Terraform is a flexible and reliable solution for managing resources across many cloud platforms in the infrastructure as code (IaC) space. Terraform's adaptability goes beyond the cloud, even though managing cloud resources is its main use case. In this post, we'll look at a real-world application where Terraform excels at handling local files by utilizing HCL variables, data types, and expressions.
Defining the Objective
Consider a scenario where you need to generate a local file with specific content. Instead of manually creating and updating files, we can harness Terraform to automate this task. Let's dive into the steps involved.
Variables in Terraform
The use of variables is a key component of Terraform. We may parameterize our configurations using these variables, which makes them more adaptable and reusable. In our use case, we create a variables.tf file with the name "file_content" as a variable:
variable "file_content" {
description = "Content to write to the local file"
type = string
default = "Default content if not provided"
}
The string value of this variable, "file_content," includes a default value in the event that it is not explicitly specified during configuration.
Creating the Local File Resource
We now go to the core of our operation, where we use Terraform's "local_file" resource to build a local file. The configuration is shown below from our main.tf file:
resource "local_file" "example" {
filename = "example.txt"
content = var.file_content
}
In this main.tf file, we're using the local_file resource to create a local file named "example.txt." We set the content attribute to the value of the file_content variable using var.file_content.
How do I run this configuration?
To use these configuration files, you'll need to initialize and apply the Terraform configuration. Here are the steps:
Step 1: Initialize Terraform in your working directory:
terraform init
Step 2: If you need a preview of what happens when you run the terraform file, you can run:
terraform plan
Otherwise, skip Step 2 and continue to Step 3. It is always recommended to check for changes before applying the configuration.
Step 3: Apply the configuration to create the local file:
terraform apply
Terraform will create a file named "example.txt" in the same directory with the content specified in the file_content variable. You can customize the content by changing the value of the file_content variable or using different filenames as needed.
Writing a quick configuration for deploying an S3 bucket using AWS provider
Let's write a simple Terraform configuration using HashiCorp Configuration Language (HCL) to create an AWS S3 bucket. We'll also add the required AWS provider configuration and test it using the Terraform CLI. Make sure you have the AWS CLI configured with the necessary credentials before proceeding.
Create a main.tf file with the following content to define the AWS provider and an S3 bucket resource:
In this main.tf file:
We specify the AWS provider configuration with the provider block, setting the AWS region to "us-east-1." You should replace this with your desired AWS region. We define an S3 bucket resource using the aws_s3_bucket resource block, giving it the name "my-example-bucket" and setting its ACL to "private." I have named the bucket as "terraweek_project_S3_bucket"
I created a separate file called providers.tf so that I can add all the necessary providers and run them at once.
Create a version.tf file to specify the required Terraform version and AWS provider version:
In this version.tf file:
We specify the minimum required Terraform version (0.15 or higher). We declare the required AWS provider version, specifying that it should be at least version 3.0.
Usage:
Now, you can use the Terraform CLI to initialize and apply the configuration:
Initialize Terraform in your working directory:
terraform init
Check what happens if you apply this configuration using:
terraform plan
Apply the configuration to create the AWS S3 bucket:
terraform apply
Terraform will prompt you to confirm the resource creation. Type "yes" to proceed.
After Terraform successfully applies the configuration, it will display the output showing the resource creation status.
You can also use terraform destroy to destroy the resources created by this configuration when you no longer need them.
Remember to replace the AWS region and bucket name with your desired values. This example demonstrates the basics of creating an AWS resource using Terraform and specifying the required AWS provider version. You can extend and customize this configuration as needed for your specific use case.
That's all for day 2 folks! Happy Learning!
#TWS #Terraweek_day2