运行时.基于Docker映像执行Lambda时的InvalidEntrypoint



我试图使用CDK部署lambda函数,当部署堆栈时,我击中api并在lambda函数中看到以下错误

{
"errorType": "Runtime.InvalidEntrypoint",
"errorMessage": "RequestId: 5afa8a81-6eb3-4293-a57c-e8c6472ddff4 Error: fork/exec /lambda-entrypoint.sh: exec format error"
}

我的lambda函数看起来像

import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda';
export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
console.log(`Event: ${JSON.stringify(event, null, 2)}`);
console.log(`Context: ${JSON.stringify(context, null, 2)}`);
return {
statusCode: 200,
body: JSON.stringify({
message: 'hello world',
}),
};
};

我的Dockerfile看起来像下面

FROM public.ecr.aws/lambda/nodejs:18 as builder
WORKDIR /usr/app
COPY package.json index.ts ./
RUN npm install
RUN npm run build
FROM public.ecr.aws/lambda/nodejs:18
WORKDIR ${LAMBDA_TASK_ROOT}
COPY --from=builder /usr/app/dist/* ./
CMD ["index.handler"]

和我的堆栈部署如下

const fakeFunction = new aws_lambda.DockerImageFunction(this, 'FakerFunction', {
code: aws_lambda.DockerImageCode.fromImageAsset(
path.join(__dirname, '..', '..', 'functions', 'fakedata')
),
});
const integration = new HttpLambdaIntegration('FakerIntegration', fakeFunction);
const httpApi = new apigw2.HttpApi(this, 'HttpApi', {
apiName: 'fake-api',
createDefaultStage: true,
corsPreflight: {
allowMethods: [CorsHttpMethod.GET],
allowOrigins: ['*'],
maxAge: Duration.days(10)
}
});
httpApi.addRoutes({
path: '/fake',
methods: [HttpMethod.GET],
integration: integration
})
new CfnOutput(this, 'API Endpoint', {
value: httpApi.url!
})

我的代码可在https://github.com/hhimanshu/typescript-cdk/tree/h2/api-query。您需要运行cdk deploy来部署此堆栈。

我不知道我错过了什么,我需要做什么来解决这个问题。非常感谢任何帮助。谢谢你

为了解决这个问题,我必须指定Platform。具体来说,我必须使用下面定义的Platform.LINUX_AMD64

import {Platform} from "aws-cdk-lib/aws-ecr-assets";
const fakeFunction = new aws_lambda.DockerImageFunction(this, 'FakerFunction', {
code: aws_lambda.DockerImageCode.fromImageAsset(
path.join(__dirname, '..', '..', 'functions', 'fakedata'),
{
platform: Platform.LINUX_AMD64
}
),
});

最新更新