TutorialbeginnerPart 9 of 18

Building and Deploying

Time to see your code live in the cloud.

April 20, 20263 min read

What you'll learn

  • Using sam build with cargo-lambda
  • Deploying your stack to AWS
  • Verifying the live endpoint
Prerequisites:Previous post completed (Connecting Lambda to API Gateway)

We've spent a lot of time talking about configuration files and handlers. Now, it's time for the payoff. We're going to build our Rust code and push it to AWS.

Building with SAM

Back in lesson 7, we added BuildMethod: rust-cargolambda to our template.yaml. This is a game-changer because it tells AWS SAM exactly how to handle Rust.

Run this in your terminal:

Terminal
bash
sam build

SAM will now:

  1. Pull down a build container (or use your local environment).
  2. Run cargo lambda build for you.
  3. Cross-compile your code for the arm64 Linux environment.
  4. Put the final bootstrap binary in a .aws-sam folder.

If everything is correct, you'll see a "Build Succeeded" message. If not, it's usually because a dependency is missing in Cargo.toml or there's a syntax error in your Rust code.

Deploying to AWS

Now, let's push it live. If this is your first time deploying this project, use the guided flag:

Terminal
bash
sam deploy --guided

SAM will ask you a few questions. Here is how I usually answer:

  • Stack Name: rust-serverless-api (or whatever you like).
  • AWS Region: Your closest region (e.g., us-east-1).
  • Parameter StageName: dev.
  • Confirm changes before deploy: Y (good for safety).
  • Allow SAM CLI IAM role creation: Y (needed for permissions).
  • Disable rollback: N.
  • HelloFunction has no authentication. Is this okay?: Y (we're just learning).
  • Save arguments to configuration file: Y.
  • SAM configuration file: samconfig.toml.
  • SAM configuration environment: default.

Once you confirm, SAM will start creating the resources on AWS. It usually takes about 2-3 minutes the first time.

Finding your API URL

Once the deployment finishes, look at the Outputs section in your terminal. You should see ApiUrl:

text
Source File
-------------------------------------------------------------------------------------------------
Outputs                                                                                         
-------------------------------------------------------------------------------------------------
Key                 ApiUrl                                                                      
Description         API Gateway endpoint URL                                                    
Value               https://a1b2c3d4e5.execute-api.us-east-1.amazonaws.com/dev                  
-------------------------------------------------------------------------------------------------

Testing the endpoint

Copy that URL and add /hello to the end. You can use your browser or curl:

Terminal
bash
curl https://a1b2c3d4e5.execute-api.us-east-1.amazonaws.com/dev/hello

You should get back:

json
Source File
{
  "statusCode": 200,
  "message": "Hello World!",
  "data": null
}

Info

Congratulations! You just deployed a high-performance Rust API to the cloud. No cold starts, tiny memory footprint, and it costs almost nothing.

What about samconfig.toml?

Now that you've run the guided deploy, you'll notice a samconfig.toml file in your root. This file stores your deployment preferences. Next time you want to deploy, you can just run:

Terminal
bash
sam deploy

It will use the settings you saved, making the process much faster.

Our API works, but it's very static. It always says the same thing. In the next post, we'll learn how to handle path parameters and query strings to make our API dynamic.