Sunday, January 8, 2023

Terraform and IaC - creating EC2 Instance

I'm revisiting the post I did and creating the same EC2 instance using Terraform instead of going through the AWS Console.

I used the reference from Terraform's Registry to create my HCL script: aws-instance documentation

Here is my main.tf file that I created.

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

  required_version = ">= 1.2.0"
}

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

resource "aws_instance" "spring_boot_webserver" {
  ami           = "ami-0b5eea76982371e91"
  instance_type = "t2.micro"

  tags = {
    Name = "iac-java-webapp"
  }

  key_name = "java-demo-keypair"

  associate_public_ip_address = true
  subnet_id                   = "subnet-01b088baff86159af"
  vpc_security_group_ids = [
    "sg-0a5cddc1e38f7b9b5"
  ]
}

It was pretty cool to go into my AWS Console (after waiting about 30 seconds) and seeing the EC2 Instance that was created by running my Terraform script:



Terraform and IaC

This weekend I started looking into Terraform (specific product) and the general concept of Infrastructure as Code, or IaC.

First step was reading articles and watching tutorials about IaC.  

I've always been on the software side of the IT world.  

Throughtout my career, I've trusted colleagues on other teams to stand up the firewalls, network, co-located physical servers (later VMs) that ran my Java / Angular / .NET applications.  

I've always maintained a great rapport with friends on the ITOps/Infrastructure side of the house, and occasionally participated in discussions about servers, racks, blades, etc.

However, for the most part this was never anything I really dug into that deeply, and as a hands-on Java developer, I never had an opportunity to go to a co-located facility to run cable.

With the trend toward organizations leveraging Cloud solutions like AWS, I'm seeing both a need and a great opportunity as a Software Developer to learn about writing code that sets up infrastructure.

I downloaded Terraform on my Windows home development machine.

Initial thoughts:

  • Absolute amazement at how simple the download and setup was.  
  • It consisted of an 18.7 MB ZIP file that has a single EXE file
  • Created a directory C:\terraform\1.3.7, unzipped to here, and added that to my PATH