How to add S3 event as trigger in Lambda API setup (with ALB)?

0

I have Lambda API setup with ALB and it is working fine. Now I want to add s3 event as another trigger so whenever a new object is created in the S3 bucket will call one of the method in LambdaAPI.

Language: .net6 I created Function.cs and added event handler code inside a method FunctionHandler(S3Event evnt, ILambdaContext context) and added the another resource entry in the serverless.template file for S3 event.

"PageGenS3EventFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "Page.Generator.Api.S3EventHandler", "Runtime": "dotnet6", "CodeUri": "", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambda_FullAccess" ]
} }

I got the below error whenever I test the above logic

"Unable to determine header mode, single or multi value, because both Headers and MultiValueHeaders are null."

Please help me how to resolve this issue.

Pandi
질문됨 일 년 전361회 조회
2개 답변
1
수락된 답변

Requests from ALB include X-Amzn-Trace-Id, X-Forwarded-For, X-Forwarded-Port, and X-Forwarded-Proto in the header.
So I thought that the request used for testing should also include X-Amzn-Trace-Id, X-Forwarded-For, X-Forwarded-Port, and X-Forwarded-Proto in the header.
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#receive-event-from-load-balancer

An example of an event would include a header like this.

{
    "requestContext": {
        "elb": {
            "targetGroupArn": "arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/6d0ecf831eec9f09"
        }
    },
    "httpMethod": "GET",
    "path": "/",
    "queryStringParameters": {parameters},
    "headers": {
        "accept": "text/html,application/xhtml+xml",
        "accept-language": "en-US,en;q=0.8",
        "content-type": "text/plain",
        "cookie": "cookies",
        "host": "lambda-846800462-us-east-2.elb.amazonaws.com",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)",
        "x-amzn-trace-id": "Root=1-5bdb40ca-556d8b0c50dc66f0511bf520",
        "x-forwarded-for": "72.21.198.66",
        "x-forwarded-port": "443",
        "x-forwarded-proto": "https"
    },
    "isBase64Encoded": false,
    "body": "request_body"
}

Also, looking at GitHub, I found that the error is generated when both "Headers" and "MultiValueHeaders" are not found in the request.
https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.AspNetCoreServer/ApplicationLoadBalancerFunction.cs#LL65C16-L66C14

            if (lambdaRequest.Headers == null && lambdaRequest.MultiValueHeaders == null)
            {
                throw new Exception("Unable to determine header mode, single or multi value, because both Headers and MultiValueHeaders are null.");
            }
profile picture
전문가
답변함 일 년 전
profile picture
전문가
검토됨 일 년 전
0

Hey Pandi!

I am not aware of .net, but can suggest you with some thoughts.

  • Error was triggered by this method.
  • possible reason -> headers are not being fetched, need to reassign them discussion here
  • There are few other scenario, you can consider using:
    • s3 have default object creation event settings, you can link another lambda or event with it.
    • if you have desire to create event from this lambda function then use sqs, or eventbridge trigger.
    • you can alter cloudwatch alarm or eventbridge rules for more detailed trigger.

There are more scenarios, but these are popular ones.

Hope this help,

profile picture
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠