Reputation: 33534
I am trying to run a bit modified 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"
Everything works, but when I try to get query string I got null. What necessary config is needed to pass query string paramters to lambda?
Upvotes: 0
Views: 937
Reputation: 33749
You will find the query string parameters in the queryStringParameters
and the multiValueQueryStringParameters
subdocuments of the received event
, e.g.
const queryStringParams = event.queryStringParameters;
console.log(JSON.stringify(queryStringParams));
The documentation of query string parameters and other data that is available can be found in the Input Format of a Lambda Function for Proxy Integration chapter of the Amazon API Gateway developer guide.
Side note, the data available in a Lambda event differs depending on the event source that triggers the Lambda. Sometimes, it is sufficient to just log the incoming event, e.g. console.log(JSON.stringify(event));
.
Upvotes: 1
Reputation: 14462
How exactly are you trying to get query string parameters?
Since you are using proxy integration, HTTP request is sent as is to your lambda function without any modification at API Gateway, meaning that the query string parameters are available through event object - event['queryStringParameters']
I have tried your example and added print statement to the function
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
}
And if I hit that API endpoint, while specifying some query string params such as ?a=1&b=2
, I see that the function is logging those parameters correctly.
CloudWatch logs:
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
Upvotes: 1