Aug 13, 2024 1 min read

Terraform: Coding an AWS Configuration

Terraform: Coding an AWS Configuration

🖳 Learn how to write Terraform code to automate the build of AWS infrastructure!

💡
Watch the video below to see the hands-on lab!

In this sub-lesson, we’ll prepare an AWS configuration by coding it step-by-step within VSCode. This lab incorporates a Linux instance and AWS security groups.

You'll need the following to complete the lab:

Enjoy the video and the lab!

📽️
Check out the Hashicorp Certified Terraform Associate video course: available on O'Reilly and InformIT. (For InformIT, use the code PROWSE70 at checkout to receive a 70% discount.)

Here's the solution for the main.tf file

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.20"
    }
  }

  required_version = ">= 1.2.8"
}

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

resource "aws_instance" "lesson_04" {
  ami           = "ami-0c7c4e3c6b4941f0f"
  instance_type = "t2.micro"
  vpc_security_group_ids = [
    aws_security_group.sg_ssh.id,
    aws_security_group.sg_https.id
  ]

  tags = {
    Name      = "Lesson-04-VM-SG"    
  }
}

resource "aws_security_group" "sg_ssh" {
  ingress {
    cidr_blocks = ["0.0.0.0/0"]
    protocol    = "tcp"
    from_port   = 22
    to_port     = 22
  }

  egress {
    cidr_blocks = ["0.0.0.0/0"]
    protocol    = "tcp"
    from_port   = 0
    to_port     = 0
  }
}

resource "aws_security_group" "sg_https" {
  ingress {
    cidr_blocks = ["192.168.0.0/16"]
    protocol    = "tcp"
    from_port   = 443
    to_port     = 443
  }

  egress {
    cidr_blocks = ["0.0.0.0/0"]
    protocol    = "tcp"
    from_port   = 0
    to_port     = 0
  }
}
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Prowse Tech.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.