By using AWS re:Post, you agree to the AWS re:Post Terms of Use

从java客户端向lambda发送请求,自定义请求头是否可以被读取到

0

根据lambda sdk手册: https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/AmazonWebServiceRequest.html#putCustomRequestHeader-java.lang.String-java.lang.String-,对putCustomRequestHeader方法的描述如下: Put a new custom header to the map of custom header names to custom header values, and return the previous value if the header has already been set in this map. Any custom headers that are defined are used in the HTTP request to the AWS service. These headers will be silently ignored in the event that AWS does not recognize them.

NOTE: Custom header values set via this method will overwrite any conflicting values coming from the request parameters.

是否意说明aws无法识别的请求头将不会被传递?

我的代码如下: java客户端: InvokeRequest invokeRequest = new InvokeRequest().withFunctionName(functionName).withPayload(payload); invokeRequest.putCustomRequestHeader("X-B3-TraceId","abcd1234")

lambda上使用go语言+iris框架获取请求头: logs.Info("X-B3-TraceId:" + ctx.GetHeader("X-B3-TraceId"))

注意:在windows环境启动go服务,使用postman进行测试,go语言服务端能收到这个请求头,在lambda上这个请求头是否被过滤掉了?

asked a month ago30 views
2 Answers
0

根据您提供的信息和问题,我可以解释一下自定义请求头在AWS Lambda中的处理情况:

  1. AWS SDK的说明确实提到,未被AWS识别的自定义请求头将被静默忽略。这意味着某些自定义请求头可能不会被传递到Lambda函数。

  2. 然而,在Lambda函数中,特别是使用API Gateway或Application Load Balancer作为触发器时,许多自定义HTTP头是可以被传递和读取的。

  3. 对于直接通过AWS SDK调用Lambda的情况(如您的Java代码示例),情况可能有所不同。Lambda服务可能不会直接传递所有自定义请求头。

  4. 在您的特定案例中,"X-B3-TraceId"是一个常用于分布式追踪的标准头,AWS可能会识别并传递它。但是,这并不能保证在所有情况下都会发生。

  5. 要确定Lambda是否接收到这个特定的请求头,您需要在Lambda函数中尝试读取它。如果使用API Gateway,您可能需要配置它来传递自定义头。

  6. 如果您在Lambda中无法读取到这个头,可以考虑将其作为函数调用的payload的一部分传递,或使用AWS X-Ray等服务进行分布式追踪。

建议您在Lambda函数中实际测试一下,看是否能读取到这个请求头。如果无法读取,可能需要考虑其他方式来传递这个信息。
Sources
ResponseHeadersPolicyCustomHeader - Amazon CloudFront
OriginCustomHeader - Amazon CloudFront
HttpHeader - Amazon Cognito User Pools

profile picture
answered a month ago
0

根据您提供的信息和问题,我可以解释一下AWS Lambda是如何处理自定义请求头的:

AWS SDK的文档确实提到,未被识别的自定义请求头将被静默忽略。这意味着一些自定义请求头可能无法传递到Lambda函数。

但是,在由API Gateway或应用程序负载均衡器触发的Lambda函数中,许多自定义HTTP头可以被传递和读取。 https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

而对于直接通过AWS SDK调用Lambda的情况(如您的Java代码示例),情况可能有所不同。Lambda服务可能不会直接传递所有自定义请求头。

在您的具体案例中,"X-B3-TraceId"是一个常用于分布式跟踪的标准头,AWS可能会识别并传递它。但是,这在所有情况下都无法得到保证。要确定Lambda是否接收到此特定请求头,您需要在Lambda函数中尝试读取它。

如果使用API Gateway,您可能需要配置它来传递自定义头。

对于API Gateway Lambda代理集成: 所有请求头(包括自定义头)都会自动传递到Lambda函数,无需任何额外配置。 Lambda函数可以从Lambda事件负载中的event.headers对象访问这些头。

对于API Gateway Lambda非代理集成: 自定义头不会自动传递到Lambda函数。 您需要在API Gateway中配置映射模板,以明确将自定义头映射到Lambda函数的输入负载。 请参阅以下链接获取操作步骤 https://repost.aws/knowledge-center/custom-headers-api-gateway-lambda https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-lambda-non-proxy-integration.html

如果您无法在Lambda函数中读取此头,可以考虑将其作为函数调用负载的一部分传递,或使用AWS X-Ray等服务进行分布式跟踪。

建议您在Lambda函数中测试读取此请求头。如果无法读取,您可能需要考虑其他方式来传递此信息。


Based on the information and question you provided, I can explain how custom request headers are handled in AWS Lambda:

The AWS SDK documentation indeed mentions that unrecognized custom request headers will be silently ignored. This means that some custom request headers may not be passed to the Lambda function.

However, in Lambda functions, especially when triggered by API Gateway or Application Load Balancer, many custom HTTP headers can be passed and read. https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

For cases where Lambda is invoked directly through the AWS SDK (like your Java code example), the situation may be different. The Lambda service may not directly pass all custom request headers.

In your specific case, "X-B3-TraceId" is a standard header commonly used for distributed tracing, and AWS may recognize and pass it along. However, this cannot be guaranteed in all cases.To determine if Lambda is receiving this specific request header, you need to try reading it within your Lambda function.

If using API Gateway, you may need to configure it to pass the custom header.

For API Gateway Lambda Proxy Integration: All request headers, including custom headers, are automatically passed through to the Lambda function without any additional configuration required. The Lambda function can access these headers from the event.headers object in the Lambda event payload.

For API Gateway Lambda Non-Proxy Integration: Custom headers are not automatically passed through to the Lambda function. You need to configure a mapping template in API Gateway to explicitly map the custom headers to the Lambda function's input payload. Please find steps to do so in the below link https://repost.aws/knowledge-center/custom-headers-api-gateway-lambda https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-lambda-non-proxy-integration.html

If you cannot read this header in your Lambda function, consider passing it as part of the function's invocation payload or use services like AWS X-Ray for distributed tracing.

It is recommended that you test reading this request header within your Lambda function. If you cannot read it, you may need to consider alternative ways to pass this information.

AWS
SUPPORT ENGINEER
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions