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
질문됨 한 달 전173회 조회
1개 답변
0
수락된 답변

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
전문가
답변함 한 달 전
profile picture
전문가
검토됨 한 달 전
profile pictureAWS
전문가
검토됨 한 달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠