Question #751
A developer is preparing to deploy a serverless application using AWS SAM CLI. Which step must be completed before packaging the application using the SAM CLI?
Compress the application into a .zip file for Lambda upload.
Validate the AWS SAM template using the SAM validate command.
Build the application dependencies using the SAM build command.
Initialize the SAM project with the sam init command.
Explanation
The correct answer is C because the sam build command compiles source code, installs dependencies, and prepares the application artifacts in a .aws-sam directory. This step is essential before packaging (sam package) or deploying (sam deploy), as packaging relies on these built artifacts to create a deployable AWS CloudFormation template. Without building dependencies, the packaged application might lack critical libraries, causing runtime failures in AWS Lambda.
Why other options are incorrect:
- A: SAM CLI automatically handles compression during packaging; manual zipping is unnecessary.
- B: While validating the template (sam validate) is useful, it is not a mandatory step before packaging.
- D: sam init is used to create a new SAM project, which is irrelevant if the application already exists.
Key Points:
1. sam build resolves dependencies and prepares artifacts.
2. Packaging (sam package) requires these artifacts to bundle the application for deployment.
3. Skipping sam build may result in missing dependencies in the Lambda function.
Answer
The correct answer is: C