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

      The Ultimate Guide to Node.js Development Pricing for Enterprises

      July 29, 2025

      Stack Overflow: Developers’ trust in AI outputs is worsening year over year

      July 29, 2025

      Web Components: Working With Shadow DOM

      July 28, 2025

      Google’s new Opal tool allows users to create mini AI apps with no coding required

      July 28, 2025

      I replaced my Samsung OLED TV with this Sony Mini LED model for a week – and didn’t regret it

      July 29, 2025

      I tested the most popular robot mower on the market – and it was a $5,000 crash out

      July 29, 2025

      5 gadgets and accessories that leveled up my gaming setup (including a surprise console)

      July 29, 2025

      Why I’m patiently waiting for the Samsung Z Fold 8 next year (even though the foldable is already great)

      July 29, 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

      Performance Analysis with Laravel’s Measurement Tools

      July 29, 2025
      Recent

      Performance Analysis with Laravel’s Measurement Tools

      July 29, 2025

      Memoization and Function Caching with this PHP Package

      July 29, 2025

      Laracon US 2025 Livestream

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

      Microsoft mysteriously offered a Windows 11 upgrade to this unsupported Windows 10 PC — despite it failing to meet the “non-negotiable” TPM 2.0 requirement

      July 29, 2025
      Recent

      Microsoft mysteriously offered a Windows 11 upgrade to this unsupported Windows 10 PC — despite it failing to meet the “non-negotiable” TPM 2.0 requirement

      July 29, 2025

      With Windows 10’s fast-approaching demise, this Linux migration tool could let you ditch Microsoft’s ecosystem with your data and apps intact — but it’s limited to one distro

      July 29, 2025

      Windows 10 is 10 years old today — let’s look back at 10 controversial and defining moments in its history

      July 29, 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

    Microsoft mysteriously offered a Windows 11 upgrade to this unsupported Windows 10 PC — despite it failing to meet the “non-negotiable” TPM 2.0 requirement

    July 29, 2025
    News & Updates

    With Windows 10’s fast-approaching demise, this Linux migration tool could let you ditch Microsoft’s ecosystem with your data and apps intact — but it’s limited to one distro

    July 29, 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

    Citrix NetScaler Console Vulnerability Enables Admin Access – PoC Released

    Security

    Best 5 Animation Explainer Video Production Companies in Shreveport

    Web Development

    CVE-2025-4264 – PHPGurukul Emergency Ambulance Hiring Portal SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-39403 – Mojoomla WPAMS SQL Injection

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    OpenAI to launch AI models that can think up their own experiments, says report

    April 15, 2025

    Dubbed o3 and o4-mini, the new models could arrive as early as this week -…

    Our favorite mobile controller is at the lowest price it’s ever been — “Well and truly the best at what it does”

    July 9, 2025

    CVE-2025-5379 – NuCom NC-WR744G Console Application Hard-Coded Credentials Remote Vulnerability

    May 31, 2025
    11 Things to Do After Installing Ubuntu 25.04

    11 Things to Do After Installing Ubuntu 25.04

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

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