A developer is using AWS SAM to deploy a serverless application consisting of an API Gateway endpoint that triggers a Lambda function, which writes data to a DynamoDB table. The template is defined as follows:
yaml
AWSTemplateFormatVersion: '2010-09-09'
Resources:
ProcessTransactionFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs18.x
CodeUri: ./src
Events:
PostTransaction:
Type: Api
Properties:
Path: /transaction
Method: post
Role: !GetAtt LambdaExecutionRole.Arn
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- 'sts:AssumeRole'
Policies:
- PolicyName: DynamoDBWritePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'dynamodb:PutItem'
Resource: !GetAtt TransactionTable.Arn
During the deployment process using the AWS SAM CLI, the deployment fails with a parser error indicating that the resource type `AWS::Serverless::Function` is invalid. Additionally, if the parsing error is resolved, the Lambda function will fail to execute due to execution role issues.
Which two modifications must the developer make to ensure the template parses successfully and the Lambda function can be successfully assumed and executed by the AWS Lambda service?
- Add Transform: AWS::Serverless-2016-10-31 at the root level of the template.Answer
- Update the trust policy of LambdaExecutionRole to list lambda.amazonaws.com as the service principal.Answer
- CUpdate the execution policy of the IAM role to include the lambda:InvokeFunction action on the ProcessTransactionFunction resource.
- DModify the Lambda function handler code to return a raw text response since the default configuration for the SAM Api event source uses custom integration.
- EAdd a default Timeout property set to 900 seconds under the Globals section to prevent execution context reuse timeouts during deployment validation.