Terraform Basics

This will be a brief overview on how to create resources in AWS with Terraform

Set Access Credentials for AWS

Configure your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY with the following

aws configure

Refer to how to access AWS on notes for getting an Access Key and Secret

This example terraform code should give you an idea of how to create resources in this case we'll be making an EC2 Instance

main.tf
# Set your provider and region in this case AWS and Ohio 
provider "aws" {
    region = "us-east-2"
}

resource "aws_instance" "my-first-instance" { # resource first then custom name
    ami           = "123gd4df5678a" # image ID is necessary to tell AWS what to build
    instance_type = "t2.nano" # this is the size of the VM. This size is free tier
    key_name      = "My-first-key" # this will tell AWS which key to use, it must already exist
}
    
    # It's important for readability to have equal signs in a resource align
    # Not doing so may cause issues in running this code
    
    # Save and exit this file and run `terraform init` followed by `terraform apply main.tf`
    

main.tf is the main file terraform runs by default. Create the main.tf file fill it with your Terraform code. Afterwards run: terraform init followed by terraform apply to create your resources.

Last updated