Question

Difficulty: Very hardAWS Serverless Application Model (SAM)

A developer is deploying a serverless application using a local AWS Serverless Application Model (SAM) template file named `template.yaml`. The template contains the following definition:

yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
GetProductFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs20.x
CodeUri: ./src
Events:
GetProduct:
Type: Api
Properties:
Path: /products/{id}
Method: get

The developer attempts to deploy the application directly by executing the following AWS CLI command:

`aws cloudformation deploy --template-file template.yaml --stack-name product-service-dev --capabilities CAPABILITY_IAM`

However, the command fails, indicating that the `CodeUri` property of the `AWS::Serverless::Function` resource must point to an Amazon S3 location.

Which of the following statements identifies the root cause of this error and the correct action to resolve it?

  1. A
    The template has the `Transform: AWS::Serverless-2016-10-31` declaration defined at the root level, but this declaration must be moved under the `Metadata` section of the template so that the CloudFormation service can parse and auto-upload local paths.
  2. CloudFormation cannot natively resolve local directory paths like `./src`. The developer must use `sam deploy` (or execute `aws cloudformation package` followed by `aws cloudformation deploy` using the generated packaged template) to zip and upload the local directory to Amazon S3, replacing the local path with an S3 URI.Answer
  3. C
    The execution role associated with the CloudFormation stack deployment is missing a trust policy allowing the `apigateway.amazonaws.com` service principal to assume the role, preventing CloudFormation from reading from the local path.
  4. D
    The API Gateway integration is configured as a proxy integration, which requires the backend Lambda function to return a structured JSON response containing headers, statusCode, and body to authorize local file access.

Answer

CloudFormation cannot natively resolve local directory paths like `./src`. The developer must use `sam deploy` (or execute `aws cloudformation package` followed by `aws cloudformation deploy` using the generated packaged template) to zip and upload the local directory to Amazon S3, replacing the local path with an S3 URI.
The correct response explains that CloudFormation cannot directly resolve local file paths. Standard CloudFormation deployments require that all Lambda code references (`CodeUri`) point to an S3 object. To resolve this, the developer must package the application using the AWS SAM CLI (`sam deploy`) or the AWS CLI package command (`aws cloudformation package`), which uploads the local zip file to S3 and returns a template with the updated S3 URLs before deploying.

Step-by-Step Solution

1
Analyze the failed deployment command and the error message.
The developer ran `aws cloudformation deploy` directly on a raw template containing `CodeUri: ./src`, and CloudFormation rejected it because it expects an S3 URL.
CloudFormation runs on AWS servers and has no direct access to the developer's local hard drive to retrieve `./src` during deployment.
2
Determine how local artifacts are prepared for AWS SAM deployments.
Local code directories must be compressed into a ZIP file, uploaded to an S3 bucket, and the template reference must be replaced with the S3 URI.
This artifact packaging step must occur prior to sending the template to the CloudFormation API.
3
Select the correct tool or sequence of commands to perform this preparation.
Using the AWS SAM CLI (`sam deploy` or `sam package`) or AWS CLI (`aws cloudformation package`) compiles and uploads local files, producing a deployable template.
These tools automate the packaging workflow and correctly rewrite local paths to S3 references before invoking CloudFormation deploy.

Key Concept

Local Artifact Packaging in AWS Serverless Application Model (SAM) Deployments
Estimated Time:3m 0s
Rate this question