☑️Day 67: Terraform State & Starting with AWS🚀

🔹Table of Contents :

  • Introduction

  • Setting Up Terraform with AWS

  • Creating an AWS IAM User for Terraform

  • Using Terraform Commands with AWS

  • Key Takeaways


In today’s DevOps journey, I explored how Terraform manages state and performed hands-on work integrating Terraform with AWS to begin provisioning cloud resources. I’ll go through each step, from setting up the AWS CLI to using Terraform commands with AWS resources, and wrap up with some real-world applications.


1. Introduction to Terraform State

Terraform’s state file (.tfstate) is a crucial part of managing infrastructure, as it keeps track of resources already provisioned. Here’s a breakdown:

  • Commands:

    •   terraform init
      

      Initializes the project and installs necessary providers.

    •   terraform plan
      

      Previews changes to be applied, without actually making changes.

    •   terraform apply
      

      Provisions resources as specified in .tf files.

  • What’s Included in .tfstate:

    • Resource types and settings.

    • Provider configurations.

    • Metadata about each resource's configuration.

    • Real-World Scenario: With .tfstate, Terraform can track whether an S3 bucket or EC2 instance exists, helping avoid redundant creation.

2. Setting Up Terraform with AWS

The AWS CLI is necessary for managing AWS resources directly from the command line, which integrates seamlessly with Terraform. Here are the setup steps I followed:

AWS CLI Installation

  1. Create a Project Directory:

     mkdir terraform-aws
     cd terraform-aws
    
  2. Install AWS CLI:

     sudo apt update
     sudo apt install awscli -y
    
  3. Verify Installation:

     aws --version
    
  4. List AWS S3 Buckets (to test connection):

     aws s3 ls
    
  5. Configure AWS CLI:

     aws configure
    

    Enter your AWS Access Key, Secret Access Key, Region, and preferred Output format. This step sets up AWS CLI to communicate with Terraform.

Real-World Scenario: AWS CLI configuration allows you to manage and automate cloud resources efficiently, enabling you to deploy and scale resources faster, especially useful in multi-region setups.

3. Creating an AWS IAM User for Terraform

AWS Identity and Access Management (IAM) provides secure access control. Creating a dedicated IAM user for Terraform helps keep permissions organized and ensures security.

Steps to Create an IAM User:

  1. Log into AWS Console and navigate to IAM.

  2. Create a New User:

    • Go to Users > Add User.

    • Name the user (e.g., terraform_user).

    • Set Access Type to Programmatic access.

  3. Attach Policies:

    • Add AdministratorAccess (or custom policies based on permissions needed).
  4. Store Credentials: Download the .csv file with the access and secret keys, which will be used in aws configure.

Real-World Scenario: By using a dedicated IAM user, you can isolate and audit Terraform operations, ensuring that all infrastructure changes are secure and trackable.

4. Using Terraform Commands with AWS

Now that the AWS CLI is set up, I created a simple Terraform configuration file (main.tf) to start defining and deploying AWS resources.

Writing main.tf for AWS Resources

Below is an example configuration that sets up an S3 bucket and an EC2 instance.

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

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-terraform-bucket-unique-id"
  acl    = "private"
}

resource "aws_instance" "my_ec2_instance" {
  ami           = "ami-0c55b159cbfafe1f0"  # Example Ubuntu AMI
  instance_type = "t2.micro"
}

Running Terraform Commands

  1. Initialize the Project:

     terraform init
    

    This downloads necessary providers and plugins for AWS.

  2. Preview Changes:

     terraform plan
    

    Shows what will be created in AWS before making actual changes.

  3. Apply Configuration:

     terraform apply
    

    Applies the configuration and provisions the resources defined in main.tf.

  4. View Terraform State:

     terraform show
    

    Provides details of the current state, showing the resources Terraform manages.

  5. List State Resources:

     terraform state list
    

    Lists all resources currently tracked by Terraform’s state file.

  6. Verify in AWS: Go to the AWS Console and check if the S3 bucket and EC2 instance are created as specified.

Real-World Scenario: Using Terraform with AWS, you can automate the creation and scaling of resources. This approach is particularly useful for quickly setting up environments for testing, scaling production resources, or creating infrastructure templates.


5. Key Takeaways

  • Terraform State: Tracks resources, prevents duplicate creation, and provides current configuration details.

  • AWS CLI and IAM: Configures Terraform to interact securely with AWS services.

  • Terraform Commands with AWS: Easily manage infrastructure lifecycle through commands like init, plan, and apply.

Real-World Application: With Terraform and AWS, infrastructure can be version-controlled and replicated across multiple environments (development, testing, production) with minimal manual effort, supporting rapid deployment and scalability.


Stay tuned for more hands-on tasks and in-depth learning!🚀

🚀Thanks for joining me on Day 67! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)
0
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti