Courses / Terraform
1. Terraform Configuration Files
Learn how to define infrastructure using HCL in Terraform configuration files.
How It Works
Think of HCL as a blueprint for your infrastructure, where each resource is a building block that Terraform assembles into a complete structure.
You define resources in a `.tf` file using HCL syntax.
Terraform reads the configuration and determines what needs to be created or modified.
Terraform applies the changes to reach the desired state.
You can version control these files to track changes over time.
See It
The code above creates a Terraform configuration file named `main.tf` that specifies an AWS provider and an S3 bucket resource. You can try changing the bucket name to see how it affects the configuration.
cat <<EOF > main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name-12345"
}
EOFUse It
This concept is used when setting up cloud infrastructure for a web application.
cat <<EOF > app.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_ec2_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
EOFUsing HCL for infrastructure as code allows for consistent and repeatable deployments across environments.
Break It
What will this apply command create?
cat <<EOF > main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
acl = "private"
}
EOFThe `acl` setting specifies that the bucket will be private, which is the default behavior unless otherwise specified.
To create a public S3 bucket, change the `acl` to `"public-read"`.
cat <<EOF > main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
acl = "public-read"
}
EOFLevel Up
Terraform Modules: Modules allow you to encapsulate and reuse configurations, making your infrastructure more modular and maintainable.
Terraform Workspaces: Workspaces enable you to manage different environments (like dev, staging, production) within the same configuration.
Prove It
Create a Terraform configuration file that sets up an AWS RDS instance.
HCL
Terraform Configuration Files
Terraform configuration files allow you to define and manage infrastructure as code.
"I want to automate my cloud setup without manual configurations."
Terraform configuration files written in HCL (HashiCorp Configuration Language) describe the desired state of your infrastructure, enabling reproducibility and automation.
"I want to automate my cloud setup without manual configurations."
Terraform configuration files written in HCL (HashiCorp Configuration Language) describe the desired state of your infrastructure, enabling reproducibility and automation.