Terraform Brew

Posted on  by admin

Today I’m setting up my new laptop. One of the tool I’ve been used recently is terraform. And I’d like to use VS Code as my code editor.

  1. Upgrade Terraform Version
  2. Terraform Brew Update
  3. Brew Terraform 13

By using the fresh install of VS Code, it doesn’t give you the ability to format your terraform template (*.tf). BTW, the shortcut for formatting text in VS Code is option+shift+P.

Terraform is a Stout - Russian Imperial style beer brewed by Stonecloud Brewing in Oklahoma City, OK. Score: n/a with 2 ratings and reviews. Choco install terraform -y. On macOS we can use homebrew to install terraform using the below command. Brew install terraform. Terraform Components. Before starting off on terraform it is essential to understand the basic building blocks needed to create a terraform script to provision and deploy resources.

After a few minutes of troubleshooting, I realised I didn’t install terraform.

Upgrade Terraform Version

Once I’ve installed terraform on my laptop, I hit the option+shift+P shortcut. VS Code pops a window to download the formatting plugin.

Everything is automatically complete. Now I can enjoy the “lazy” formatting for my terraform template.

Amazon AWS' Lambdas are incredibly powerful, mainly due to their stateless nature and ability to scale horizontally almost infinitely. But once you have written a Lambda function, how do you update it? Better yet, how do you automate deploying and updating it across multiple regions? Today, we're going to take a look at how to do exactly that using Hashicorp's Terraform

What is Terraform?

Terraform

Managing server resources can be either very manual, or you can automate the process. Automating the process can be tricky though, especially if you have a complex tree of resources that depend on one another. This is where Terraform comes in.

Terraform enables you to safely and predictably create, change, and improve production infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

Terraform provides a DSL that allows you to describe the resources that you need and their dependencies, allowing Terraform to launch/configure resources in a particular order.

Installing Terraform

Installing Terraform is pretty straightforward.

Terraform Brew Update

If you're on macOS simply run:

If you're on Linux, depending on your distro and package manager of choice, it might be available, otherwise, follow the directions provided on the installation page.

Setting up AWS credentials

Before setting up the credentials, we're going to install the AWS command line interface.

On macOS, the awscli is available through homebrew:

On Linux, you can often find the awscli in your package manager:

Terraform

You can also install it manually using pip:

Once installed, simply run:

And follow the prompts to provide your AWS credentials. This will generate the proper credentials file that Terraform will use when communicating with AWS.

Describe your infrastructure

Now that we have AWS configured, we can start to describe the AWS Lambda that we're going to deploy.

To start, create a new directory.

In that directory we're going to create a main.tf file that looks like this:

main.tf

This is telling Terraform that we're going to be using the AWS provider and to default to the 'us-east-1' region for creating our resources.

Now, in main.tf, we're going to describe our lambda function:

Here, we're saying that we want a NodeJS based lambda and will expose its handler as an exported function called 'handler' on the index.js file (don't worry, we'll create this shortly), and that it will be uploaded as a zip file called 'function.zip'. We're also taking a hash of the zip file to determine if we should re-upload everything.

Create an execution role

Next, what we need to do is set the execution role of our Lambda, otherwise it wont be able to run. In main.tf we're going to define a role in the following way:

Brew Terraform 13

This creates an IAM role in AWS that the Lambda function will assume during execution. If you wanted to grant access to other AWS services, such as S3, SNS, etc, this role is where you would attach those policies.

Now, we need to add the 'role' property to our lambda definition:

Creating a test NodeJS function

We specified NodeJS as runtime for our lambda, so let's create a function that we can upload and use.

index.js

Now let's zip it up:

Test our Terraform plan

To generate a plan and show what Terraform will execute, run terraform plan:

This tells us that terraform is going to add both the role and the lambda when it applies the plan.

When you're ready, go ahead and run terraform apply to create your lambda:

To see if it worked properly, you can use the aws cli to list all of your lambda functions:

We can now invoke our lambda directly from the aws cli. In this script, Im using a commandline utility called jq for parsing the JSON response. If you're on macOS, simply run brew install jq to install it:

Terraform

This will run your lambda and decode the last 4kb of the logfile. To view the full logfile, log into the aws web console and head over to the CloudWatch logs.

Wrap up

That's it! From here, you'll be able to set up a lamba that gets run on certain triggers - SNS events, S3 operations, consume data from a Kinesis firehose, etc.

All of the files we've created here can be found on Github at seanmcgary/blog-lambda-terraform