• Nikody Keating

Terraform: An Introductory Guide for Serverless Application Development on AWS


AWS Lambda has taken the world by storm. It allows you to write and run code without having to worry about servers. But what if you want to use Terraform to manage your AWS resources? In this blog article, we will show you how to do just that! Terraform is a powerful tool for managing serverless applications, but it doesn't protect you from vendor lock in. Make sure you are aware of the pros and cons before using Terraform for your next project.

Writing your first Terraform Lambda Function

Terraform is a powerful tool for managing serverless applications. In this section, we will show you how to write your first Terraform Lambda function. This function will print "Hello, World!" to the console.

In order to run this code, you will need to install the Terraform CLI and the Terraform AWS provider.


Click here to for the instructions on how to install the terraform CLI from Hashicorp.


Create a file called "terraform.tf" and add the following

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

Then run this command to install the AWS provider:

 $ terraform init

After running this command, you will be able to run the Terraform AWS provider by running the following command:

$ terraform plan 
$ terraform apply

Now that we have our Terraform configuration file, we can deploy our code with Terraform. In order to do this, we need to create a Terraform file called hellolambda.tf . This file contains the

instructions for Terraform to deploy our code. Here is an example:

module "lambda" {
	source = "github.com/terraform-providers/aws-lambda-terraform?ref=master"
	vendor = "aws"
	provider "aws" {
	region = "us-east-1"
}

data "archive_file" "lambda_zip" {
    type        = "zip"
    source_dir  = "source"
    output_path = "lambda.zip"
}

resource "aws_lambda_function" "hello" { 	
	function_name = "hello"
	handler = "hello.handler"
	runtime = "python2.7"
	filename = "lambda.zip"
	source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
}


Next we need to write the code for our lambda function. We first need to make a directory called "source", which was defined in the lambda_zip archive resource defined in our hellolambda.tf file. After creating the directory, we'll create a file in the directory called "hello.py". This is in reference to the first part of our lambda handler (before the "."). We'll then define a function called "handler" as follows.



def handler(event, context):
    print("Hello!")

Lets prepare to deploy it

Deploying with terraform is a multistep process. First we need to make sure we have all the modules necessary for all the terraform files we've defined (both terraform.tf and hellolambda.tf). We do this by running the following command.


tf init

Next we need to construct the content and zip files, along with a plan for what resources are being created, which ones are being updated and which ones are being deleted. We do this by running the following.


tf plan

Finally we're ready to deploy using the following command.


tf apply

What is the advantage of using AWS Lambda with Terraform?

One of the big myths is that having the flexibility to switch between cloud providers will give you leverage against a price hikes, or allow you to protect yourself from a single cloud provider bringing your system down. While there is some truth to the later, systems which use native services in AWS run at 1% - 5% the cost as those designed to be cloud agnostic. Team's which work with a cloud provider's native services often write less code, have less infrastructure to maintain and write more secure systems as well. This is often due to a mentality of leveraging the cloud services inherent abilities rather than writing their own way to handle the same problems.

AWS Lambda, S3 and various other serverless components in AWS can be used in Terraform, AWS CloudFormation and the AWS CDK. The method of defining Lambda Functions is more of a language and design choice rather than a choice to be cloud native of agnostic.

Terraform Myth #1: Terraform will make you vendor agnostic

Terraform is a powerful tool that can be used for managing serverless applications. However, it does not protect you from vendor lock in. This means that if you choose to use Terraform for your next project, you may be limited to using AWS as your provider. Make sure you are aware of the pros and cons of using Terraform before deciding whether or not to use it.


Terraform Myth #2: Terraform is going to limit what you can do in AWS

This is an easy trap to fall into if you've never used terraform before, I had the same preconceived notion before using it. Terraform is evolving quickly, and has the ability to have custom components written for it, as well as common components for AWS services shortly after they're released.


Conclusion

Writing Lambda Functions using Terraform can be a good choice, and offers the benefits of Terraform's modular approach. It can be very similar to developing code in AWS CloudFormation or the AWS CDK, and doesn't take long to learn. If you're making a choice as to using Terraform, make sure you're choosing it for the right reasons though, and set expectations with your company as to what it does and what it doesn't do.

64 views0 comments

Subscribe to get our latest content by email and a free "Guide to Building an Internal Enterprise Website on AWS Serverless".