I’m having trouble passing query string parameters to my AWS API Gateway and getting them processed correctly by my Lambda function. Despite following several tutorials and guides, my Lambda function consistently returns an error indicating that the official_name parameter is missing.
Thank you in advance, newbie trying to learn Serverless AWS and I am stuck.
Website>API Gateway>Lambda>DynamoDB
Setup Details:
API Gateway:
I have set up a GET method in API Gateway.
Under Method Request, I’ve added a query string parameter official_name and marked it as required.
Under Integration Request, I have configured a mapping template for application/json as follows:
{
"official_name": "$input.params('official_name')"
}
enter image description here
The API is deployed to a stage named deploy.
Lambda Function:
My Lambda function is designed to handle the incoming request and return HTML content based on the official_name parameter.
import json
import boto3
def lambda_handler(event, context):
dynamodb = boto3.client('dynamodb')
s3 = boto3.client('s3')
# Check if 'queryStringParameters' exists and is not None
query_params = event.get('queryStringParameters', {})
# Get 'official_name' from query string parameters
official_name = query_params.get('official_name', '')
if not official_name:
return {
'statusCode': 400,
'body': json.dumps({'error': 'official_name parameter is required'}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-key'
}
}
print(f"Official Name from request: {official_name}")
try:
response = dynamodb.get_item(
TableName='GlobalSkiAtlasDatabase',
Key={
'OfficalName': {'S': official_name}
}
)
print(f"DynamoDB Response: {response}")
if 'Item' in response:
item = response['Item']
formatted_item = {k: list(v.values())[0] for k, v in item.items()}
print(f"Formatted Item: {formatted_item}")
skiable_acres = formatted_item.get('Skiable Acres', 'N/A')
print(f"Skiable Acres: {skiable_acres}")
s3_bucket = 'globalskiatlas.com'
s3_key = 'skiresort.html'
try:
s3_response = s3.get_object(Bucket=s3_bucket, Key=s3_key)
html_template = s3_response['Body'].read().decode('utf-8')
except Exception as e:
print(f"Error fetching HTML template from S3: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-key'
}
}
html_content = html_template.format(
resort_name=formatted_item.get('Name', 'N/A'),
owner=formatted_item.get('Owner', 'N/A'),
ticket_price_youth=formatted_item.get('Ticket Price Youth', 'N/A'),
ticket_price_adult=formatted_item.get('Ticket Price Adult', 'N/A'),
ticket_price_senior=formatted_item.get('Ticket Price Senior', 'N/A'),
rental_price=formatted_item.get('Rental', 'N/A'),
multi_mountain_passes=formatted_item.get('Multi-Mountain Pass', 'N/A'),
major_cities=formatted_item.get('Major Cities', 'N/A'),
vertical_drop=formatted_item.get('Vertical Drop', 'N/A'),
base_altitude=formatted_item.get('Base Altitude', 'N/A'),
peak_altitude=formatted_item.get('Peak Altitude', 'N/A'),
trails=formatted_item.get('Trails Total', 'N/A'),
lifts=formatted_item.get('Lifts', 'N/A'),
skiable_acres=skiable_acres,
longest_run=formatted_item.get('Longest Run', 'N/A'),
snow_making=formatted_item.get('Snow Making', 'N/A'),
annual_snowfall=formatted_item.get('Annual Snowfall', 'N/A'),
operating_hours_weekdays=formatted_item.get('Operating Hours: Weekdays', 'N/A'),
operating_hours_weekends=formatted_item.get('Operating Hours: Weekends', 'N/A'),
opening_date=formatted_item.get('Opening Date', 'N/A'),
closing_date=formatted_item.get('Closing Date', 'N/A'),
days_open=formatted_item.get('Days Open', 'N/A'),
resort_lodging=formatted_item.get('Resort Lodging', 'N/A'),
night_skiing=formatted_item.get('Night Skiing', 'N/A'),
terrain_park=formatted_item.get('Terrain Park', 'N/A'),
tubing_park=formatted_item.get('Tubing Park', 'N/A'),
dining=formatted_item.get('Dining', 'N/A'),
resort_description=formatted_item.get('Opening Paragraph', 'N/A'),
resort_image_url=formatted_item.get('ImageURL', 'N/A'),
extra_image_url=formatted_item.get('Extra Image URL', 'N/A'),
video_url=formatted_item.get('Video', 'N/A')
)
return {
'statusCode': 200,
'body': html_content,
'headers': {
'Content-Type': 'text/html',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-key'
}
}
else:
return {
'statusCode': 404,
'body': json.dumps({'error': 'Ski resort not found'}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-key'
}
}
except Exception as e:
print(f"Error fetching item from DynamoDB: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-key'
}
}
enter image description here
Output
enter image description here
Problem:
When I test the API using Postman or a simple HTML page, I get the following response from my Lambda function:
json
{
"statusCode": 400,
"body": "{"error": "official_name parameter is required"}",
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, x-api-key"
}
}
Steps Taken:
Configured API Gateway:
Added official_name as a required query string parameter under Method Request.
Configured Integration Request with a mapping template to pass the official_name parameter to the Lambda function.
Tested with Postman:
Sent a GET request to
“
https://sjtdu9sez2.execute-api.us-east-1.amazonaws.com/deploy?official_name=Whitetail%20Resort
“
Received the 400 error indicating the parameter is missing.
enter image description here
Could anyone help me identify what I might be missing or doing wrong? How can I properly pass the official_name query string parameter to my Lambda function through API Gateway?