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
posta un anno fa360 visualizzazioni
2 Risposte
1
Risposta accettata

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
ESPERTO
con risposta un anno fa
profile picture
ESPERTO
verificato un anno fa
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
con risposta un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande