Bird
0
0

Which handler code correctly implements this?

hard📝 Best Practice Q15 of 15
AWS - Lambda
You want your AWS Lambda handler to process an event with optional user key. If user is missing, return status 400 with message 'User missing'. Otherwise, return status 200 with 'Hello, <user>'. Which handler code correctly implements this?
Adef handler(event): if 'user' in event: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': f"Hello, {event['user']}"}
Bdef handler(event, context): return {'statusCode': 200, 'body': f"Hello, {event['user']}"}
Cdef handler(event, context): if event['user'] == None: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': 'Hello, user'}
Ddef handler(event, context): if 'user' not in event: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': f"Hello, {event['user']}"}
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter correctness

    Lambda handler must have event and context parameters.
  2. Step 2: Validate logic for user key

    def handler(event, context): if 'user' not in event: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': f"Hello, {event['user']}"} correctly checks if 'user' key is missing and returns 400 with message. Then returns 200 with personalized greeting. def handler(event, context): return {'statusCode': 200, 'body': f"Hello, {event['user']}"} does not check for missing user. def handler(event, context): if event['user'] == None: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': 'Hello, user'} incorrectly checks if event['user'] == None which causes error if key missing. def handler(event): if 'user' in event: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': f"Hello, {event['user']}"} reverses logic and misses context.
  3. Final Answer:

    def handler(event, context): if 'user' not in event: return {'statusCode': 400, 'body': 'User missing'} return {'statusCode': 200, 'body': f"Hello, {event['user']}" } -> Option D
  4. Quick Check:

    Check key presence before access to avoid errors [OK]
Quick Trick: Check key exists before access; include event and context [OK]
Common Mistakes:
MISTAKES
  • Missing context parameter
  • Accessing missing keys without check
  • Returning wrong status codes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More AWS Quizzes