aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.65k stars 3.92k forks source link

lambda : Error deploying docker lambda function #26372

Open ETisREAL opened 1 year ago

ETisREAL commented 1 year ago

Describe the bug

I am trying to deploy a dockerized lambda function.

This is my code:

const leaderboardsStreamHandler = new lambda.Function(this, 'leaderboardsStreamHandler', {
            functionName: 'leaderboardsStreamHandler',
            code: lambda.Code.fromDockerBuild(path.resolve('resources/lambdas-code/leaderboardsStreamHandler')),
            handler: lambda.Handler.FROM_IMAGE,
            runtime: lambda.Runtime.FROM_IMAGE,
            environment: {
                'TABLE_NAME': props.leaderboardsTable.tableName,
                'STAGE': props.STAGE.toLowerCase()
            },
            logRetention: RetentionDays.ONE_WEEK,
            description: 'Lambda function responsible for inserting participants in the Leaderboards table'
        })

The Image build completes succesfully, but I get the error reported below:

Why is this happening? It doesn't make any sense :/

Expected Behavior

I am following the documentation, so it should work just fine TBH

Current Behavior

I get the following error:

Error: handler must be `Handler.FROM_IMAGE` when using image asset for Lambda function
    at verifyCodeConfig (/home/ettore/Documents/Innovyou/Qlash/leaderboards/node_modules/aws-cdk-lib/aws-lambda/lib/function.js:1:22156)
    at new Function (/home/ettore/Documents/Innovyou/Qlash/leaderboards/node_modules/aws-cdk-lib/aws-lambda/lib/function.js:1:7490)
    at new LeaderboardsStreamLambdaStack (/home/ettore/Documents/Innovyou/Qlash/leaderboards/lib/lambdas/leaderboards-stream-lambda-stack.ts:19:43)
    at Object.<anonymous> (/home/ettore/Documents/Innovyou/Qlash/leaderboards/bin/leaderboards.ts:38:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module.m._compile (/home/ettore/Documents/Innovyou/Qlash/leaderboards/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .ts] (/home/ettore/Documents/Innovyou/Qlash/leaderboards/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)

Subprocess exited with error 1

Reproduction Steps

cdk deploy

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.87

Framework Version

No response

Node.js Version

v18.04

OS

Linux - Ubuntu

Language

Typescript

Language Version

No response

Other information

No response

pahud commented 1 year ago

For lambda container runtime, I think you should use DockerImageFunction instead.

ETisREAL commented 1 year ago

No, that's not it. DockerImageFunction is what I should use if I already have the image pushed to ECR. In this case I ma trying to build it on deployment

pahud commented 1 year ago

@ETisREAL DockerImageFunction allows you to specify DockerImageCode for the code property. And you can specify your local docker assets directory with fromImageAssets. On cdk synth, the docker image assets will be built with docker build from the path you specify and published to the staging ECR repo. Is this something you expect?

ETisREAL commented 1 year ago

@pahud As the fllowing code I suppose: new lambda.DockerImageFunction(this, 'leaderboardsStreamHandler', { functionName: 'leaderboardsStreamHandler', code: lambda.DockerImageCode.fromImageAsset(path.resolve('resources/lambdas-code/leaderboardsStreamHandler/code')), environment: { 'TABLE_NAME': props.leaderboardsTable.tableName, 'STAGE': props.STAGE.toLowerCase() }, events: [ new lambda_event_sources.DynamoEventSource(props.leaderboardsTable, { startingPosition: lambda.StartingPosition.TRIM_HORIZON, retryAttempts: 2, batchSize: 1, filters: [ lambda.FilterCriteria.filter({ dynamodb: { Keys: { PK: {S: lambda.FilterRule.beginsWith('PARTICIPANT#')}, SK: {S: lambda.FilterRule.beginsWith('#TOURNAMENT#')} } } }) ] }) ], logRetention: RetentionDays.ONE_WEEK, description: 'Lambda function responsible for creating and updating leaderboards' })