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
asked a month ago161 views
1 Answer
0
Accepted Answer

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
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
profile pictureAWS
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions