TutorialbeginnerPart 7 of 18

Adding Lambda to template.yaml

Your code exists, but AWS doesn't know about it yet. Let's fix that.

April 18, 20263 min read

What you'll learn

  • How to define Lambda resource
  • What CodeUri, Handler, and BuildMethod mean
  • How to register binaries in Cargo.toml
Prerequisites:Previous post completed (Writing Your First Handler)

In the last lesson on configuration, we set up the API Gateway, routes, and error responses. Now we need to add the actual Lambda function under the Resources section.

Add this after your gateway responses:

template.yaml
yaml
> # Gateway Responses
...
# Lambda
  HelloFunction:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: rust-cargolambda
    Properties:
      CodeUri: .
      Handler: bootstrap
      Runtime: provided.al2023
      Architectures: ["arm64"]

Let's go through each line:

Type: AWS::Serverless::Function

This tells CloudFormation "create a Lambda function". The AWS::Serverless prefix means we're using SAM's simplified syntax, which saves us from writing a lot of boilerplate.

BuildMethod: rust-cargolambda

This tells SAM to use cargo-lambda when building your code. Without this, SAM would try to build it like a Node.js or Python project and fail.

CodeUri: .

This points to the directory where your Cargo.toml lives. Since we moved Cargo.toml to the root of the project back in the organizing lesson, the path is just . (current directory).

Handler: bootstrap

For custom runtimes like Rust, the compiled binary must be called bootstrap. That's just how Lambda's custom runtime works. cargo-lambda handles this naming automatically.

Runtime: provided.al2023

This tells Lambda to use Amazon Linux 2023 as the base environment. Since Rust compiles to a native binary, we don't need a language-specific runtime like nodejs20.x. We just need the OS.

Architectures: ["arm64"]

We're building for ARM64 (Graviton). It's cheaper and faster than x86 on Lambda. If you have issues with this, you can switch to x86_64, but ARM is the way to go.

Registering the binary in Cargo.toml

Rust needs to know which files are binaries. Since we're putting our handlers in src/bin/, we should explicitly register them. Add this to the bottom of your Cargo.toml:

Cargo.toml
toml
[[bin]]
name = "hello"
path = "src/bin/hello.rs"

The name here is important. It's what cargo-lambda will use to create the binary. We'll reference this name later when we move to the Makefile approach.

Pro Tip

If you have a file at src/bin/something.rs, Cargo will auto-discover it as a binary named something. The [[bin]] section is optional for simple cases, but it's good practice to be explicit, especially when you start adding features or required-features later.

Your template.yaml so far

Here's what the full template looks like at this point:

template.yaml
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: rust-serverless-api, Sample SAM Template for rust-serverless-api
 
# Parameters
Parameters:
  StageName:
    Type: String
    Default: dev
    Description: The stage name for the API Gateway.
 
# Globals
Globals:
  Function:
    MemorySize: 128
    Timeout: 30
    Tracing: Active
    Runtime: provided.al2023
    LoggingConfig:
      LogGroup: !Sub /rust-example/${StageName}/example
      LogFormat: JSON
    Environment:
      Variables:
        STAGE: !Sub ${StageName}
 
Resources:
  RustExample:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: !Sub ${AWS::StackName}
      Description: !Sub Rust Example Service ( ${StageName} )
 
  # ... gateway responses from previous lesson ...
  # ... routes from previous lesson ...
 
# Lambda
  HelloFunction:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: rust-cargolambda
    Properties:
      CodeUri: .
      Handler: bootstrap
      Runtime: provided.al2023
      Architectures: ["arm64"]

We've told AWS about our Lambda, but it's still not connected to the API Gateway routes we defined earlier. In the next post, we'll wire them together using API Gateway Methods.