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

      How To Prevent WordPress SQL Injection Attacks

      June 14, 2025

      This week in AI dev tools: Apple’s Foundations Model framework, Mistral’s first reasoning model, and more (June 13, 2025)

      June 13, 2025

      Open Talent platforms emerging to match skilled workers to needs, study finds

      June 13, 2025

      Java never goes out of style: Celebrating 30 years of the language

      June 12, 2025

      6 registry tweaks every tech-savvy user must apply on Windows 11

      June 14, 2025

      Here’s why network infrastructure is vital to maximizing your company’s AI adoption

      June 14, 2025

      The AI video tool behind the most viral social trends right now

      June 14, 2025

      Got a new password manager? How to clean up the password mess you left in the cloud

      June 14, 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

      Right Invoicing App for iPhone: InvoiceTemple

      June 14, 2025
      Recent

      Right Invoicing App for iPhone: InvoiceTemple

      June 14, 2025

      Tunnel Run game in 170 lines of pure JS

      June 14, 2025

      Integrating Drupal with Salesforce SSO via SAML and Dynamic User Sync

      June 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2 tests toggle to turn off Recommended feed in the Start menu

      June 14, 2025
      Recent

      Windows 11 24H2 tests toggle to turn off Recommended feed in the Start menu

      June 14, 2025

      User calls Windows 11 “pure horror,” Microsoft says it’s listening to feedback

      June 14, 2025

      John the Ripper is an advanced offline password cracker

      June 14, 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

    Operating Systems

    Windows 11 24H2 tests toggle to turn off Recommended feed in the Start menu

    June 14, 2025
    Operating Systems

    User calls Windows 11 “pure horror,” Microsoft says it’s listening to feedback

    June 14, 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

    Stateless decision making

    Learning Resources

    Apple’s “Liquid Glass” Vision: iPhone 2027 & Unified OS 26

    Security

    CVE-2025-30171 – ASPECT System File Deletion Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-5081 – Campcodes Cybercafe Management System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Cisco Warns of Credential Vuln on AWS, Azure, Oracle Cloud

    June 5, 2025

    Cisco Warns of Credential Vuln on AWS, Azure, Oracle Cloud

    Source: Pior Swat via Alamy Stock PhotoNEWS BRIEFA vulnerability found in Amazon Web Services (AWS), Microsoft Azure, and Oracle Cloud Infrastructure (OCI) cloud deployments of Cisco Identity Services …
    Read more

    Published Date:
    Jun 05, 2025 (1 hour, 41 minutes ago)

    Vulnerabilities has been mentioned in this article.

    CVE-2025-20286

    How AI is Transforming the World

    May 14, 2025

    The death of spreadsheets: 6 reasons why AI will soon be the dominant business reporting tool

    May 12, 2025

    CVE-2025-20967 – Samsung Gallery File Access Vulnerability (Privilege Escalation)

    May 7, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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