how to use AWS SAM on NodeJs Lambda by Gitlab-ci?

201 Views Asked by At

My gitlab-ci.yml:

build_aws_sam:
  image: python:3.11
  stage: build
  before_script:
    - pip3 install awscli --upgrade
    - pip3 install aws-sam-cli --upgrade
  script:
    - sam build --config-env "dev"

Error:

Building codeuri: /builds/xxxx-aws/xxxx-application/serverless/src/populateDynamo runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ServerlessDynamoStack/PopulateDynamoFunction
package.json file not found. Continuing the build without dependencies.
Build Failed
Error: NodejsNpmBuilder:Resolver - Path resolution for runtime: nodejs18.x of binary: npm was not successful

I try add this in before_script but same error:

    - pip3 install nodejs-bin --upgrade
    - pip3 install npm --upgrade

I read AWS official documentation for aws-sam-cli and we need use Python image.

2

There are 2 best solutions below

0
On BEST ANSWER

Solution work for me: - apt-get install nodejs -yq but after a curl on https://deb.nodesource.com/setup_18.x

full gitlab-ci file:

build_aws_sam:
  image: python:3.11
  stage: build
  before_script:
    - pip3 install awscli --upgrade
    - pip3 install aws-sam-cli --upgrade
    - apt-get update -y
    - curl -sL https://deb.nodesource.com/setup_18.x | bash
    - apt-get install nodejs -yq
  script:
    - sam build --config-env "dev"
3
On

One of the prebuilt AWS SAM images can be used. For Node.js:

Node.js 20  public.ecr.aws/sam/build-nodejs20.x
Node.js 18  public.ecr.aws/sam/build-nodejs18.x
Node.js 16  public.ecr.aws/sam/build-nodejs16.x
Node.js 14  public.ecr.aws/sam/build-nodejs14.x
Node.js 12  public.ecr.aws/sam/build-nodejs12.x

All images can be found here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-image-repositories.html#serverless-image-repository-uris

The .gitlab-ci.yml will look like this:

build_aws_sam:
  image: public.ecr.aws/sam/build-nodejs20.x
  stage: build
  script:
    - sam build
    - sam deploy

The Dockerfiles for the AWS SAM images above can be found here: https://github.com/aws/aws-sam-build-images/tree/develop/build-image-src