Courseiva
Development with AWS ServiceseasyMultiple ChoiceObjective-mapped

DVA-C02 Development with AWS Services Practice Question

A developer is building a serverless REST API using Amazon API Gateway and AWS Lambda. The API should return JSON responses to client requests. The developer is using the Lambda proxy integration. What is the simplest way to return a JSON response from the Lambda function?

⚠ Common exam trap

Candidates often think returning a JSON object directly from Lambda is sufficient, but they overlook the requirement that the body must be a JSON string and the response must include the exact 'statusCode', 'headers', and 'body' keys for API Gateway proxy integration to work correctly.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Return a dictionary containing 'statusCode', 'headers', and 'body' with 'body' as a JSON string.

With Lambda proxy integration, API Gateway passes the entire request to the Lambda function and expects the function to return a specific response format. The simplest way to return a JSON response is to return a dictionary (or object) containing 'statusCode', 'headers', and 'body', where 'body' is a JSON string. This format is required by API Gateway to correctly interpret the Lambda output and forward it to the client.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Return a string from the Lambda handler.

    Why it's wrong here

    When utilizing API Gateway's Lambda proxy integration, the Lambda function is expected to return a specific JSON structure that represents a complete HTTP response. A plain string output from the Lambda handler lacks essential HTTP components like a status code and response headers. Consequently, API Gateway cannot correctly interpret a simple string as a valid HTTP response, leading to an internal server error or an unexpected response format for the client, as it does not conform to the required proxy integration contract.

  • Return a dictionary containing 'statusCode', 'headers', and 'body' with 'body' as a JSON string.

    Why this is correct

    This format precisely adheres to the API Gateway Lambda proxy integration contract, where the Lambda function is solely responsible for constructing the entire HTTP response. By returning a dictionary containing `statusCode`, `headers` (as a dictionary of key-value pairs), and a `body` field (which itself must be a string, often a JSON string), the Lambda function provides all necessary information for API Gateway to directly pass through to the client. This ensures the client receives a properly formatted HTTP response with the correct status, custom headers, and a valid JSON payload.

  • Use API Gateway integration response and mapping templates to transform the Lambda output.

    Why it's wrong here

    With Lambda proxy integration, API Gateway passes the entire client request to the Lambda function and expects the function itself to return a properly formatted HTTP response, including the JSON body. Integration response and mapping templates are used only with non-proxy integration, where API Gateway transforms the Lambda output before sending it to the client. This option is tempting because mapping templates are a legitimate way to reshape responses in standard integrations, and would be correct if the developer had chosen the non-proxy integration type instead.

  • Return a JSON object from Lambda and set a Content-Type header in the API Gateway method response.

    Why it's wrong here

    Returning only a JSON object from Lambda, such as `{"key": "value"}`, does not satisfy the Lambda proxy integration contract, which mandates a specific envelope including `statusCode` and `headers`. While setting a `Content-Type` header in the API Gateway method response is relevant for non-proxy integrations to define the output type, it has no effect when Lambda proxy integration is enabled. In proxy integration, the Lambda function must explicitly define all response headers within its returned dictionary, making API Gateway's method response headers redundant for this purpose.

Quick reference

Cloud Service Model Comparison

ModelYou ManageProvider ManagesExamples
IaaSOS, runtime, apps, dataHardware, hypervisor, networkingEC2, Azure VMs, GCP Compute Engine
PaaSApps and dataOS, runtime, middleware, hardwareElastic Beanstalk, Azure App Service
SaaSData and settings onlyEverything elseMicrosoft 365, Salesforce, Workday
FaaS / ServerlessFunction code onlyInfra, scaling, runtimeLambda, Azure Functions, Cloud Run
CaaSContainers and appsKubernetes, OS, hardwareEKS, AKS, GKE

About these practice questions

This DVA-C02 question is part of Courseiva's 724-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This DVA-C02 practice question is part of Courseiva's free Amazon Web Services certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DVA-C02 exam.