Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Operating Systems»Linux»Writing Your First Terraform Configuration: A Step-by-Step Guide

    Writing Your First Terraform Configuration: A Step-by-Step Guide

    June 13, 2025

    Time to Write Your First Terraform Code, Infra Coders!

    Hey, Infra coders! By now, you’ve got Terraform installed and set up with a cloud provider like AWS from our last article. Awesome work! Today, we’re diving into the fun part—writing your first Terraform configuration file to build something real in the cloud. Think of this as your first hands-on project, like sketching out a blueprint and watching it come to life. We’ll create an AWS S3 bucket and a simple EC2 instance (a virtual server) step by step. Don’t worry, I’ll keep it super simple and walk you through everything. Let’s get coding!

    Your first Terraform Configuraiton

    Step 1: Set Up Your Project Folder

    First, let’s get organized. Create a new folder on your computer for this project—call it something like first-terraform. Open your terminal or command prompt, navigate to this folder, and create a file named main.tf. This is where we’ll write our Terraform code.

    You can use any text editor you like—VS Code, Notepad, or even Vim. Just make sure your file ends with .tf so Terraform knows it’s a configuration file.

    Why main.tf?

    Terraform reads all .tf files in a folder, but main.tf is a common name for your main configuration. It’s like the entry point for your project.

    Step 2: Configure the AWS Provider

    Since we’re using AWS, we need to tell Terraform to connect to it. Open main.tf and add this code:

    
    provider "aws" {
      region = "us-east-1"
    }
    
    

    This tells Terraform:

    • Use AWS as the cloud provider.
    • Work in the us-east-1 region (you can change this to another region if you want).

    Make sure your AWS credentials are set up in ~/.aws/credentials (like we did in the last article). Terraform will use those to talk to AWS.

    Step 3: Create an S3 Bucket

    Let’s start with something simple—an AWS S3 bucket to store files. Add this to your main.tf file:

    
    resource "aws_s3_bucket" "my_bucket" {
      bucket = "my-unique-bucket-name-12345"
    }
    
    

    Here’s what this code does:

    • resource: Tells Terraform we’re creating something (in this case, an S3 bucket).
    • aws_s3_bucket: The type of resource we want (AWS’s S3 bucket).
    • my_bucket: A name we give this resource to refer to it in our code.
    • bucket: The actual name of the bucket in AWS (must be globally unique, so replace my-unique-bucket-name-12345 with something unique).

    Pro Tip

    S3 bucket names must be unique across all AWS users, so add random numbers or your name to avoid conflicts.

    Step 4: Add an EC2 Instance

    Now, let’s make things a bit more exciting by adding an EC2 instance—a virtual server in AWS. Add this to main.tf:

    
    resource "aws_instance" "my_server" {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = "t2.micro"
    }
    
    

    What’s happening here?

    • aws_instance: Creates an EC2 instance.
    • my_server: Our name for this resource.
    • ami: The Amazon Machine Image (AMI) ID, which defines the operating system (this one is for Amazon Linux 2 in us-east-1—check AWS for the latest AMI ID in your region).
    • instance_type: The size of the server (t2.micro is free-tier eligible).

    Note on AMIs

    AMI IDs change by region and over time. To find the latest Amazon Linux 2 AMI ID:

    1. Go to the AWS EC2 Console.
    2. Click Launch Instance.
    3. Search for Amazon Linux 2 and note the AMI ID.
    Find AMI ID of EC2 Image
    Find AMI ID of Image

    Step 5: Initialize Terraform

    Before we can run our configuration, we need to set up Terraform in our project folder. In your terminal, navigate to the first-terraform folder and run:

    
    terraform init
    
    

    This command:

    • Downloads the AWS provider plugin.
    • Sets up a .terraform folder and a lock file to track dependencies.

    You’ll see a message like Terraform has been successfully initialized! if it works.

    Step 6: Preview Your Plan

    Let’s see what Terraform is about to do. Run:

    
    terraform plan
    
    

    Terraform will show you a plan that says it will create:

    • One S3 bucket.
    • One EC2 instance.

    Check the output to make sure it looks right. This is like a dry run to avoid surprises.

    Step 7: Apply Your Configuration

    Time to make it real! Run:

    
    terraform apply
    
    

    Terraform will show the plan again and ask you to type yes to confirm. Once you do, it’ll create the S3 bucket and EC2 instance in AWS. This might take a minute or two. When it’s done, you’ll see Apply complete!.

    Head to the AWS Console:

    • Check S3 to see your bucket.
    • Check EC2 to see your running instance.

    Step 8: Clean Up to Save Costs

    Since AWS charges for EC2 instances (even free-tier ones have limits), let’s clean up to avoid surprises. Run:

    
    terraform destroy
    
    

    Type yes to confirm, and Terraform will delete the bucket and instance. Check the AWS Console to confirm they’re gone.

    Your Complete main.tf File

    For reference, here’s what your main.tf should look like:

    
    provider "aws" {
      region = "us-east-1"
    }
    
    resource "aws_s3_bucket" "my_bucket" {
      bucket = "my-unique-bucket-name-12345"
    }
    
    resource "aws_instance" "my_server" {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = "t2.micro"
    }
    
    

    Tips for Writing Great Configurations

    You’re off to a great start! Here are some tips to keep your Terraform code clean and effective:

    • Use Descriptive Names: Name resources like my_bucket or web_server so you know what they do.
    • Keep Files Organized: For small projects, one main.tf is fine. For bigger ones, split code into multiple .tf files.
    • Check AWS Free Tier: Stick to free-tier resources like t2.micro to avoid costs.
    • Save Your Code: Use Git to version your Terraform files and share with your team.

    What’s Next?

    You just wrote and ran your first Terraform configuration—how cool is that? You created an S3 bucket and an EC2 instance with a few lines of code. In the next article, we’ll explore how to manage multiple providers (like AWS and Azure together) and add more complex resources. Keep your first-terraform folder handy—we’ll build on it! See you in the next lesson, Infra coders!

    The post Writing Your First Terraform Configuration: A Step-by-Step Guide appeared first on TecAdmin.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMitel OpenScape Flaw (CVE-2025-23092): High-Severity Path Traversal Allows Admin RCE
    Next Article Welcome to the Terraform Tutorial Series, Infra Coders!

    Related Posts

    News & Updates

    A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

    July 22, 2025
    News & Updates

    “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Why top SOC teams are shifting to Network Detection and Response

    Development

    CVE-2025-46631 – Tenda RX2 Pro Telnet Access Control Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Celebrating GAAD by Committing to Universal Design: Flexibility in Use

    Development

    AI will change the trades too – and field service technicians can’t wait

    News & Updates

    Highlights

    Chinese Hackers Exploit SAP RCE Flaw CVE-2025-31324, Deploy Golang-Based SuperShell

    May 19, 2025

    A China-linked unnamed threat actor dubbed Chaya_004 has been observed exploiting a recently disclosed security…

    Dos and don’ts when sunsetting open source projects

    May 6, 2025

    Accelerate generative AI inference with NVIDIA Dynamo and Amazon EKS

    July 15, 2025

    Multiple Vulnerabilities in NETSCOUT nGeniusONE Threaten Infrastructure Visibility Platforms

    April 28, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.