Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
๐ Declarative Syntax in Infrastructure as Code (IaC): Defining What You Want, Not How
Imagine walking into a restaurant. Instead of explaining to the chef step by step how to prepare your meal, you simply say: โIโd like a grilled salmon with lemon butter and mashed potatoes.โ
The chef takes care of how itโs cooked.
This is exactly how Declarative Syntax works in Infrastructure as Code (IaC). You declare what infrastructure you want, and the IaC tool figures out the details of how to build it.
This article will cover: โ What Declarative Syntax is. โ How it differs from Imperative Syntax. โ Example programs (Terraform, CloudFormation, Kubernetes). โ Tips for remembering this concept for interviews and exams. โ Why learning Declarative Syntax is essential in todayโs cloud world.
By the end, youโll not only understand Declarative Syntax but also feel confident explaining it in real-world scenarios.
๐ What is Declarative Syntax?
Declarative Syntax is a programming approach where you specify the desired end state of infrastructure, not the detailed steps to achieve it.
- You say what you need (e.g., โI want 2 servers, a load balancer, and a databaseโ).
- The tool (Terraform, Kubernetes, or CloudFormation) takes care of the how (e.g., order of resource creation, dependencies, and configurations).
๐ Declarative IaC scripts are often written in YAML, JSON, or HCL (HashiCorp Configuration Language).
๐ Declarative vs Imperative Syntax
Aspect | Declarative | Imperative |
---|---|---|
Focus | What the end result should look like | How to reach the result step by step |
Example | โI want a Kubernetes cluster with 3 nodes.โ | โFirst create a VM, then install Kubernetes, then join nodes.โ |
Idempotency | Yes (running script multiple times = same result) | No (re-running may break or duplicate) |
Ease of Use | Easier to scale and maintain | Harder to manage in large environments |
Tools | Terraform, CloudFormation, Kubernetes, ARM | Ansible, Puppet, custom shell scripts |
๐ Most modern IaC tools prefer declarative syntax because itโs predictable, repeatable, and easier for teams.
๐ Declarative Syntax Examples (Programs)
Letโs see 3 practical examples of Declarative Syntax in action.
โ Example 1: Terraform โ Deploy an AWS EC2 Instance
provider "aws" { region = "us-east-1"}
resource "aws_instance" "my_vm" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro"
tags = { Name = "Declarative-EC2" }}
๐ Here, we declare:
- AWS region
- Instance type
- AMI (Amazon Machine Image)
- Tag name
Terraform automatically decides how to create and configure the instance.
โ Example 2: AWS CloudFormation โ Create an S3 Bucket
AWSTemplateFormatVersion: "2010-09-09"Resources: MyS3Bucket: Type: AWS::S3::Bucket Properties: BucketName: my-declarative-bucket
๐ Here, we declare:
- The type of resource (S3 Bucket).
- The name of the bucket.
CloudFormation automatically handles how to provision and configure the bucket.
โ Example 3: Kubernetes โ Deploy a Nginx Pod
apiVersion: v1kind: Podmetadata: name: nginx-podspec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80
๐ Here, we declare:
- A pod called
nginx-pod
. - The container image (
nginx:latest
). - Port mapping.
Kubernetes ensures the pod runs as declared, even if the node fails (self-healing).
๐ Real-World Use Cases of Declarative Syntax
-
Multi-Cloud Deployments
- Easily replicate infrastructure across AWS, Azure, and GCP.
- Example: Terraform modules managing multiple regions.
-
Kubernetes Orchestration
- Declarative YAML ensures consistent deployments.
- Example: Scaling from 3 to 10 pods is just a single line change.
-
Automated Disaster Recovery
- Store declarative configs in Git.
- In case of outages, reapply configs to recreate infrastructure.
๐ฏ Why is Declarative Syntax Important?
- Simplicity โ Focus on outcomes, not steps.
- Idempotency โ Running code multiple times gives the same result.
- Scalability โ Easy to scale resources (change a number in code).
- Version Control โ Store declarative files in Git for collaboration.
- Consistency โ Ensures identical environments for Dev, Test, and Prod.
- Future-Proof โ Most modern DevOps tools adopt declarative models.
๐ง How to Remember Declarative Syntax (Interview & Exam Tips)
Hereโs a simple memory technique (mnemonic):
D.E.C.L.A.R.E
- D โ Define the end state
- E โ Eliminate manual steps
- C โ Consistency across environments
- L โ Less complexity
- A โ Automation at scale
- R โ Repeatable results
- E โ Easier collaboration
๐ Use flashcards with:
- Tool name โ Example declarative snippet.
- Question: Declarative vs Imperative differences.
๐ Interview Example Answer: โDeclarative syntax in IaC focuses on describing the desired infrastructure state, leaving the tool to figure out the steps. For example, in Terraform, I can declare an EC2 instance, and Terraform automatically handles dependencies like networking or storage.โ
๐ Best Practices for Declarative Syntax
- Keep infrastructure code modular (use Terraform modules, Kubernetes Helm charts).
- Store declarative configs in Git repositories.
- Apply linting and validation tools (e.g.,
terraform validate
,kubeval
). - Implement GitOps workflows for automated deployments.
- Test changes in staging before production.
๐ฎ Future of Declarative Syntax
- GitOps Evolution โ Declarative + Git for full automation.
- Policy-as-Code โ Security and compliance declared alongside infra.
- AI-powered IaC โ AI suggesting or fixing declarative configs.
- Multi-Cloud Portability โ Single declarative syntax across all providers.
๐ Summary
- Declarative Syntax defines what infrastructure should look like, not how to build it.
- Examples include Terraform HCL, CloudFormation YAML, and Kubernetes YAML.
- Benefits: simplicity, idempotency, scalability, consistency.
- Remember with D.E.C.L.A.R.E mnemonic.
- Declarative Syntax is a must-have DevOps skill for cloud engineers, developers, and system admins.
โ By mastering Declarative Syntax, you unlock the ability to manage infrastructure reliably, consistently, and at scale. Itโs the foundation of Terraform, Kubernetes, and GitOps, making it one of the most valuable skills for cloud careers.