question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LambdaRestApi throws an error in C#

See original GitHub issue

Describe the bug I’ve seen a new error using CDK 0.29 and 0.30 in .Net. Using LambdaRestApi throws an error.

“principal: required but missing”

To Reproduce This code:

var api = new LambdaRestApi(this, "myrestapi",
    new LambdaRestApiProps
    {
        Handler = lambda
    });

gives me this error: While synthesizing stack-name/lambda-name/ApiPermission.ANY…: Supplied properties not correct for “CfnPermissionProps” principal: required but missing

I could not reproduce using Typescript so I believe it’s a .Net specific issue.

My collegue made a work around by recreating the CustomLambdaIntegration in C# and using that directly.

Expected behavior Should be able to use LambdaRestApi to create an Lambda integration with API Gateway

Version:

  • Windows 10
  • C#
  • 0.29 and 0.30

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
wmmageecommented, May 21, 2019

@Kylia669, we created a line for line remake of the CustomLambdaIntegration class but in C# (see below). Then we used the RestApi construct and set it’s default integration to an instance of our CustomLambdaIntegration class.

CustomLambdaIntegration (it’s private because we implemented it inside the class where we used it and expect to remove our implementation when this bug is fixed)

private class CustomLambdaIntegration : AwsIntegration
{
    private readonly IIFunction handler;
    private readonly bool enableTest;

    public CustomLambdaIntegration(IIFunction handler, ILambdaIntegrationOptions options)
        : base(new AwsIntegrationProps
        {
            Proxy = options.Proxy ?? true,
            Service = "lambda",
            Path = $"2015-03-31/functions/{handler.FunctionArn}/invocations",
            Options = options
        })
    {
        this.handler = handler;
        this.enableTest = options.AllowTestInvoke == null ? true : false;
    }

    public override void Bind(Method method)
    {
        base.Bind(method);

        var principal = new ServicePrincipal("apigateway.amazonaws.com", new ServicePrincipalOpts());
        string description = $"{method.HttpMethod}.{method.Resource.Path.Replace('/', '.')}";

        this.handler.AddPermission($"ApiPermission.{description}", new Permission
        {
            Principal = principal,
            SourceArn = method.MethodArn
        });

        if (this.enableTest)
        {
            this.handler.AddPermission($"ApiPermission.Test.{description}", new Permission
            {
                Principal = principal,
                SourceArn = method.TestMethodArn
            });
        }
    }
}

Then, in place of using a LambdaRestApi we used a RestApi in the following manor

var proxyHandlerBackend = new CustomLambdaIntegration(proxyHandler.Build(stack), new LambdaIntegrationOptions());
restApi = new RestApi(stack, this.name, new RestApiProps
{
     DefaultIntegration = proxyHandlerBackend
});
0reactions
eladbcommented, Jul 2, 2019

Thanks for letting us know. Closing for now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle Lambda errors in API Gateway - AWS Documentation
If there is no match, API Gateway returns the error as a default response or throws an invalid configuration exception if no default...
Read more >
Automatically deploy REST APIs with Lambda authorizers ...
Developer C sits at a desk working on an intermediate-level project. ... catch (error) { throw Error(`Error in backend: ${error}`); ...
Read more >
Error Handling in AWS Lambda and API Gateway - Medium
If createDeal() throws an error that is not caught and rethrown we will not break the gateway handling. serverless.yml: createDeal: handler: ...
Read more >
The Missing Guide to AWS API Gateway Access Logs
Debugging: If you get a spike in 500 Internal Server Error responses, ... required auth header or if your custom authorizer throws an...
Read more >
How to handle Lambda errors in API Gateway - Fathom
Best practices for handling errors in AWS Lambda functions when working with API ... we can't simply throw an error back to API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found