gpt4 book ai didi

amazon-web-services - 如何在aws gateway lambda集成中获取查询字符串

转载 作者:行者123 更新时间:2023-12-03 07:32:03 25 4
gpt4 key购买 nike

我正在尝试运行一些修改的example :

AWSTemplateFormatVersion: "2010-09-09"
Description: "My API Gateway and Lambda function"

Parameters:
apiGatewayStageName:
Type: "String"
AllowedPattern: "^[a-z0-9]+$"
Default: "call"

lambdaFunctionName:
Type: "String"
AllowedPattern: "^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$"
Default: "my-function"

Resources:
apiGateway:
Type: "AWS::ApiGateway::RestApi"
Properties:
Name: "my-api"
Description: "My API"

apiGatewayRootMethod:
Type: "AWS::ApiGateway::Method"
Properties:
AuthorizationType: "NONE"
HttpMethod: "GET"
Integration:
IntegrationHttpMethod: "POST"
Type: "AWS_PROXY"
Uri: !Sub
- "arn:aws:apigateway:${AWS::Region}:lambda:path/functions/${lambdaArn}/invocations"
- lambdaArn: !GetAtt "lambdaFunction.Arn"
ResourceId: !GetAtt "apiGateway.RootResourceId"
RestApiId: !Ref "apiGateway"

apiGatewayDeployment:
Type: "AWS::ApiGateway::Deployment"
DependsOn:
- "apiGatewayRootMethod"
Properties:
RestApiId: !Ref "apiGateway"
StageName: !Ref "apiGatewayStageName"

lambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Code:
ZipFile: |
def handler(event,context):
return {
'body': 'Hello there {0}'.format(event['requestContext']['identity']['sourceIp']),
'headers': {
'Content-Type': 'text/plain'
},
'statusCode': 200
}
Description: "My function"
FunctionName: !Ref "lambdaFunctionName"
Handler: "index.handler"
MemorySize: 128
Role: !GetAtt "lambdaIAMRole.Arn"
Runtime: "python2.7"
Timeout: 10

lambdaApiGatewayInvoke:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !GetAtt "lambdaFunction.Arn"
Principal: "apigateway.amazonaws.com"
SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/*/GET/"

lambdaIAMRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "sts:AssumeRole"
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Policies:
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Effect: "Allow"
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:*"
PolicyName: "lambda"

lambdaLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
LogGroupName: !Sub "/aws/lambda/${lambdaFunctionName}"
RetentionInDays: 90

Outputs:
apiGatewayInvokeURL:
Value: !Sub "https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/${apiGatewayStageName}"

lambdaArn:
Value: !GetAtt "lambdaFunction.Arn"

一切正常,但是当我尝试获取查询字符串时,我得到了空值。将查询字符串参数传递给 lambda 需要什么必要的配置?

最佳答案

您究竟如何尝试获取查询字符串参数?

由于您使用代理集成,HTTP 请求将按原样发送到您的 lambda 函数,无需在 API Gateway 进行任何修改,这意味着查询字符串参数可通过事件对象 - event['queryStringParameters']

我已经尝试了你的示例并向函数添加了 print 语句

def lambda_handler(event,context):
print('query string params', event['queryStringParameters'])
return {
'body': 'Hello there {0}'.format(event['requestContext']['identity']['sourceIp']),
'headers': {
'Content-Type': 'text/plain'
},
'statusCode': 200
}

如果我点击该 API 端点,同时指定一些查询字符串参数,例如 ?a=1&b=2,我会看到该函数正在正确记录这些参数。

CloudWatch 日志:

START RequestId: 18f343c8-55ff-4b26-8d74-ae81ce90e8de Version: $LATEST
query string params {'a': '1', 'b': '2'}
END RequestId: 18f343c8-55ff-4b26-8d74-ae81ce90e8de
REPORT RequestId: 18f343c8-55ff-4b26-8d74-ae81ce90e8de Duration: 20.58 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 53 MB

关于amazon-web-services - 如何在aws gateway lambda集成中获取查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56945686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com