使用 CDK 部署带有容器映像的 .NET 5 AWS Lambda:内部服务器错误
Deploy .NET 5 AWS Lambda with Container Images using CDK: Internal Server Error
我通过 Visual Studio 项目模板创建了一个新的 .NET 5 AWS Lambda Function using Container Images:
现在我想使用 CDK 部署 Lambda 函数和 Api网关。
我能够部署它,但是当我调用该方法时,我得到了这个错误:
❯ iwr https://3l0xxxxxx.execute-api.us-west-2.amazonaws.com/prod/
Invoke-WebRequest: {"message": "Internal server error"}
这是我的堆栈中的内容:
public class Dotnet5LambdaStack : Stack
{
internal Dotnet5LambdaStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var dotnet5Lambda = new Amazon.CDK.AWS.Lambda.Function(this, "dotnet5Lambda", new FunctionProps
{
Runtime = Runtime.FROM_IMAGE,
// relative to the cdk.json file
Code = Code.FromAssetImage("src/lambdaNet5"),
Handler = Handler.FROM_IMAGE
});
new LambdaRestApi(this, "dotnet5ApiEndpoint", new LambdaRestApiProps
{
Handler = dotnet5Lambda
});
}
}
如何修复我的 CDK 堆栈代码以便我的 Lambda + Api 网关正确部署?
意识到我可以挖掘我的 CloudWatch 日志来解决真正的问题 - Lambda 需要定义函数处理程序:
我能够将 AssetImageCodeProps
与我在 hte Cmd
属性 中设置的处理程序传递给 Code.FromAssetImage
并使其正常工作。
internal Dotnet5LambdaStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var dotnet5Lambda = new Amazon.CDK.AWS.Lambda.Function(this, "dotnet5Lambda", new FunctionProps
{
Runtime = Runtime.FROM_IMAGE,
// relative to the cdk.json file
Code = Code.FromAssetImage("src/lambdaNet5", new AssetImageCodeProps
{
// !!Set Handler Here!! Assembly::Type::Method
Cmd = new string[]{"lambdaNet5::lambdaNet5.Functions::Get"}
}),
Handler = Handler.FROM_IMAGE
});
new LambdaRestApi(this, "dotnet5ApiEndpoint", new LambdaRestApiProps
{
Handler = dotnet5Lambda
});
}
部署后:
❯ iwr https://3l0xxxxxx.execute-api.us-west-2.amazonaws.com/prod/
StatusCode : 200
StatusDescription : OK
我通过 Visual Studio 项目模板创建了一个新的 .NET 5 AWS Lambda Function using Container Images:
现在我想使用 CDK 部署 Lambda 函数和 Api网关。
我能够部署它,但是当我调用该方法时,我得到了这个错误:
❯ iwr https://3l0xxxxxx.execute-api.us-west-2.amazonaws.com/prod/
Invoke-WebRequest: {"message": "Internal server error"}
这是我的堆栈中的内容:
public class Dotnet5LambdaStack : Stack
{
internal Dotnet5LambdaStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var dotnet5Lambda = new Amazon.CDK.AWS.Lambda.Function(this, "dotnet5Lambda", new FunctionProps
{
Runtime = Runtime.FROM_IMAGE,
// relative to the cdk.json file
Code = Code.FromAssetImage("src/lambdaNet5"),
Handler = Handler.FROM_IMAGE
});
new LambdaRestApi(this, "dotnet5ApiEndpoint", new LambdaRestApiProps
{
Handler = dotnet5Lambda
});
}
}
如何修复我的 CDK 堆栈代码以便我的 Lambda + Api 网关正确部署?
意识到我可以挖掘我的 CloudWatch 日志来解决真正的问题 - Lambda 需要定义函数处理程序:
我能够将 AssetImageCodeProps
与我在 hte Cmd
属性 中设置的处理程序传递给 Code.FromAssetImage
并使其正常工作。
internal Dotnet5LambdaStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var dotnet5Lambda = new Amazon.CDK.AWS.Lambda.Function(this, "dotnet5Lambda", new FunctionProps
{
Runtime = Runtime.FROM_IMAGE,
// relative to the cdk.json file
Code = Code.FromAssetImage("src/lambdaNet5", new AssetImageCodeProps
{
// !!Set Handler Here!! Assembly::Type::Method
Cmd = new string[]{"lambdaNet5::lambdaNet5.Functions::Get"}
}),
Handler = Handler.FROM_IMAGE
});
new LambdaRestApi(this, "dotnet5ApiEndpoint", new LambdaRestApiProps
{
Handler = dotnet5Lambda
});
}
部署后:
❯ iwr https://3l0xxxxxx.execute-api.us-west-2.amazonaws.com/prod/
StatusCode : 200
StatusDescription : OK