Terraform
AdvanceTopics
TerraformCloudEnterprise
Info

Terraform AWS Documentation

Documentation Referred

ec2.tf File

provider "aws" {
  region = "us-west-2"
}
 
resource "aws_instance" "myec2" {
  ami           = "ami-082b5a644766e0e6f"
  instance_type = "t2.micro"
}

Environment Variables to Add in Terraform Cloud

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY

sleep.tf File

resource "time_sleep" "wait_30_seconds" {
  create_duration = "30s"
}

Documentation Referred

remote-backend.tf

terraform {
  cloud {
    organization = "mykplabs-org"
 
    workspaces {
      name = "remote-operation"
    }
  }
}

iam.tf

provider "aws" {
  region     = "us-west-2"
  access_key = "YOUR-ACCESS-KEY"
  secret_key = "YOUR-SECRET-KEY"
}
 
resource "aws_iam_user" "lb" {
  name = "loadbalancer"
  path = "/system/"
}

CLI Commands Used

terraform login
terraform init
terraform plan
terraform apply -auto-approve
terraform destroy -auto-approve

Documentation Referred

Sentinel Policy

import "tfplan"
 
main = rule {
  all tfplan.resources.aws_instance as _, instances {
    all instances as _, r {
      (length(r.applied.tags) else 0) > 0
    }
  }
}

EC2 Instance with Tags Code

provider "aws" {
  region = "us-west-2"
}
 
resource "aws_instance" "myec2" {
  ami           = "ami-082b5a644766e0e6f"
  instance_type = "t2.micro"
 
  tags = {
    Name = "HelloWorld"
  }
}

🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!