Deploying AWS Lambda & API Gateway Using Terraform Modules.

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

What is AWS Lambda?

What is API Gateway?

How do Lambda functions and API Gateways work with Terraform?

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:

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

  • In the terminal use the aws configure command to enter your credentials.
$ aws configureaws_access_key_id=******************
aws_secret_access_key=*******************

Create Directory:

This working directory I am naming it lambda-lambda-lambda, but name it whatever you want.

Create the handler:

  • Create the file mylab.js with the following code in your directory.

Create zip file:

zip -r mylab.js mylab.zip

Create bucket & upload the zip.

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:

Run Terraform init and apply:

Copy URL to browser:

https://eosjxibed6.execute-api.us-east-1.amazonaws.com/test
  • You should get the following message in the browser.

AWS Console:

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:

  • 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

  • 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

Root Module

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.

  • 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:

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

Email → quileswest@gmail.com

Junior DevOps Engineer