Making an HTTPS request from a Lambda (nodejs)

0

I'm new to AWS lambda and am using nodejs v20 and am trying to write a very simple lambda at the moment:

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand, GetCommand } from "@aws-sdk/lib-dynamodb";

import { https } from "https";

export const handler = async (event, context, callback) => {
// 1. Make an https.request() method call based on data in “event”
// 2. Based on the result, log something to DynamoDB  
// 3. Reply to client with result 
const response = {
 statusCode: 200,
  body: "..."  };
  return response;
};

The line import { https } from "https"; seems to be causing the below error. I also tried using const https = require('https'); and also const https = require('nodejs:https'); but I get the exact same error:

2024-04-29T06:19:51.669Z	undefined	ERROR	Uncaught Exception 	
{
    "errorType": "Runtime.UserCodeSyntaxError",
    "errorMessage": "SyntaxError: Unexpected token '*'",
    "stack": [
        "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '*'",
        "    at _loadUserApp (file:///var/runtime/index.mjs:1084:17)",
        "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
        "    at async start (file:///var/runtime/index.mjs:1282:23)",
        "    at async file:///var/runtime/index.mjs:1288:1"
    ]
}

Mind that I have not even written any code, I'm not able to import the https library to make the query to begin with. If I comment out this line, then the lambda runs with no issues. Would greatly appreciate some guidance in how I can import the https library so I can make an https.request() function call.

Gavin
preguntada hace un mes184 visualizaciones
1 Respuesta
0
Respuesta aceptada

Hello.

How about trying the following?

import * as https from "https";

If you only use https objects, I think the following import method will also work.

import https from "https";
profile picture
EXPERTO
respondido hace un mes
profile picture
EXPERTO
revisado hace un mes
profile pictureAWS
EXPERTO
revisado hace un mes

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas