2

I've created a new .NET 5 AWS Lambda Function using Container Images via the Visual Studio Project Template:

And now I want to deploy the Lambda Function and an ApiGateway using the CDK.

I was able to get it deployed, but when I invoke the method, I get this error:

❯ iwr https://3l0xxxxxx.execute-api.us-west-2.amazonaws.com/prod/
Invoke-WebRequest: {"message": "Internal server error"}

This is what I have in my Stack:

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
        });
    }
}

How do I fix my CDK Stack code so my Lambda + Api Gateway deploy correctly?

5
  • 1
    Did you check cloudwatch logs for any lambda error messages? Commented Feb 27, 2021 at 5:17
  • put a try catch and see the exception details Commented Feb 27, 2021 at 5:18
  • 1
    @Marcin - yup, needed to check my CloudWatch logs, turns out my Function Handler wasn't defined. Initially I wasn't sure where to put it as the CDK would error out if I set FunctionProps.Handler directly. Commented Feb 27, 2021 at 5:26
  • @viveknuna - Didn't try putting a try/catch in my lambda application code. Normally tha's a good place to start, though I'm guessing with what turned out to be my issue (missing function handler) it wouldn't have helped so much as Lambda didn't even start to execute my function code so the try/catch wouldn't even get hit. Commented Feb 27, 2021 at 5:28
  • @PhilipPittle good you solved the issue Commented Feb 27, 2021 at 5:32

1 Answer 1

5

Realized I could mine my CloudWatch logs to get to the real problem - Lambda wants a function handler defined:

enter image description here

I was able to pass a AssetImageCodeProps, with my handler set in hte Cmd property, to the Code.FromAssetImage and that got it working.

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
    });
}

After a deploy:

❯ iwr https://3l0xxxxxx.execute-api.us-west-2.amazonaws.com/prod/

StatusCode        : 200
StatusDescription : OK
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.