Deploying to AWS using Terraform for Local Experimentation

Terraform is a tool used to turn an api into code, and in this case we are going to use with AWS.

The download of terraform is available here

You don’t want aws secrets in your git repo, so this is a basic structure i start out with when i am doing local terraform experimentation.

Terraform Setup on Ubuntu

There is a nice script here that installs terraform.  Just run it periodically to keep it up to date.

https://github.com/ryanmaclean/awful-bash-scripts/blob/master/install_recent_terraform_packer.sh

I am a noob in the linux stuff so i needed to google to how to make it executable.

sudo chmod +x scriptname

vars.tf

Used to define the set of variables used in Terraform

variable "AWS_ACCESS_KEY" { }
variable "AWS_SECRET_KEY" { }
variable "AWS_REGION" 
{
    default = "us-east-1"
}

 

provider.tf

Defines what api will get ran. Since this is aws we are deploying to we are using the aws provider.  If we were deploying to azure the provider would be azurerm

provider "aws" {
    access_key = "${var.AWS_ACCESS_KEY}"
    secret_key = "${var.AWS_SECRET_KEY}"
    region = "${var.AWS_REGION}
}

terraform.tfvars

Put this in the git ignore so the secrets stay on your local machine. If we were using a build server an alternative solution would need to be used.  But for a small scripting effort this works great.

AWS_ACCESS_KEY = ""
AWS_SECRET_KEY = ""
AWS_REGION = ""

instance.tf

This one can really be named anything. It is where all of the resources would get built out.

resource "aws_instance" "example" {
   ami             = ""
   instance_type   = "t2.micro"
}

Here is the github repo that this is stored in.

https://github.com/jonshern/terraformstarter

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s