- Newest
- Most votes
- Most comments
Hit error with sample code above :)
Really? It works perfectly for me.
Judging from index.mjs
file you use, do you use ES Modules? My version is index.js
that uses CommonJS module.
I'll give you my full Lambda configuration. There may be some differences between yours.
AWSTemplateFormatVersion: "2010-09-09" Transform: AWS::Serverless-2016-10-31 Description: My serverless stack Resources: HttpRequestFunction: Type: AWS::Serverless::Function Properties: FunctionName: Fn::Sub: ${AWS::StackName}-HttpRequestFunction Description: Send an HTTP request Handler: index.handler Runtime: nodejs18.x Architectures: - arm64 InlineCode: | const http = require("node:http"); exports.handler = async () => { const globalIpAddress = await new Promise((resolve, reject) => { const req = http.request( { hostname: "ifconfig.me", port: 80, path: "/", method: "GET" }, res => { res.on("data", chunk => { const body = chunk.toString(); resolve(body); }); }, ); req.on("error", e => { reject(e.message); }); req.end(); }); return globalIpAddress; };
If you want to send HTTP request with Node.js standard library, http.request should be your friend. Here's a sample implementation:
const http = require("node:http"); exports.handler = async () => { const globalIpAddress = await new Promise((resolve, reject) => { const req = http.request( { hostname: "ifconfig.me", port: 80, path: "/", method: "GET" }, res => { res.on("data", chunk => { const body = chunk.toString(); resolve(body); }); }, ); req.on("error", e => { reject(e.message); }); req.end(); }); return globalIpAddress; };
If you want to use HTTPS instead, then replace http
to https
and port 80
to 443
.
Hello,
From the error it looks like your function is not able to fetch the required dependencies to run the function successfully. This may be because your layer content is not packaged properly. A Lambda layer is a .zip file archive that contains supplementary code or data. Layers usually contain library dependencies (such as axios), a custom runtime, or configuration files.
Please take a look at the following links to get more insights into layers and how to package them. https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html
Make sure your zip contains correct directory structure as mentioned in the above links. Hope this helps.
Relevant content
- asked 8 months ago
- Accepted Answerasked 9 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
cool! it's working now!