Tech with Tyler
LinkedInGitHubYouTube
  • 👋Welcome!
    • whoami
    • !!! Disclaimer !!!
  • 🎓Academy
    • AWS Security Cookbook by Tyler
      • AWS Control Tower
        • Lab: Deploying AWS Control Tower via Terraform
      • AWS CloudTrail
      • AWS GuardDuty
        • Lab: Deploying AWS GuardDuty via Terraform
        • Lab: Logging GuardDuty Findings to S3
        • Lab: Adversary Simulation Detection with Stratus Red Team and GuardDuty
      • AWS Organizations
        • Lab: Deploying AWS Organizations via Terraform
      • AWS Root Account Management
        • Lab: Deploying AWS Root Account Management via Terraform
      • AWS Service Control Policies (SCPs)
        • Lab: Deploying AWS Service Control Policies (SCPs) via Terraform
      • TBD - Coming Soon!
        • [TBD] AWS Account Factory
        • [TBD] AWS Identity Center
    • My content on Cybr
      • Course - Terraform on AWS: From Zero to Cloud Infrastructure
      • Lab - Create Static AWS S3 Website with Terraform
      • Lab - Secure EC2 Access with SSM Session Manager and KMS
      • Lab - Encrypt and Decrypt Data with KMS and Data Encryption Keys
    • My content on PwnedLabs
      • Cyber Range - Electra
      • Lab - Abusing Identity Providers in AWS
      • Lab - Prowler and AWS Security Hub
      • Blog - Abusing Identity Providers in AWS
      • Blog - Building Security Guardrails with AWS Resource Control Policies
      • Blog - Defending Against the whoAMI Attack with AWS Declarative Policies
    • My content on YouTube
      • AWS Security Engineering
      • Linux in 60 Seconds!
  • ☁️Cloud Security
    • AWS Attacks and Techniques
      • Enumerate AWS Account IDs
      • Enumerate AWS IAM Users
      • Enumerate (Unauthenticated) IAM Users and Roles
      • Enumerate AWS Public Resources
      • Enumerate Secrets in AWS
      • Generate AWS Console Session
      • Generate IAM Access Keys from CloudShell
      • Password Spraying AWS IAM Users
      • Subdomain Takeovers
    • AWS Privilege Escalation
      • Identity Access Management (IAM)
      • IAM Trust Policies
      • Key Management Service (KMS)
      • Lightsail
      • OpenID Connect (OIDC)
      • S3
      • Secrets Manager
      • Security Token Service (STS)
    • AWS General Info
      • Amazon Bedrock
      • EC2
      • KMS
      • S3
      • SNS Topic
    • AWS CLI Cheat Sheet
    • Capture the Flags (CTFs)
      • Flaws.Cloud
        • Level 1
        • Level 2
        • Level 3
        • Level 4
        • Level 5
        • Level 6
      • PwnedLabs
        • Escalate Privileges by IAM Policy Rollback
        • Exploiting Weak S3 Bucket Policies
        • Leveraging S3 Bucket Versioning
        • S3 Enumeration Basics
        • Pillage Exposed RDS Instances
        • EC2 SSRF Attack
        • Hunt for Secrets in Git Repos
      • Cybr
        • Challenge - Secrets Unleashed
    • Tools
      • Tooling Index
      • dsnap
      • Pacu
      • s3-account-search
      • GoAWSConsoleSpray
      • aws_consoler
      • cloudenum
  • 📦Containers & Orchestration
    • Kubernetes
  • 👨‍💻Coding & CLI Tooling
    • CLI Tools
      • AWS CLI
      • Git
      • GitHub Copilot (CLI)
      • Homebrew
      • jq
      • ngrok
      • ssh
    • Coding and Scripting
      • Bash
      • Python
    • Terminal Customization
  • ⚙️DevSecOps
    • CI/CD
      • GitLab
    • Hashicorp Terraform
    • Hashicorp Vault
    • IAC Scanning
      • tfsec
    • Secrets Scanning
      • Trufflehog
  • 🎁Miscellaneous
    • Jenkins
  • 💻Operating Systems
    • Linux
      • APT Package Manager
      • CLI Tools Cheat Sheet
      • Man Pages
      • Services
      • Users and Groups
  • 🏗️Projects
    • Active Directory Homelab Automation
    • AWS Cloud Resume Challenge
    • Proxmox Homelab as Code
  • 📌Other
    • Useful Resources
Powered by GitBook
On this page
  • What is Terraform?
  • Installation
  • Commands
  • Example

Was this helpful?

  1. DevSecOps

Hashicorp Terraform

Get up and running with Terraform

PreviousGitLabNextHashicorp Vault

Last updated 5 months ago

Was this helpful?

Check out my full terraform course on Cybr!

What is Terraform?

  • is an open-source, declarative Infrastructure as Code language from HashiCorp.

  • are used to define the resources to build. These abstract the underlying API calls made to build, modify, and destroy resources by wrapping this into HCL syntax. In other words, you just worry about writing Terraform code without having to understand and work with the underlying APIs.

Installation

# Installing via Homebrew on MacOS
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew upgrade hashicorp/tap/terraformh

# Enabling tab completion
terraform -install-autocomplete

# Restarting shell
. ~/.zshrc    # bash is ~/.bashrc

Commands

  • Initialize the directory where Terraform files are stored: terraform init

  • Verify the Terraform syntax is correct: terraform validate

  • View the resources the code would build if run: terraform plan

  • Build the resources: terraform apply

  • Destroy the resources created with Terraform: terraform destroy

Example

  • Typically, Terraform code is defined in a main.tf file and variables found in that file can be declared in variables.tf

  • Variables are not required and you could hard code everything into main.tf if desired.

main.tf
# Create the bucket
resource "aws_s3_bucket" "s3-bucket-1" {
  bucket = var.mybucketname
}

# Enable server-side encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "s3-encryption-config" {
  bucket = aws_s3_bucket.s3-bucket-1.bucket # defines bucket name

  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = var.sse-algorithm # defines encryption type
    }
  }

  depends_on = [
    aws_s3_bucket.s3-bucket-1 # ensures bucket is created before trying to apply encryption
  ]
}

# Configure bucket policy, set to deny HTTP requests
resource "aws_s3_bucket_policy" "s3-bucket-policy" {
  bucket = aws_s3_bucket.s3-bucket-1.id

  # defines bucket policy below, SecureTransport false means it blocks HTTP access
  policy = <<POLICY
  {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::${var.mybucketname}/*",
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        }
    ]
}
POLICY
}

# Block public access to the bucket
resource "aws_s3_bucket_public_access_block" "s3-bucket-access-control" {
  bucket = aws_s3_bucket.s3-bucket-1.id

  block_public_acls       = var.s3-bucket-ac["block_public_acls"]
  block_public_policy     = var.s3-bucket-ac["block_public_policy"]
  ignore_public_acls      = var.s3-bucket-ac["ignore_public_acls"]
  restrict_public_buckets = var.s3-bucket-ac["restrict_public_buckets"]
}
variables.tf
variable "mybucketname" {
  description = "Set a unique bucket name"
  type        = string
}

variable "sse-algorithm" {
  description = "Specify the encryption type to use"
  type        = string
  default     = "AES256"
}

variable "s3-bucket-ac" {
  description = "Block public access"
  type        = map(any)
  default = {
    block_public_acls       = "true"
    block_public_policy     = "true"
    ignore_public_acls      = "true"
    restrict_public_buckets = "true"
  }
}

Here's an example of configuring an AWS S3 bucket in Terraform utilizing the .

⚙️
https://cybr.com/courses/terraform-on-aws-from-zero-to-cloud-infrastructure/
Terraform
Providers
AWS Provider