Soru

Zorluk: OrtaAWS Serverless Application Model (SAM)

A developer uses AWS SAM to deploy a serverless application. The template defines an `AWS::Serverless::Function` triggered by an API Gateway HTTP API event source, and a custom `AWS::IAM::Role` for the execution role. The deployment completes successfully. However, when the API is invoked, the client receives a `502502 Bad Gateway` error. The CloudWatch logs show that the Lambda service is unable to assume the configured execution role, and the function is not executed.

Here is a portion of the template:

yaml
Resources:
ProcessOrderFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: index.handler
Runtime: nodejs18.x
Role: !GetAtt ExecutionRole.Arn
Events:
CreateOrder:
Type: Api
Properties:
Path: /orders
Method: post

ExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: WriteLogs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'

The handler code in `src/index.js` is defined as:

javascript
exports.handler = async (event) => {
return "Order successfully processed!";
};

Which TWO modifications are required to resolve both the execution role assumption issue and the `502502 Bad Gateway` error? (Select TWO.)

  1. Change the service principal in the AssumeRolePolicyDocument of ExecutionRole to lambda.amazonaws.comCevap
  2. Modify the handler code in src/index.js to return a JSON object containing statusCode and body keysCevap
  3. C
    Change the service principal in the AssumeRolePolicyDocument of ExecutionRole to sts.amazonaws.com
  4. D
    Configure an API Gateway integration mapping template in the SAM template to parse the raw string return value
  5. E
    Increase the Timeout property of the Lambda function in the template to 3030 seconds to bypass proxy response validation

Cevap

To resolve these errors, change the execution role's trust policy service principal to lambda.amazonaws.com, and update the Lambda function handler to return a structured JSON object containing statusCode and body properties.
The trust policy must authorize the AWS Lambda service principal (lambda.amazonaws.com) to assume the role. The default Lambda Proxy Integration requires the response payload to be formatted with statusCode and body keys; otherwise, API Gateway returns a 502 Bad Gateway error.

Adım Adım Çözüm

1
Analyze the IAM trust configuration for ExecutionRole in the template.
The principal service is set to apigateway.amazonaws.com, which grants API Gateway trust to assume the role instead of granting it to the Lambda execution service.
AWS Lambda must be explicitly trusted in the role's AssumeRolePolicyDocument to run the function code.
2
Analyze the API response model requirements for SAM Api events.
The Api event source implicitly sets up a Lambda Proxy Integration, which requires the handler to return a JSON object with statusCode and body properties.
Returning a raw string causes API Gateway to fail parsing the response, leading to a HTTP 502 Bad Gateway response.
3
Update the IAM service principal to lambda.amazonaws.com and refactor the handler return statement.
The execution role can now be assumed by Lambda, and the handler output conforms to the required proxy integration format.
These changes address both the execution permissions and the API response structure validation rules.

Anahtar Kavram

AWS SAM Integration Mechanics and IAM Service Trust Principles
Tahmini Süre:1m 30s
Bu soruyu puanla