Panic while generating UUID in node.js runtime

I am writing some code in the rust and compiling it to wasm to run it using node.js runtime for lambda@edge. I need to generate the uuid as part of this code. I am doing that in rust and the code is compiling. But when I run, it is throwing below exception

panicked at 'could not retrieve random bytes for uuid: Node.js crypto module is unavailable', /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/uuid-0.8.2/src/v4.rs:31:13

Stack:

Error
    at _.exports.__wbg_new_693216e109162396 (/Users/user/tech/DataPlatform/RustLambda/build.function/origin-request.js:1:3651)
    at wasm-function[124]:0x10671
    at wasm-function[705]:0x21211
    at wasm-function[223]:0x17ad0
    at wasm-function[327]:0x1bc2e
    at wasm-function[521]:0x1ff7d
    at wasm-function[544]:0x20402
    at wasm-function[445]:0x1e9c4
    at wasm-function[303]:0x1ae09
    at wasm-function[310]:0x1b1fd


(node:36099) UnhandledPromiseRejectionWarning: RuntimeError: unreachable
    at wasm-function[223]:0x17b06
    at wasm-function[327]:0x1bc2e
    at wasm-function[521]:0x1ff7d
    at wasm-function[544]:0x20402
    at wasm-function[445]:0x1e9c4
    at wasm-function[303]:0x1ae09
    at wasm-function[310]:0x1b1fd
    at wasm-function[67]:0x59f1
    at wasm-function[320]:0x1b80b
    at wasm-function[127]:0x10b8f
(Use `node --trace-warnings ...` to show where the warning was created)
(node:36099) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:36099) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here is my code snippet in the rust

        let visitor_id = Uuid::new_v4();
        debug_log!("visitor_id: {:#?}", visitor_id);
    }

I am using uuid crate as

use uuid::Uuid;

and the dependencies are here

uuid = { version = "0.8", features = ["v4", "wasm-bindgen"] }
getrandom = { version = "0.2", features = ["js"] }

Any idea how to proceed with creating the UUID here?

The panic message says "could not retrieve random bytes for uuid: Node.js crypto module is unavailable" and googling the second bit brings me to this section of the API docs for NodeJS's crypto module:

It is possible for Node.js to be built without including support for the crypto module. In such cases, attempting to import from crypto or calling require('crypto') will result in an error being thrown.

Does lambda@edge disable support for crypto APIs? Or alternatively, it might be that they haven't installed OpenSSL in the runtime environment.

You can probably check whether this is an issue with Rust or NodeJS by running a JavaScript script containing require("crypto") in the same environment and seeing if the import fails.

Thanks, Michael. I am doing this work for lambda at the edge, but the error I am getting on my local environment even before deploying the lambda. And the node version on my local is v14.4.0, if that helps. Any idea how can I enable the crypto for this node environment. I tried googling but didn't get much valuable help apart from a few people asking to import it in the application which will not help in this case, as my code is rust based.

I was able to generate the hash and uuid in node js using the below code

const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
    .update('I love cupcakes')
    .digest('hex');
console.log(hash);
var uuid = require('uuid');
console.log(uuid.v4());

Here is the output

c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
4c427c9c-5ee4-4675-a59c-fd55aa73f6c2

But when I tried it using the rust code, it is throwing error.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.