I currently use a lambda that returns a mock response one out of ten times. It is integrated with AWS API gateway. The lambda implementation is as below:
def lambda_handler(event, context):
body = json.loads(event['body'])
if random.random() < 0.1: // Failure response
response_body = {
"error": "Something went wrong!"
}
return_value = {
"isBase64Encoded": False,
"statusCode": 400,
"headers": {},
"body": json.dumps(response_body)
}
logger.info(return_value)
return return_value
else: // Success response
return_value = {
"isBase64Encoded": False,
"statusCode": 200,
"headers": {},
"body": json.dumps({})
}
logger.info(return_value)
return return_value
Is it possible to achieve this directly using AWS API Gateway mock integration without the use of a lambda?
We tried using VTL in integration request, but the random number generation and wiring the value with response was tricky.
Integration Request
{
#set ($random = $math.getRandom(1,10))
#if($random < 1)
"statusCode" : 400,
#else
"statusCode" : 200,
#end
"body" : "[1,2,3]"
}
Integration Response – Default 200
#set($inputRoot = $input.path('$'))
{
"status" : "success",
"prop" : "$inputRoot"
}
Integration Response – 4xx
HTTP status regex – 4d{2}
{
"statusCode": 400,
"message": "Go ahead without me"
}
New contributor
Vishnupriya Ramanathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.