•
So, you have decided to offload all your application’s infrastructure needs to the cloud. Great choice! A way better option than clicking around forever to get things deployed.
The next step is managing and provisioning that infrastructure efficiently, which is where comes in. IaC allows you to automate the creation, configuration, and management of your cloud resources using code. This ensures consistency, repeatability, and scalability, making infrastructure management more reliable and less error-prone.
With IaC, you can describe your cloud infrastructure in human-readable configuration files. These can be versioned, shared, and reused just like application code. Whether you're deploying a simple app or a complex architecture, IaC helps you define, deploy, and manage it all with ease.
is the most popular IaC tool and a great choice for automating cloud infrastructure.
Learn Terraform only if you're already familiar with manually creating resources on AWS, Azure, or other cloud platforms. This manual knowledge helps when debugging issues.
Terraform allows you to manage infrastructure across multiple cloud providers using a unified syntax. It’s declarative, scalable, and maintainable.
We’ll explore Terraform fundamentals and build a simple setup to deploy an S3 bucket and an EC2 instance.
To install Terraform via Chocolatey on Windows:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Then install Terraform:
choco install terraform
Verify the installation with:
terraform --version
We’ll cover:
Create a file
providers.tf
:
terraform { required_version = "~> 1.9.5" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.64.0" } } } provider "aws" { region = "us-east-1" default_tags { tags = { Environment = "staging" Owner = "Mukesh Murugan" Project = "codewithmukesh" } } }
Create a file
buckets.tf
:
resource "aws_s3_bucket" "codewithmukesh" { bucket = "codewithmukesh-bucket" }
terraform init
– Initialize directory and download providersterraform plan
– Preview changesterraform apply
– Apply changesterraform destroy
– Destroy infrastructureterraform show
, terraform state
, terraform validate
, etc.provider "aws" { region = "us-west-2" access_key = "my-access-key" secret_key = "my-secret-key" }
provider "aws" { region = "us-west-2" profile = "mukesh" }
:
variable "region" { description = "The AWS region to deploy resources in" type = string default = "us-east-1" }
Use in
providers.tf
:
provider "aws" { region = var.region ... }
:
output "bucket_name" { description = "The name of the S3 bucket" value = aws_s3_bucket.my_bucket.id }
terraform { backend "s3" { bucket = "cwm-tfstates" key = "demo/beginners-guide/terraform.tfstate" region = "us-east-1" dynamodb_table = "cwm-state-locks" encrypt = true } }
:
resource "aws_instance" "demo" { ami = "ami-066784287e358dad1" instance_type = "t3.micro" }
To clean up resources and avoid costs, run:
terraform destroy
Next, we’ll explore:
0 comments
Discover the latest insights and trends from our blog.
Sustainable living involves making intentional choices to preserve natural resources, reduce pollution, and support long-term ecological balance through practices like ...
Poor software architecture leads to 5deep, hard-to-fix bugs. This article explores causes, examples, and best practices to build scalable, maintainable, bug-resistant systems. ...
Explore key software design principles like SOLID, DRY, KISS, and design patterns to build clean, scalable, and maintainable applications. Perfect for modern developers. ...