我通过无服务器框架使用API Gateway和AWS Lambda来创建API终端节点。默认情况下,lambda函数被部署为lambda代理。
[当我向我的一个端点发送POST
请求时,我会包含自定义标头,例如client_version: 1.0.0
。
[现在发生了奇怪的事情,当向端点发出请求时,Lambda函数并不总是获得标头client_version
。这种情况主要发生在将服务部署到aws之后的几分钟,一段时间之后,lambda函数接收到标头。但是有时它会收到标头,然后在以后的某个时间不再接收标头。
我测试了从我的机器和在线服务向单个端点发出的请求,并且看起来它是完全独立的。
意思是我的机器POST
请求可能会发生奇怪的错误,而在线服务POST
请求成功设法将标头传递给lambda函数,或者同时或同时传递给lambda函数。
这使我发疯,因为我根本不知道问题可能是什么,我需要将自定义标头始终发送到端点。任何帮助表示赞赏。
无服务器配置
serverless.yml:
custom: ${file(serverless_config/custom.yml):custom}
functions: ${file(serverless_config/functions.yml):functions}
resources: ${file(serverless_config/resources.yml):resources}
service: "${self:custom.WEBSERVICE_NAME}"
provider:
name: aws
profile: "${self:custom.CURRENT_PROFILE}"
stage: "${self:custom.CURRENT_DEPLOY_MODE}"
region: "${self:custom.AWS_DEPLOY_REGION}"
runtime: "${self:custom.AWS_LAMBDA_RUNTIME}"
memorySize: ${self:custom.AWS_LAMBDA_MEMORY_SIZE}
timeout: "${self:custom.AWS_LAMBDA_TIMEOUT}"
logRetentionInDays: ${self:custom.AWS_CLOUDWATCH_LOG_TTL}
environment: ${file(serverless_config/environment.yml):environment}
functions.yml
functions:
Account-Create:
role: DefaultLambdaIAMRole
handler: src/v1/Account/Create/Email/main.main
events:
- http:
path: v${self:custom.CURRENT_API_VERSION}/account/create-email
method: POST
custom.yml:
custom:
WEBSERVICE_NAME: account-service
WEBSERVICE_ENDPOINT: "api.accountservice.testing.galaxgate.com"
DEFAULT_DEPLOY_MODE: dev
stage: "${opt:stage, self:custom.DEFAULT_DEPLOY_MODE}"
CURRENT_DEPLOY_MODE: "${self:custom.stage}"
CURRENT_PROFILE: galaxfinity
CURRENT_API_VERSION: "1"
WEBSERVICE_PREFIX: "${self:custom.WEBSERVICE_NAME}-${self:custom.CURRENT_DEPLOY_MODE}-"
AWS_ACCOUNT_ID: "XXXXXXXXX"
AWS_DEPLOY_REGION: eu-central-1 # dep: _config.js
AWS_LAMBDA_RUNTIME: nodejs12.x
AWS_LAMBDA_TIMEOUT: 30
AWS_LAMBDA_MEMORY_SIZE: 512
IAM_LAMBDA_POLICY_NAME: "${self:custom.WEBSERVICE_PREFIX}policy-lambda"
IAM_LAMBDA_ROLE_NAME: "${self:custom.WEBSERVICE_PREFIX}role-lambda"
AWS_CLOUDWATCH_LOG_TTL: 180 # in days
DB_TABLE_DELETION_POLICY_VALUES:
dev: Delete
prod: Retain
DB_TABLE_DELETION_POLICY: ${self:custom.DB_TABLE_DELETION_POLICY_VALUES.${self:custom.stage}}
S3_BUCKET_DELETION_POLICY_VALUES:
dev: Delete
prod: Retain
S3_BUCKET_DELETION_POLICY: ${self:custom.S3_BUCKET_DELETION_POLICY_VALUES.${self:custom.stage}}
customDomain:
endpointType: 'regional'
securityPolicy: tls_1_2
domainName: '${self:custom.WEBSERVICE_ENDPOINT}'
certificateName: '${self:custom.WEBSERVICE_ENDPOINT}'
basePath: '${self:custom.CURRENT_DEPLOY_MODE}'
stage: ${self:custom.CURRENT_DEPLOY_MODE}
createRoute53Record: true
一些图片
相同的请求结果导致不同的结果。
UPDATE 1我能够直接从API网关输入获取日志。似乎API Gateway甚至没有收到自定义标头。仍然不敢相信邮递员会造成麻烦。
Method request headers: {Accept=*/*, Cache-Control=no-cache, User-Agent=PostmanRuntime/7.24.1, X-Forwarded-Proto=https, X-Forwarded-For=5.147.136.132, Host=api.accountservice.testing.galaxgate.com, Postman-Token=9236b95a-3cef-4e04-a188-38e996122811, Accept-Encoding=gzip, deflate, br, X-Forwarded-Port=443, X-Amzn-Trace-Id=Root=1-5ec30f6a-6e719a6a7cc4e1c12e54c030, Content-Type=text/plain}
UPDATE 2
在这里您可以看到这一切的怪异。我使用reqbin.com发送两个请求,一个来自美国服务器,另一个来自DE服务器。服务器应以PAYLOAD_INVALID
响应。但是,如果未设置头字段client_version
,它将以CLIENT_VERSION_MISSING
进行响应。
自从头字段在通过互联网发送时神秘地消失了吗?
大最终更新
我找到了解决方案:原来,将自定义标头client_version
更改为X-Client-Version
可以解决问题,并且标头始终被接收。我不知道为什么这样的事情会影响API网关的功能,但是不能正常使用解决方案。
您需要在cors上设置自定义标头https://www.serverless.com/framework/docs/providers/aws/events/apigateway#enabling-cors
例如:
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
cors:
origin: '*'
headers:
- Content-Type
- X-Amz-Date
- Authorization
- client_version
allowCredentials: false