☑️Day 69: Terraform for AWS S3 and Meta Arguments🚀
🔹Table of Contents :
Introduction
Task 1: Creating an AWS S3 Bucket
Task 2: Understanding Meta Arguments
Examples and Real-Time Scenarios
Commands Reference
Real-Time Use Cases
Conclusion
✅What I Learned Today
Today, I focused on learning how to create an AWS S3 bucket using Terraform and explored meta arguments like count
, for_each
, depends_on
, and lifecycle
. These powerful features make Terraform even more versatile in managing cloud infrastructure.
✅Task 1: Creating an AWS S3 Bucket
Objective: Create an S3 bucket with tags using Terraform.
✅Step-by-Step Commands
Navigate to Your Project Directory:
mkdir terraform-s3 && cd terraform-s3
Create
main.tf
:terraform { required_providers { aws = { source = "hashicorp/aws" version = ">= 4.0.0" } } } provider "aws" { region = "us-east-1" } resource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name-123456" # Use a globally unique name tags = { Name = "MyS3Bucket" Environment = "Dev" } }
Initialize Terraform:
terraform init
Plan Infrastructure:
terraform plan
Apply Configuration:
terraform apply
- Confirm by typing
yes
when prompted.
- Confirm by typing
Verify Bucket Creation:
aws s3 ls
Destroy Infrastructure:
terraform destroy
- Confirm by typing
yes
.
- Confirm by typing
✅Task 2: Understanding Meta Arguments
Meta arguments enhance resource behavior in Terraform. Here's a breakdown of the key meta arguments:
1. count
Purpose: Create multiple instances of a resource based on a number.
Example: Launch multiple EC2 instances.
resource "aws_instance" "web_server" { count = 3 ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" tags = { Name = "WebServer-${count.index}" } }
Commands:
terraform init terraform apply
2. for_each
Purpose: Create resources for each item in a list or map.
Example: Create instances with specific names.
resource "aws_instance" "web_server" { for_each = { app1 = "ami-0c55b159cbfafe1f0", app2 = "ami-04505e74c0741db8d" } ami = each.value instance_type = "t2.micro" tags = { Name = each.key } }
Commands:
terraform init terraform apply
3. depends_on
Purpose: Ensure resource creation order.
Example: Ensure an S3 bucket is created before an EC2 instance.
resource "aws_s3_bucket" "my_bucket" { bucket = "dependency-example-bucket" } resource "aws_instance" "web_server" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" depends_on = [aws_s3_bucket.my_bucket] }
Commands:
terraform init terraform apply
4. lifecycle
Purpose: Customize the behavior of resource creation, update, or deletion.
Example: Prevent S3 bucket deletion.
resource "aws_s3_bucket" "my_bucket" { bucket = "lifecycle-example-bucket" lifecycle { prevent_destroy = true } }
Commands:
terraform init terraform apply terraform destroy # This will fail due to `prevent_destroy`
✅Real-Time Scenarios
Using
count
:- Deploy multiple identical resources, such as EC2 instances for scaling applications.
Using
for_each
:- Assign different roles or configurations to multiple resources dynamically.
Using
depends_on
:- Ensure database creation before connecting a web application to it.
Using
lifecycle
:- Protect critical infrastructure like S3 buckets from accidental deletion.
✅Conclusion
Today's learning focused on creating AWS S3 buckets and understanding meta arguments in Terraform. Meta arguments like count
, for_each
, depends_on
, and lifecycle
provide greater control and flexibility in managing infrastructure as code. This is crucial for building scalable, reliable, and production-ready environments in DevOps.
Stay tuned for more hands-on tasks and in-depth learning!🚀
🚀Thanks for joining me on Day 69! Let’s keep learning and growing together!
Happy Learning! 😊
#90DaysOfDevOps
Subscribe to my newsletter
Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by