Featured image of post Installing Terraform and Creating an EC2 Instance

Installing Terraform and Creating an EC2 Instance

 

Ensure the prerequisites

  • An AWS Account
  • AWS cli installed, instructions for the same can be found here.
  • Have generated credentials (access key and secret), can be created from here.

Setting up AWS CLI

Type aws configure on the terminal and enter the Access Key ID and Secret Access Key, other fields can be blank.

Creating a config file for terraform to understand

  • Create a directory and switch to it
  • Create a file named main.tf and populate it with the following sample configuration,
    where
    • app_server is the name of the instance
    • us-west-2 is the region where the instance will be deployed
    • ami-830c94e3 is the unique identifier for the instance
    • t2.micro is the instance type
    • ExampleAppServerInstance is the tag name for grouping of instances
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }

  required_version = ">= 0.14.9"
}

provider "aws" {
  profile = "default"
  region  = "us-west-2"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

Installation of and Working with Terraform CLI:

  • Terraform can be installed following this guide
  • Configuration of terraform CLI can be done using command terraform init
  • The configuration file can be formatted using terraform fmt command
  • And the file can be validated using terraform validate command
  • Finally all the changes can be viewed using terraform plan command and perfomed using terraform apply command

Here is a terminal cast performing the above actions

comments powered by Disqus