Set up Terraform with AWS from scratch

This article explains how to set up Terraform from scratch to provision AWS resources. In this tutorial we will set up Terraform with all of the required prerequisites and will spin up an EC2 instance using Terraform.

Terraform from scratch

Before provisioning any resource in AWS using Terraform following steps needs to be completed, we will cover each of these in detail.

  1. Terraform Installation
  2. AWS CLI Installation
  3. AWS Credentials Configuration

Terraform Installation

To install Terraform in Windows complete below steps.
  • Download appropriate Terraform package from this link.
  • Extract downloaded zip file.
  • Create new directory terraform and place terraform executable in it.
  • Add the terraform folder location to your PATH variable, eg: Control Panel -> System -> System settings -> Environment Variables .
  • Verify Installation: Launch new cmd prompt and run below command, you should see terraform version as output.
  •  
    terraform --version
    
    
    

AWS CLI Installation

  1. Run the below msiexec command in command prompt with admin privileges to run the MSI installer. This will launch the AWS CLI Setup wizard.
  2.    
    msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
         
  3. Complete the installation by following instructions from Setup wizard.
  4. Verify Installation: Launch new cmd prompt and run below command, you should see aws cli version as output.
  5.    
    aws --version
         

AWS Credentials Configuration

For setting up Credentials first we need to create AWS IAM User along with AWS Access Key ID and AWS Secret Access Key.

  1. Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/
  2. In the navigation pane, choose Users.
  3. Click on Add Users.
  4. Enter username aws-tf-user and select Access key - Programmatic access.
  5. tf-user

  6. Click on Next: Permissions
  7. Click on Select Attach existing policies directly and add required permissions.
  8. aws-permissions
  9. Click on Next: Tags, if you need to provide tags enter required tags or click on Next:Review.
  10. Click on Create user.
  11. Download the csv and click on close.
  12. csv-download

  13. Open cmd, enter command aws configure and provide required information from downloaded csv.
  14. aws-configure

  15. Verify Installation: run below command in cmd and you should see user details in output
  16.    
    aws sts get-caller-identity
         

Spinning up EC2 instance using Terraform

Now we have the Prerequisites properly installed and configured, lets proceed with resource provisioning using Terraform.
  • Create directory for Terraform configuration.
  •    
    mkdir tf-with-gcptutorials
         
  • Navigate to created directory.
  •    
    cd tf-with-gcptutorials
         
  • Create a file with name main.tf
  • Open main.tf with any text editor
  • Copy the configuration below, and save main.tf.
  •    
    terraform {
        required_providers {
            aws = {
            source  = "hashicorp/aws"
            version = "~> 3.27"
            }
        }
        
        required_version = ">= 0.14.9"
        }
        
        provider "aws" {
        profile = "default"
        region  = "us-east-1"
        }
        
        resource "aws_instance" "test_instance" {
        ami           = "ami-033b95fb8079dc481"
        instance_type = "t2.micro"
        
        tags = {
            Name = "test instance"
        }
        }
         
  • Initialize the directory with terraform init command.
  •    
    terraform init
         
  • Format and validate configuration with below commands.
  •    
    terraform fmt
    
    terraform validate
    
         
  • Check the execution plan with command terraform plan
  •    
    terraform plan
         
  • Create EC2 instance with command terraform apply. Type "yes" in "Do you want to perform these actions" prompt, in some time you should see EC2 instance created.
  •    
    terraform apply
         

    terraform-apply


Category: AWS