Code Monkey home page Code Monkey logo

node-rust-wasm-lambda's Introduction

Node.js and WASM Lambda

Introduction

This is a sample project for compiling a Rust module to WASM and using JCO to import it in a Node.js AWS Lambda function.

Prerequisites

You must have the following tooling installed

Setup

Note: This is based on the JCO launch announcement blog post but the steps appear to be outdated so what follows is the changes required to make it work as of 2024-05-25.

The project was created by running the following cargo-component command:

cargo component new <project-name> --lib

This creates a component in src/lib.rs that has a simple Hello World function. This function was changed to one that will turn a string into upper case:

impl Guest for Component {
    fn shout(input: String) -> String {
        let mut s = input.to_uppercase();
        s.push_str("!");
        s.into()
    }
}

The wit/world.wit file which uses the [IDL(https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md)] that underpins the WebAssembly component model was also changed:

package component:<project-name>;

world example {
    export shout: func(input: string) -> string;
}

Instructions:

  1. Run npm i to install the @bytecodealliance/jco and@bytecodealliance/preview2-shim dependencies aqqqlocally.
  2. Run npm run build to compile the Rust module, invoke JCO and then build a zip file of the code in a folder called dist.
  3. Create a blank Node.js AWS lambda function.
  4. Upload the zip to the lambda function.
  5. Execute the lambda function with the default test event, which should produce the following output:
"VALUE1!"

node-rust-wasm-lambda's People

Contributors

svozza avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.