Deploying AWS Lambda & API Gateway Using Terraform Modules.

Christopher Quiles
6 min readMar 22, 2021

The process of building and deploying AWS Lambda and API Gateway using Terraform modules, and loading code repository with Git.

What is AWS Lambda?

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time that you consume — there is no charge when your code is not running.

What is API Gateway?

An API gateway provides a single, unified API entry point across one or more internal APIs. They typically layer rate limiting and security as well. An API gateway can help provide a unified entry point for external consumers, independent of the number and composition of internal microservices.

How do Lambda functions and API Gateways work with Terraform?

Serverless computing is a cloud computing model in which a cloud provider automatically manages the provisioning and allocation of compute resources. This contrasts with traditional cloud computing where the user is responsible for directly managing virtual servers.

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It uses Infrastructure as Code to provision and manage any cloud, infrastructure, or service.

Download Terraform:

Please download the proper package for your operating system.

https://www.terraform.io/downloads.html

  • To confirm terraform is working or not, run terraform --version , it should display the version of terraform installed.

Creating an IAM user

  • Go to Create IAM User (console) and create an IAM user with Administrator access permission. Ensure that this IAM user has console as well as programmatic access.
  • In the terminal use the aws configure command to enter your credentials.
$ aws configureaws_access_key_id=******************
aws_secret_access_key=*******************

Create Directory:

  • Create a directory with a name of your choice. For example:
This working directory I am naming it lambda-lambda-lambda, but name it whatever you want.

Create the handler:

  • The AWS Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event.
  • Create the file mylab.js with the following code in your directory.

Create zip file:

  • Zip this file into a new zip file such as mylab.zip by using the following command in your terminal.
zip -r mylab.js mylab.zip

Create bucket & upload the zip.

  • Create an S3 bucket in AWS with a unique name with the following command.
aws s3api create-bucket - bucket=igetbuckets21 - region=us-east-1
  • Upload mylab.zip into the S3 bucket you just created.
aws s3 cp mylab.zip s3://igetbuckets21/mylab.zip
the bucket has been created and uploaded

Create Terraform Module:

  • Create a file named Main.tf with the following code in the working directory you created earlier. Replace the name of the bucket file with your unique bucket. The region should be the same as your S3 bucket.

Run Terraform init and apply:

  • Now run terraform init. Which will download all the required plugins. Then run terraform apply -auto-approve.

Copy URL to browser:

  • Copy the URL from the Outputs from the Terraform apply. For example:
https://eosjxibed6.execute-api.us-east-1.amazonaws.com/test
  • You should get the following message in the browser.

AWS Console:

  • Look inside your AWS console to verify the Lambda and API Gateway was created successfully.
firstFunction lambda function has been created.
Test your lambda function to see if it runs successfully.
myAPI has been created.
API Gateway has been created successful and is now open for business.

Create Module Block:

A Terraform module is any set of Terraform configuration files in a folder. Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .… Yup, you guessed it…Modules! They can also be called multiple times, either within the same configuration or in separate configurations, allowing resource configurations to be packaged and re-used. To see what modules are really capable of, you have to use one module from another module. Makes sense right? Let me show you an example.

  • I created a child module, here’s how.
  • Made a new directory named “modules” (very original) and then inside that Modules folder, I created another folder called “lambda-api”.
child module
  • I then called all the resources from the child module to the parent (root) module. I then created the outputs and variables files.

Variables.tf

  • Input variables serve as parameters for a Terraform module, allowing aspects of the module to be customized without altering the module’s own source code, and allowing modules to be shared between different configurations.
  • When you declare variables in the root module of your configuration, you can set their values using CLI options and environment variables. When you declare them in child modules, the calling module should pass values in the module block.

Outputs.tf

  • In a parent module, outputs of child modules are available in expressions as module.<MODULE NAME>.<OUTPUT NAME>. For example, if a child module named web_server declared an output named instance_ip_addr, you could access that value as module.web_server.instance_ip_addr.

Root Module

  • The root module is the directory that holds the Terraform configuration files that are applied to build your desired infrastructure. These files provide an entry point into any nested modules you might utilize. Any module should include, at minimum, a main.tf , a variables.tf , and an outputs.tf file
We can now whip up 10 resources in less than 20 lines of code.
  • The resources have been applied and run with much more easily accessible module blocks. This makes it much easier for developers to read.

Push project to GitHub.

  • GitHub is a web-based platform used for project version control and codebase hosting. GitHub uses Git, a widely-used version control system.
  • Follow these steps below.
git initgit add .git commit -m "the changes you made to your code here"
  • Go to GitHub.com and create a new repository from your account. Click the + sign. It is located at the top right corner of the home page.
  • Name the repository.
  • Go back into your terminal and push the existing repository for your project to GitHub.
git remote add origin https://github.com/your-github-account-here/new-repository.gitgit branch -M maingit push -u origin main
Code repository has been successfully pushed to GitHub account.

Contact:

Linked In → https://www.linkedin.com/in/quiwest/

Website → https://www.chrisquiles.com/

Email → quileswest@gmail.com

--

--