Chapter 41: Terraform Environments and Jenkins Plan Approval Apply Pipeline
Chala mitrano, aaj Terraform la Jenkins madhe. dev, stage, prod — pratyekacha vegla folder aani vegla state. Jenkins scan karto, plan banavto, manus approve karto, aani mag apply. Laptop varun prod la apply nahi. Key Jenkins madhe nahi, role vaparto.
What you will learn in this chapter
- Separate dev, stage and prod with one shared module
- A Jenkinsfile with validate, Checkov and tfsec scans, plan, approval and apply
- Scan the plan JSON as well as the code
- Read and fix scanner findings, or skip with a reason
Concepts in this chapter
- 41.1Environments — dev, stage and prod
- 41.2Jenkins pipeline — scan, plan, approval, apply
- 41.3Reading Checkov and tfsec results
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Flipkart, the e-commerce company, working from Bengaluru. Karthik Reddy leads the team, and we look after the cart checkout service. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Flipkart is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy Input variable
Why: Variables make one module usable for many environments.
What to do: Write a variable "instance_type" with type = string and default = "t3.micro".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Easy Name the practice
Why: IaC is the base of repeatable cloud setups.
What to do: Flipkart describes its servers, networks and databases in text files kept in Git and creates them with a tool. What is this called (three-letter short form)?
- Write down the facts or numbers the task gives you.
- Work it out step by step.
- Type only the final answer (one word or number) in the box.
- Click Check answer.
Easy S3 bucket resource
Why: Buckets in code are reviewed and never forgotten.
What to do: Write a resource aws_s3_bucket named logs with bucket = "flipkart-logs" and tags { Team = "devops" }.
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Medium Plan with a variable value
Why: The same code builds dev and prod with different values.
What to do: Run a plan with the variable region set to ap-south-1 and using the file prod.tfvars.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Create a workspace
Why: Workspaces keep separate state for each environment.
What to do: Create and switch to a new workspace named staging.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Save a plan file
Why: Applying a saved plan applies exactly what was reviewed.
What to do: Create a plan and save it to the file flipkart.tfplan.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium EC2 instance using a variable
Why: Referencing var.* avoids hard-coding sizes.
What to do: Write resource aws_instance "web" with ami = "ami-0abcdef1234567890", instance_type = var.instance_type and tags Name = "cart-checkout".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Hard Plan one resource only
Why: During an incident you may need to touch only one resource.
What to do: Plan only the resource aws_instance.web.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Hard Manual approval before production
Why: A human sign-off stops an accidental production release.
What to do: Declarative pipeline, agent any, stage Prod with input message 'Deploy to production?' and steps sh './deploy.sh prod'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Hard Remote state in S3
Why: Shared remote state with locking stops two people breaking each other.
What to do: Write a terraform block with backend "s3": bucket = "flipkart-tfstate", key = "prod/terraform.tfstate", region = "ap-south-1", dynamodb_table = "tf-lock".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Hard Create several copies with count
Why: count builds N identical resources from one block.
What to do: Write resource aws_instance "worker" with count = 3, ami = "ami-0abcdef1234567890", instance_type = "t3.small" and tags Name = "worker-${count.index}".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Hard Call a module
Why: Modules package a tested pattern, like a VPC, for reuse.
What to do: Write module "vpc" with source = "terraform-aws-modules/vpc/aws", version = "5.8.1", name = "flipkart-vpc" and cidr = "10.0.0.0/16".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Ravi Sir's Cards: 9 revision, interview and comparison cards for this chapter · मराठी cards