Skip to content

Commit bd6e898

Browse files
Update README.md
1 parent c9b377c commit bd6e898

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,170 @@
11
# terraform-for-devops
22
This repository is your one stop solution for Terraform for DevOps Engineers
3+
4+
# Terraform Commands - Complete Guide
5+
6+
## **1. Setup & Initialization**
7+
### **Install Terraform**
8+
```sh
9+
# Linux & macOS
10+
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
11+
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
12+
sudo apt-get update && sudo apt-get install terraform
13+
14+
# Verify Installation
15+
terraform -v
16+
```
17+
18+
### **Initialize Terraform**
19+
```sh
20+
terraform init
21+
```
22+
- Downloads provider plugins
23+
- Sets up the working directory
24+
25+
## **2. Terraform Core Commands**
26+
### **Format & Validate Code**
27+
```sh
28+
terraform fmt # Formats Terraform code
29+
terraform validate # Validates Terraform syntax
30+
```
31+
32+
### **Plan & Apply Infrastructure**
33+
```sh
34+
terraform plan # Shows execution plan without applying
35+
terraform apply # Creates/updates infrastructure
36+
terraform apply -auto-approve # Applies without manual confirmation
37+
```
38+
39+
### **Destroy Infrastructure**
40+
```sh
41+
terraform destroy # Destroys all managed resources
42+
terraform destroy -auto-approve # Without confirmation
43+
```
44+
45+
## **3. Managing Terraform State**
46+
### **Check Current State**
47+
```sh
48+
terraform state list # Lists all managed resources
49+
terraform show # Shows detailed resource info
50+
```
51+
52+
### **Manually Modify State**
53+
```sh
54+
terraform state mv <source> <destination> # Move resource in state file
55+
terraform state rm <resource> # Removes resource from state (not from infra)
56+
```
57+
58+
### **Remote Backend (S3 & DynamoDB)**
59+
```hcl
60+
terraform {
61+
backend "s3" {
62+
bucket = "my-terraform-state"
63+
key = "global/s3/terraform.tfstate"
64+
region = "us-east-1"
65+
dynamodb_table = "terraform-lock"
66+
encrypt = true
67+
}
68+
}
69+
```
70+
```sh
71+
terraform init # Reinitialize with remote backend
72+
```
73+
74+
## **4. Variables & Outputs**
75+
### **Define & Use Variables**
76+
```hcl
77+
variable "instance_type" {
78+
default = "t2.micro"
79+
}
80+
resource "aws_instance" "web" {
81+
instance_type = var.instance_type
82+
}
83+
```
84+
85+
### **Pass Variables in CLI**
86+
```sh
87+
terraform apply -var="instance_type=t3.small"
88+
```
89+
90+
### **Output Values**
91+
```hcl
92+
output "instance_ip" {
93+
value = aws_instance.web.public_ip
94+
}
95+
```
96+
```sh
97+
terraform output instance_ip
98+
```
99+
100+
## **5. Loops & Conditionals**
101+
### **for_each Example**
102+
```hcl
103+
resource "aws_s3_bucket" "example" {
104+
for_each = toset(["bucket1", "bucket2", "bucket3"])
105+
bucket = each.key
106+
}
107+
```
108+
109+
### **Conditional Expressions**
110+
```hcl
111+
variable "env" {}
112+
resource "aws_instance" "example" {
113+
instance_type = var.env == "prod" ? "t3.large" : "t2.micro"
114+
}
115+
```
116+
117+
## **6. Terraform Modules**
118+
### **Create & Use a Module**
119+
```sh
120+
mkdir -p modules/vpc
121+
```
122+
```hcl
123+
# modules/vpc/main.tf
124+
resource "aws_vpc" "main" {
125+
cidr_block = "10.0.0.0/16"
126+
}
127+
```
128+
```hcl
129+
# Root module
130+
module "vpc" {
131+
source = "./modules/vpc"
132+
}
133+
```
134+
```sh
135+
terraform init
136+
terraform apply
137+
```
138+
139+
## **7. Workspaces (Environment Management)**
140+
### **Create & Switch Workspaces**
141+
```sh
142+
terraform workspace new dev
143+
terraform workspace new prod
144+
terraform workspace select prod
145+
terraform workspace list
146+
```
147+
148+
## **8. Terraform Debugging & Logs**
149+
```sh
150+
export TF_LOG=DEBUG # Enable debug logs
151+
terraform apply 2>&1 | tee debug.log # Save logs
152+
```
153+
154+
---
155+
156+
## Projects
157+
158+
### Terraform with Ansible
159+
[Get it here](https://github.com/LondheShubham153/terraform-ansible-multi-env)
160+
161+
### Terraform with GitHub
162+
[Get it here](https://github.com/Amitabh-DevOps/online_shop/tree/github-action/.github/workflows)
163+
164+
### Terraform to EKS
165+
[Get it here](https://github.com/DevMadhup/Springboot-BankApp/tree/DevOps/Terraform/EKS-Deployment)
166+
167+
## **Final Thoughts**
168+
This README covers all the Terraform commands needed for your **"Terraform in One Shot"** video. Let me know if you need modifications or extra details! 🚀
169+
170+

0 commit comments

Comments
 (0)