Cannot deploy Nodejs and Typescript to AWS cdk + lambda

Viewed 40

I'm beginner of aws cdk + lambda. I tried to deploy my code but i saw this error.

 => [internal] load build definition from Dockerfile                                                                                                                                                   0.0s
 => => transferring dockerfile: 37B                                                                                                                                                                    0.0s
 => [internal] load .dockerignore                                                                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                                                                        0.0s
 => [internal] load metadata for public.ecr.aws/sam/build-nodejs14.x:latest                                                                                                                            1.0s
 => [1/9] FROM public.ecr.aws/sam/build-nodejs14.x@sha256:06985a76d686d0c86515379f71bed3a1acbab280d661ef551b5b4cda640e8f90                                                                             0.0s
 => CACHED [2/9] RUN npm install --global yarn@1.22.5                                                                                                                                                  0.0s
 => CACHED [3/9] RUN npm install --global pnpm                                                                                                                                                         0.0s
 => CACHED [4/9] RUN npm install --global typescript                                                                                                                                                   0.0s
 => CACHED [5/9] RUN npm install --global --unsafe-perm=true esbuild@0                                                                                                                                 0.0s
 => CACHED [6/9] RUN mkdir /tmp/npm-cache &&     chmod -R 777 /tmp/npm-cache &&     npm config --global set cache /tmp/npm-cache                                                                       0.0s
 => CACHED [7/9] RUN mkdir /tmp/yarn-cache &&     chmod -R 777 /tmp/yarn-cache &&     yarn config set cache-folder /tmp/yarn-cache                                                                     0.0s
 => CACHED [8/9] RUN npm config --global set update-notifier false                                                                                                                                     0.0s
 => CACHED [9/9] RUN /sbin/useradd -u 1000 user && chmod 711 /                                                                                                                                         0.0s
 => exporting to image                                                                                                                                                                                 0.0s
 => => exporting layers                                                                                                                                                                                0.0s
 => => writing image sha256:8b25175ce58bbb3ce58ba4f0b73e8ae17cf0fcb8532a6c02b0aca7b1a7429499                                                                                                           0.0s
 => => naming to docker.io/library/cdk-470fb4012c9ce199d4de1e4462a2a6890905c9a52978baa58741ff003794f41b                                                                                                0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Bundling asset CdkMqHrApiStack/MqHrApiHandler/Code/Stage...
esbuild cannot run locally. Switching to Docker bundling.
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
qemu: uncaught target signal 11 (Segmentation fault) - core dumped

structure

- bin
- build
- cdk.out
- lib
- node_modules
- src
  -- index.ts
- package.json
- package-lock.json

bin/cdk-test.ts file

const app = new cdk.App();
new CdkTestStack(app, 'CdkTestStack');

lib/cdk-test-stack.ts file

export class CdkTestStack extends cdk.Stack {
    constructor(scope: Construct, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        // defines an AWS Lambda resource
        // new lambda.Function(this, 'TestHandler', {
        //  memorySize: 1024,
        //  runtime: lambda.Runtime.NODEJS_14_X, // execution environment
        //  code: lambda.Code.fromAsset('./src'), // code loaded from "lambda" directory
        //  handler: 'index.handler',
        //  functionName: 'TestHandler',
        //  timeout: cdk.Duration.seconds(300),
        // });

        new lambdaNodejs.NodejsFunction(this, 'TestHandler', {
            runtime: lambda.Runtime.NODEJS_14_X,
            handler: 'handler',
            entry: './build/index.js',
        });
    }
}

src/index.ts file

export const handler = ServerlessAdapter.new(app).setFramework(new FastifyFramework()).build();

I use mac m1 and cdk version 2.42. I don't know why i can't deploy. Please, help me.

2 Answers

Try using the platform command and see if you can get rid of the error. A couple of things you might try; --platform linux/amd64 --platform linux/arm64/v8 You could also try building inside a docker image that is the same kind of machine as your image you're building so you avoid the conflict with the platform types.

Do you have esbuild in your package.json as a devDependency?

Ideally, you should not be using Docker to build the Lambda, but instead using esbuild. That will circumvent the Docker/M1 issues.

Related