SERVERLESS RUBY
Build and Deploy AWS Lambda Functions from Scratch

Chapter 5: Your First Lambda with Terraform

Deploy Infrastructure Like a Dev, Not a Click-Ops Intern

It's time to leave the AWS Console behind and use code to define and deploy your Lambda function.

In this chapter, we'll:

  • Set up a Terraform project from scratch
  • Define your AWS provider and region
  • Create an ECR repository
  • Deploy a Lambda function using your Docker image
  • Run and test your function

You'll go from zero to infrastructure-as-code, and build a rock-solid foundation for the rest of the guide.


🧱 Step 1: Create a New Terraform Project

Make a new directory

mkdir terraform && cd terraform

Create this file to define your project:

# main.tf

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

  required_version = ">= 1.4.0"
}

provider "aws" {
  region = "us-east-1" # Change this if you're in another region
}

📦 Step 2: Set Up Your ECR Repository

Now let's define the Docker repository your Lambda will use:

# main.tf

resource "aws_ecr_repository" "ruby_lambda" {
  name = "my-ruby-lambda"
}

This creates a private ECR repo named my-ruby-lambda where we'll push your Docker image.

Continue reading with full access

Get unlimited access to Serverless Ruby and learn to build production-ready serverless Ruby applications.
📖
Complete guide with 8 chapters and bonus content
💻
Real-world examples with copy-paste code templates
🎯
Step-by-step tutorial and walkthroughs
Pay once to get full access to the book, including all future updates.