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

      5 preinstalled apps you should delete from your Samsung phone immediately

      July 30, 2025

      Ubuntu Linux lagging? Try my 10 go-to tricks to speed it up

      July 30, 2025

      How I survived a week with this $130 smartwatch instead of my Garmin and Galaxy Ultra

      July 30, 2025

      YouTube is using AI to verify your age now – and if it’s wrong, that’s on you to fix

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

      Time-Controlled Data Processing with Laravel LazyCollection Methods

      July 30, 2025
      Recent

      Time-Controlled Data Processing with Laravel LazyCollection Methods

      July 30, 2025

      Create Apple Wallet Passes in Laravel

      July 30, 2025

      The Laravel Idea Plugin is Now FREE for PhpStorm Users

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

      New data shows Xbox is utterly dominating PlayStation’s storefront — accounting for 60% of the Q2 top 10 game sales spots

      July 30, 2025
      Recent

      New data shows Xbox is utterly dominating PlayStation’s storefront — accounting for 60% of the Q2 top 10 game sales spots

      July 30, 2025

      Opera throws Microsoft to Brazil’s watchdogs for promoting Edge as your default browser — “Microsoft thwarts‬‭ browser‬‭ competition‬‭‬‭ at‬‭ every‬‭ turn”

      July 30, 2025

      Activision once again draws the ire of players for new Diablo Immortal marketing that appears to have been made with generative AI

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

    New data shows Xbox is utterly dominating PlayStation’s storefront — accounting for 60% of the Q2 top 10 game sales spots

    July 30, 2025
    News & Updates

    Opera throws Microsoft to Brazil’s watchdogs for promoting Edge as your default browser — “Microsoft thwarts‬‭ browser‬‭ competition‬‭‬‭ at‬‭ every‬‭ turn”

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

    MM-Ego: Towards Building Egocentric Multimodal LLMs

    Machine Learning

    CVE-2025-47951 – Weblate Second Factor OTP Guessing Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CodeSOD: The Firefox Fix

    News & Updates

    KitchenOwl – self-hosted grocery list and recipe manager

    Linux

    Highlights

    What’s the difference between named functions and arrow functions in JavaScript?

    June 30, 2025

    Comments Source: Read More 

    TAG-140 Deploys DRAT V2 RAT, Targeting Indian Government, Defense, and Rail Sectors

    July 7, 2025

    New generative AI tools open the doors of music creation

    May 13, 2025

    Creating a Brand Kit in Stream: Why It Matters and How It helps Organizations

    July 15, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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