softprops / serverless-rust

⚡ 🦀 a serverless framework plugin for rustlang applications
https://www.npmjs.com/package/serverless-rust
MIT License
548 stars 82 forks source link

📣 PSA for AWS Lambda: Easy Switch to `cargo lambda` 📈 #125

Open kalzoo opened 8 months ago

kalzoo commented 8 months ago

Hi everyone!

First of all, thanks to @softprops for building this tool and running it for several years.

If you're like me, you have some legacy projects using serverless-rust from the days when it was maintained. It was a great product that was irreplaceable when it launched and AWS didn't have great support for Rust.

But times have changed - the author has moved on to using SAM, and hey, open-source projects take a lot of time and effort to run. But now it's stuck in a broken state and you can't redeploy your once-working functions. It seems like this project has run its course.

But - fear not - it's easy to switch to the AWS-backed cargo lambda while sticking with serverless. I just did it in under 15 minutes. Here's how:

Step 1: Install & RTFM

Check out https://www.cargo-lambda.info/

Step 2: [Optional] Uninstall serverless-rust

just for sanity, to make sure it's fully out of your workflow.

  1. Remove from dependencies
  2. Remove from serverless.yml plugins field

Step 3: Select a Runtime

You'll want one of the OS-Only runtimes provided by AWS. I used provided.al2023 but you may want something else.

Replace this line:

runtime: rust

with

runtime: provided.al2023 # or your selected value

Step 4: Update Your Function Handlers

Update your serverless.yml like so:

package:
  individually: true

functions:
  hello-world:
    handler: does-not-matter-write-whatever
    package:
      # build with `cargo lambda build --output-format zip` and then fill in the build path. This is the default for `src/bin/hello_world.rs`
      artifact: target/lambda/hello_world/bootstrap.zip

Step 5: Build

cargo lambda build --output-format zip

Now that you have support for it (since this is pending issue #119 in serverless-rust), you can easily switch to ARM if you want:

cargo lambda build --output-format zip --arm64

just also make the switch in your serverless.yml:

provider:
  name: aws
  architecture: arm64

Step 6: Deploy

Just like always. Now, instead of building the project (like serverless-rust did), it'll just pick up your .zip file. You'll probably want to update your build scripts to reflect this two-step workflow.

sls deploy

That's all!

softprops commented 8 months ago

Pinned this issue for greater visibility. It's great to see someone actively taking up the space. Great work @kalzoo!