0% found this document useful (0 votes)
5 views3 pages

Terraform

This document provides a quick overview of using Terraform with AWS, detailing the basic workflow, important files, and AWS basics for Terraform. It outlines commands for initializing, planning, applying, and destroying infrastructure, as well as the purpose of key files like main.tf and terraform.tfstate. Additionally, it includes tips, common errors, and best practices for managing Terraform configurations and AWS credentials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Terraform

This document provides a quick overview of using Terraform with AWS, detailing the basic workflow, important files, and AWS basics for Terraform. It outlines commands for initializing, planning, applying, and destroying infrastructure, as well as the purpose of key files like main.tf and terraform.tfstate. Additionally, it includes tips, common errors, and best practices for managing Terraform configurations and AWS credentials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Terraform + AWS Quick Notes

1️⃣ Basic Terraform Workflow

Step Command What Happens?

✅ Downloads provider plugins (e.g., AWS), sets up backend. No


1️⃣ Initialize Terraform terraform init
resources created.

2️⃣ View Execution Plan terraform plan ✅ Shows what changes Terraform will make. No changes yet.

3️⃣ Apply Changes


terraform apply ✅ Creates resources and generates terraform.tfstate.
(Create Infra)

4️⃣ Destroy Infra terraform


❌ Removes all resources Terraform created.
(Optional) destroy

2️⃣ Terraform Important Files

File Purpose

main.tf Defines infrastructure (EC2, VPC, S3, etc.).

variables.tf Stores variables (like AMI ID, instance type).

outputs.tf Defines outputs (e.g., EC2 instance IP after creation).

terraform.tfstate Tracks current infra state (auto-generated after apply).

.terraform/ Stores provider plugins and settings (created after init).

📌 Remember: terraform.tfstate is NOT created during terraform init. It appears only after terraform
apply.

3️⃣ AWS Basics for Terraform

🟢 AWS IAM (Identity and Access Management)

 IAM is used to manage permissions and security in AWS.

 You can create IAM Users & Roles.

 To use Terraform, create an IAM User with Programmatic Access (Access Key & Secret Key).

🔑Setting Up AWS Credentials

1️⃣ Login to AWS as IAM User → Create Access Key. 2️⃣ Configure AWS CLI in VS Code:
aws configure

 Enter Access Key & Secret Key.

 Choose AWS Region (e.g., us-east-1).

💿 Finding a Valid AMI ID (For EC2 Instances)

 Run this command in AWS CLI to get the latest Amazon Linux 2 AMI:

 aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-*-


x86_64-gp2" --query "Images | sort_by(@, &CreationDate) [-1].ImageId"

4️⃣ Terraform Providers & Resources

📌 What is a Provider?

 A provider is what Terraform uses to interact with cloud services (AWS, Azure, GCP, etc.).

 Example AWS Provider in main.tf:

 provider "aws" {

 region = "us-east-1"

 }

📌 What is a Resource?

 A resource is an actual infrastructure component (EC2, S3, VPC, etc.).

 Example EC2 Instance:

 resource "aws_instance" "my_instance" {

 ami = "ami-XXXXXXXXXX" # Replace with valid AMI ID

 instance_type = "t2.micro"

 }

5️⃣ Terraform State File (terraform.tfstate)

Action What Happens?

First terraform apply Creates terraform.tfstate.

Second terraform plan Compares main.tf with terraform.tfstate.

Manually Deleting Terraform will think the resources don’t exist and recreate them. ⚠️
terraform.tfstate Avoid deleting manually!
6️⃣ Terraform Tips & Tricks

📝 Easy Mapping:

 Init → Set up Terraform.

 Plan → Check what will change.

 Apply → Create infra.

 Destroy → Delete infra.

⚠️Common Errors & Fixes:

 InvalidAMIID.NotFound: AMI is not available in your region. Find a new AMI.

 403 InvalidClientTokenId: Wrong AWS credentials. Run aws configure again.

✅ Best Practices:

 Always use version control (Git) for .tf files.

 Don’t edit terraform.tfstate manually!

 Use terraform destroy before changing regions (to avoid orphaned resources).

provider "aws" {

region = "us-east-1"

resource "aws_instance" "example" {

ami = "ami-XXXXXXXXXXXXXXX" # Correct AMI ID for your region

instance_type = "t2.micro"

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy