Terraform - Additional - Self Exercises
Terraform - Additional - Self Exercises
Task:
Solution:
Commands to Run:
terraform init
terraform plan
terraform apply
Objective: Explore the Terraform state by viewing and understanding the state file.
Task:
Solution:
# Configure AWS provider
provider "aws" {
region = "us-east-1"
}
# Create an S3 bucket
resource "aws_s3_bucket" "example" {
bucket = "my-unique-bucket-name-123"
acl = "private"
}
Commands to Run:
terraform init
terraform apply
terraform show
terraform state list
Task:
Solution:
# Variables
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
variable "aws_region" {
description = "AWS region"
type = string
default = "us-east-1"
}
# Provider configuration
provider "aws" {
region = var.aws_region
}
Commands to Run:
terraform init
terraform apply
Task:
Solution:
# AWS provider
provider "aws" {
region = "us-east-1"
}
# Output VPC ID
output "vpc_id" {
value = data.aws_vpc.default.id
}
Commands to Run:
terraform init
terraform apply
Solution:
# AWS provider
provider "aws" {
region = "us-east-1"
}
# Create an S3 bucket
resource "aws_s3_bucket" "example" {
bucket = "my-unique-bucket-to-delete"
acl = "private"
}
Commands to Run:
terraform init
terraform apply
terraform destroy
Objective: Practice using terraform fmt and terraform validate to ensure code quality.
Task:
Solution:
Commands to Run:
terraform fmt
terraform validate
terraform apply