(AWS CDK)Unable to use Axios in Lambda function

0

This is my cdk structure Enter image description here

This is the Error I got Enter image description here

This my lambda Code Enter image description here

This is my package.json, I have installed esbuild and axios locally Enter image description here

Could anyone help?

RECJ
질문됨 8달 전456회 조회
1개 답변
1

Hi,

You are getting this error because you are trying to use the CDK project's package.json to be used by your Lambda Function too. But, it doesn't work that way.

There are 2 options to achieve this:

  1. You can create a separate runtime environment (i.e. package.json) for your lambda, and then install the "axios" dependency into it. [1]
  2. You can also choose to create a Lambda Layer that includes the "axios" dependency and configure the Lambda function to use the Lambda Layer.

For ease, you can go with the first option. Here's a reference for it: [1]. The folder structure should look like this:

|-- lambdas/
|    |-- package.json
|    |-- index.ts
|-- bin/
|-- lib/
|-- package.json
|-- ...

References:
[1] https://stackoverflow.com/questions/67361096/how-can-i-zip-node-lambda-dependencies-in-aws-cdk-stack
[2] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda-readme.html

Thanks,
Atul

profile picture
답변함 8달 전

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

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

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

관련 콘텐츠