Soru

Zorluk: ZorAWS Serverless Application Model (SAM)

A developer is deploying a serverless application using the AWS Serverless Application Model (SAM). The template defines an AWS::Serverless::Api resource with an OpenAPI specification in the DefinitionBody property. The template also defines an AWS::Serverless::Function resource.

Within the OpenAPI specification, the developer configures the integration for a POST route as follows:

yaml
paths:
/orders:
post:
x-amazon-apigateway-integration:
type: "aws"
httpMethod: "POST"
uri:
Fn::Sub: "arn:aws:apigateway:AWS::Region:lambda:path/20150331/functions/{AWS::Region}:lambda:path/2015-03-31/functions/{OrderFunction.Arn}/invocations"

The Lambda function handler is implemented to return the following structure:

{
"statusCode": 201,
"body": "{\"message\": \"Order created successfully\"}",
"headers": {
"Content-Type": "application/json"
}
}

When the client sends a POST request to /orders, it receives an HTTP status code of 200 OK with the following response body:

{
"statusCode": 201,
"body": "{\"message\": \"Order created successfully\"}",
"headers": {
"Content-Type": "application/json"
}
}

Which configuration change should the developer make to ensure the client receives an HTTP status code of 201 Created with the message body '{"message": "Order created successfully"}'?

  1. A
    Modify the Lambda function handler to return only the JSON string '{"message": "Order created successfully"}' directly as the payload.
  2. Change the integration type to 'aws_proxy' in the x-amazon-apigateway-integration extension inside the OpenAPI specification.Cevap
  3. C
    Update the Lambda function's execution role to define the 'apigateway.amazonaws.com' service principal as a trusted entity in the trust policy.
  4. D
    Remove the 'Transform: AWS::Serverless-2016-10-31' line from the top of the template and deploy the stack.

Cevap

The correct answer is to change the integration type to 'aws_proxy' in the x-amazon-apigateway-integration extension inside the OpenAPI specification.
Changing the integration type to 'aws_proxy' enables the Lambda proxy integration. With proxy integration, API Gateway automatically parses the JSON response returned by the Lambda function, setting the HTTP status code, response headers, and response body matching the keys 'statusCode', 'headers', and 'body' in the returned object. This ensures the client receives the status code 201 instead of 200 with the raw JSON payload.

Adım Adım Çözüm

1
Analyze the current client response and the integration configuration in the template.
The client receives a 200 OK HTTP status code containing the raw JSON output of the Lambda function. The template uses a custom integration ('type: "aws"').
In custom integrations, API Gateway does not parse the status code or body from the Lambda response payload by default; it simply forwards the raw output with a default 200 OK status.
2
Identify the correct integration type required to automatically parse the Lambda proxy response structure.
Lambda proxy integration ('type: "aws_proxy"') is required.
When using 'aws_proxy', API Gateway automatically maps the 'statusCode', 'headers', and 'body' fields returned by the Lambda function to the final HTTP response.
3
Select the option that configures the Lambda proxy integration in the template.
Change 'type: "aws"' to 'type: "aws_proxy"' under x-amazon-apigateway-integration.
This correctly switches the API Gateway integration type to Lambda proxy integration, enabling the desired response mapping behavior.

Anahtar Kavram

API Gateway Integration Types in AWS SAM
Bu soruyu puanla