Building and Deploying
Time to see your code live in the cloud.
What you'll learn
- Using sam build with cargo-lambda
- Deploying your stack to AWS
- Verifying the live endpoint
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:
sam buildSAM will now:
- Pull down a build container (or use your local environment).
- Run
cargo lambda buildfor you. - Cross-compile your code for the
arm64Linux environment. - Put the final
bootstrapbinary in a.aws-samfolder.
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:
sam deploy --guidedSAM 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:
-------------------------------------------------------------------------------------------------
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:
curl https://a1b2c3d4e5.execute-api.us-east-1.amazonaws.com/dev/helloYou should get back:
{
"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:
sam deployIt 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.