Easy ReviewDOP-C02
SDLC Automation22

SAM

Define serverless resources, build and test artifacts, and release Lambda versions through an alias.

AWS SAM combines serverless template declarations with a CLI for building, local testing, packaging, and deployment. Gradual releases add CodeDeploy traffic shifting, validation hooks, and alarm-based rollback.

Templates and Globals

AWS Serverless Application Model (SAM) describes a serverless application using a CloudFormation template with shorter resource declarations. A transformA transform expands SAM declarations into the CloudFormation resources used for deployment. is the instruction that expands those declarations into the CloudFormation resources deployed to AWS.

How it works

  1. Include Transform: AWS::Serverless-2016-10-31 and a Resources section. Mix SAM and ordinary CloudFormation resources when needed.

  2. Choose the resource declaration that matches the application.

    Resource What it defines
    AWS::Serverless::Function Lambda function and its event sources
    AWS::Serverless::Api API Gateway resources and methods for HTTPS endpoints
    AWS::Serverless::SimpleTable DynamoDB table with a single-attribute primary keyA primary key identifies an item in a DynamoDB table.
  3. Put triggers in the function’s Events property. An Api event without an explicit API reference contributes to an implicit API created by SAM.

  4. Put common supported settings, such as function Runtime and Timeout, in Globals. A runtimeA runtime is the language-specific environment that executes function code. is the language environment that executes a function.

  5. Apply inheritance according to the property’s data type.

    Data type Resource-specific behavior
    Primitive, such as a string or number Replaces the global value
    MapA map contains named values that SAM merges when combining Globals with resource settings., a set of named values Merges entries; local entries win duplicate keys
    List, an ordered collection Follows the global entries, which are prepended

Exam cues

  • If the question says many functions share a runtime and timeout, think Globals.Function, because supported properties can be inherited.
  • If the question says an API event has no explicit API resource, think implicit API, because SAM builds it from the function events.
  • If the question says a basic table needs a single key, think SimpleTable, because advanced DynamoDB configuration can use the ordinary CloudFormation table resource.

Remember

  • Required sections: Transform and Resources.
  • SimpleTable default key: String attribute id.
  • Omit ProvisionedThroughput for PAY_PER_REQUEST billing.
  • Only supported properties belong in Globals.

Trap: Resource-specific settings do not universally replace global settings. Maps merge, lists accumulate, and a resource cannot remove an inherited property.

From the AWS docs (15)
  • SAM templates require Transform: AWS::Serverless-2016-10-31.
  • SAM templates require a Resources section and can mix SAM resources with ordinary CloudFormation resources.
  • AWS::Serverless::Function defines a Lambda function; its Events property describes sources that trigger it.
  • AWS::Serverless::Api defines API Gateway resources and methods exposed through HTTPS endpoints.
  • SAM creates an implicit API from function Api events that do not reference an explicit AWS::Serverless::Api resource.
  • AWS::Serverless::SimpleTable creates a DynamoDB table with a single-attribute primary key; use AWS::DynamoDB::Table for advanced configuration.
  • The default SimpleTable primary key is a String attribute named id.
  • SimpleTable uses PAY_PER_REQUEST when ProvisionedThroughput is omitted.
  • Globals supplies shared supported properties, including Function Runtime and Timeout; unsupported properties cannot be placed in Globals.
  • A resource-specific primitive value replaces the corresponding Globals value.
  • Resource map entries merge with global entries, with resource entries winning duplicate keys.
  • Global list entries are prepended to the resource-specific list.
  • A resource cannot remove a property inherited from Globals.
  • SAM transforms its serverless resource declarations into CloudFormation resources at deployment.
  • A runtime is the language-specific execution environment used by a Lambda function.

Build, package, and deploy

The SAM CLI prepares code, uploads deployment artifactsAn artifact is packaged code and dependencies prepared for upload and deployment., and asks CloudFormation to deploy the application. An artifact is the packaged code and dependencies that the cloud service needs to run.

How it works

  1. Run sam build to prepare source code and dependencies. Its default output is .aws-sam/build; edit the original source and rebuild after changes.
  2. Use sam local to test locally when needed. Use sam local invoke for one invocation and sam local start-api for a local HTTP API server. SAM can also test Lambda functions in a synthesized CDK CloudFormation template, the template produced from CDK application code.
  3. Upload artifacts and update template references. sam package sends ZIP code artifacts to S3; image artifacts use ECR, a container-image registry. A separate package command is useful when build and deployment are separate stages.
  4. Run sam deploy to deploy through CloudFormation. It includes packaging, so a separate sam package is optional. sam deploy --guided collects settings and can save them in samconfig.toml.
  5. During cloud development, use sam sync --watch to watch changes. SAM Accelerate uses service APIs for supported changes and CloudFormation when needed.

Exam cues

  • If the question says a build stage must hand a deployable template to another stage, think sam build then sam package, because packaging replaces local paths with uploaded artifact locations.
  • If the question says test an API locally before deployment, think sam local start-api, because it exposes a local HTTP endpoint for the functions.
  • If the question says fast cloud iteration on a development stack, think sam sync, because direct service API updates avoid a full CloudFormation deployment for supported changes.

Remember

  • Build output: .aws-sam/build.
  • sam build --use-container uses a build container and requires Docker.
  • ZIP artifacts use S3; image artifacts use ECR.
  • sam deploy already includes packaging.
  • Use sam sync for development; use sam deploy or a delivery pipeline for production.

Trap: A successful build only prepares artifacts locally. The upload and CloudFormation deployment still need to happen.

SAM artifact preparation and deployment
123Source + templateisam buildiUpload artifactsiCloudFormationi
  1. Build the source code and dependencies.

  2. Upload the built artifacts with sam package or the packaging included in sam deploy.

  3. Deploy the template through CloudFormation.

Box details (4)
Source + template

Edit the original source and SAM template.

sam build

Prepare code and dependencies under .aws-sam/build.

Upload artifacts

ZIP artifacts use S3; image artifacts use ECR. Packaging updates template references.

CloudFormation

sam deploy includes packaging and deploys the application stack.

From the AWS docs (15)
  • sam build prepares application code and dependencies for local testing and deployment.
  • The default SAM build artifacts and built template are written under .aws-sam/build.
  • Edit original sources and rerun sam build after changes; the build directory is overwritten.
  • sam build --use-container builds inside a provided container image and requires Docker.
  • sam package uploads ZIP code artifacts to S3 and returns a template with local artifact paths replaced by S3 locations.
  • For Image package types, sam package uses an ECR repository specified with --image-repository.
  • sam deploy deploys an application using CloudFormation.
  • sam deploy includes packaging functionality, so a separate sam package command is optional.
  • sam local invoke runs a one-time local invocation of a Lambda function.
  • sam local start-api runs a local HTTP server for API-triggered Lambda functions.
  • sam sync, part of SAM Accelerate, uses direct service APIs for supported changes and CloudFormation deployment when those APIs are not available.
  • sam sync --watch monitors local changes and syncs them to the cloud.
  • AWS recommends sam sync for development environments and sam deploy or a CI/CD pipeline for production.
  • sam deploy --guided prompts for deployment settings and can save them to samconfig.toml.
  • The SAM CLI can locally test Lambda functions defined in a synthesized AWS CDK CloudFormation template.

Gradual deployments

SAM gradual deployment uses CodeDeploy to move a Lambda aliasA Lambda alias is a named invocation target that points to published function versions. between published versionsA published version locks Lambda function code and most configuration settings.. An alias is a named invocation target, while a published version is a snapshot with locked code and most settings. Callers keep using the alias while its traffic weights change.

How it works

  1. Deploy AutoPublishAlias first to establish an existing version and alias. On a later update, add DeploymentPreference.

  2. SAM detects new code, publishes an updated version, and generates the CodeDeploy resources needed for the deployment.

  3. Run the configured PreTraffic validation function before shifting traffic. HooksA validation hook is a function that tests a deployment and reports success or failure to CodeDeploy. must report Succeeded or Failed using PutLifecycleEventHookExecutionStatus.

  4. Shift requests through the alias using the selected type.

    Type Mechanism Predefined options
    CanaryA canary deployment exposes a new version to an initial share of traffic before shifting the remainder. First share, wait, then remaining traffic Canary10Percent5Minutes, Canary10Percent10Minutes, Canary10Percent15Minutes, Canary10Percent30Minutes
    Linear Equal shares at equal intervals Linear10PercentEvery1Minute, Linear10PercentEvery2Minutes, Linear10PercentEvery3Minutes, Linear10PercentEvery10Minutes
    All at once Entire shift immediately AllAtOnce
  5. Run PostTraffic validation after the shift. Failed validation or a configured CloudWatch alarm triggers rollback to the previous version.

Exam cues

  • If the question says move 10 percent of requests and wait 10 minutes, think Canary10Percent10Minutes, because it has an initial share and one later shift of the remainder.
  • If the question says test before customers reach new code, think PreTraffic, because it validates before the shift.
  • If the question says no canary behavior despite a correct template, think the caller’s invocation target, because direct version calls bypass alias routing.

Remember

  • Required pair: AutoPublishAlias plus DeploymentPreference.
  • DeploymentPreference.Enabled defaults to true.
  • Weighted aliases use at most two published versions and cannot point to $LATEST.
  • A hook that never reports back fails the deployment after one hour.

Trap: A first deployment has no previous version to shift from. Establish the alias and initial version before enabling gradual traffic shifting.

SAM gradual Lambda release
123ALARM → rollback4SAM templatealias + preferenceiCodeDeploytraffic shiftingiLambda aliasstable caller targetiNew versionpublished codeiCloudWatch alarmerrors, latencyi
  1. SAM generates CodeDeploy resources from the configured DeploymentPreference.

  2. CodeDeploy runs the pre-traffic hook, then moves alias weight to the new version using the chosen canary or linear type.

  3. The alias sends the configured share of requests to the new published version.

  4. A triggered alarm (or a failed hook) stops the deployment and the alias points back to the old version.

Box details (5)
SAM template

AutoPublishAlias creates a version and alias; DeploymentPreference enables CodeDeploy traffic shifting.

CodeDeploy

CodeDeploy runs configured pre-traffic checks, shifts alias traffic, and runs post-traffic checks. Failed checks or configured alarms trigger rollback.

Lambda alias

Invoke the alias to receive its traffic split between published versions.

New version

AutoPublishAlias publishes a new version when it detects a code change.

CloudWatch alarm

Configured CloudWatch alarms trigger rollback if they fire during deployment.

From the AWS docs (18)
  • DeploymentPreference requires AutoPublishAlias on AWS::Serverless::Function.
  • AutoPublishAlias publishes an updated Lambda version when a code change is detected and creates or updates the named alias.
  • DeploymentPreference generates a CodeDeploy application and deployment group.
  • Function invocations must use the alias qualifier to receive SAM gradual traffic shifting.
  • A canary deployment shifts traffic in two increments, separated by the configured interval.
  • A linear deployment shifts traffic in equal increments at equal time intervals.
  • AllAtOnce shifts all traffic to the new version immediately.
  • SAM lists Canary10Percent5Minutes, Canary10Percent10Minutes, Canary10Percent15Minutes, and Canary10Percent30Minutes as predefined types.
  • SAM lists Linear10PercentEvery1Minute, Linear10PercentEvery2Minutes, Linear10PercentEvery3Minutes, and Linear10PercentEvery10Minutes as predefined types.
  • Canary10Percent10Minutes shifts 10 percent immediately and the remaining traffic after 10 minutes.
  • PreTraffic and PostTraffic identify validation functions run before and after traffic shifting.
  • Failed pre-traffic or post-traffic validation, or a triggered configured CloudWatch alarm, causes CodeDeploy rollback.
  • Lambda validation hooks must report Succeeded or Failed using PutLifecycleEventHookExecutionStatus; missing notification within one hour fails the deployment.
  • For a first gradual release, deploy AutoPublishAlias without DeploymentPreference, then add DeploymentPreference on a later update to provide a previous version.
  • A weighted Lambda alias can route to at most two published versions and cannot point to $LATEST.
  • DeploymentPreference.Enabled defaults to true.
  • A Lambda alias is a named pointer to a function version.
  • Publishing a Lambda version locks the function code and most configuration settings.

Flashcards

tap to flip
Answer

·

All 22 cards as a list

A team has several event-driven Lambda functions with the same runtime. How should it define the application in AWS SAM?

Declare AWS::Serverless::Function resources with Events. Put the shared supported Runtime setting in Globals and include the required SAM transform and Resources section.

Which Transform declaration is required by an AWS SAM template?

Transform: AWS::Serverless-2016-10-31.

An AWS SAM function needs one environment variable changed while preserving shared global variables. How does Globals inheritance handle it?

Maps merge. Resource-specific entries replace duplicate global keys while other global entries remain.

An AWS SAM function declares a list that is also set in Globals. Does the function list replace the global list?

No. Global entries are prepended to the function list. A resource cannot remove inherited global properties.

An AWS SAM application needs only a DynamoDB table with a single String key and on-demand billing. Which resource and defaults fit?

AWS::Serverless::SimpleTable. Its default key is String id, and omitting ProvisionedThroughput sets PAY_PER_REQUEST.

An AWS SAM function declares Api events without an explicit AWS::Serverless::Api resource. What happens?

SAM creates an implicit API from those events. Use an explicit AWS::Serverless::Api when configuring the API directly.

A pipeline needs a packaged AWS SAM template before a separate deployment stage. What should it run?

Run sam build, then sam package to upload ZIP artifacts to S3 and produce a template with remote artifact locations. Deploy that template in the deployment stage.

An AWS SAM pipeline runs sam build followed by sam deploy without sam package. Is a packaging command missing?

No. sam deploy includes packaging and uploads the referenced local artifacts as needed before CloudFormation deployment.

Which AWS SAM commands invoke one function locally and expose local API endpoints?

sam local invoke performs a one-time invocation. sam local start-api runs a local HTTP server for API-triggered functions.

An AWS SAM developer wants code edits quickly reflected in a development cloud stack. What should the developer use?

sam sync --watch. It watches local changes and uses direct service APIs for supported updates, falling back to CloudFormation when needed.

Where does sam build put its default output, and what should be edited after a source change?

Output goes under .aws-sam/build. Edit the original source and rerun sam build because the build directory is overwritten.

An AWS SAM function uses an Image package type. Where are its packaged image artifacts uploaded?

An Amazon ECR repository, specified for sam package with --image-repository. ZIP code artifacts use S3.

Does sam sync provide the recommended release path for a production AWS SAM stack?

No. AWS recommends sam deploy or a CI/CD pipeline for production and sam sync for development.

A team defines Lambda functions with AWS CDK but wants to invoke them locally with SAM. What should it provide to SAM?

Synthesize the CDK application to a CloudFormation template, then point the SAM CLI at that template for local Lambda testing.

A team needs gradual Lambda releases from an AWS SAM template. What should it configure?

AutoPublishAlias and DeploymentPreference on AWS::Serverless::Function. SAM publishes the version and alias and generates CodeDeploy resources for traffic shifting.

Which predefined traffic-shifting types does AWS SAM list for DeploymentPreference?

Canary10Percent5Minutes, Canary10Percent10Minutes, Canary10Percent15Minutes, Canary10Percent30Minutes; Linear10PercentEvery1Minute, Linear10PercentEvery2Minutes, Linear10PercentEvery3Minutes, Linear10PercentEvery10Minutes; and AllAtOnce.

An AWS SAM application has no previously deployed Lambda version. How should the team introduce gradual deployment?

Deploy once with AutoPublishAlias and no DeploymentPreference. Add DeploymentPreference on a subsequent update so CodeDeploy has an existing version to shift traffic from.

What causes an AWS SAM gradual deployment to roll back, and how do validation hooks report results?

A configured alarm or failed pre-traffic or post-traffic validation triggers rollback. Hook functions report Succeeded or Failed to CodeDeploy using PutLifecycleEventHookExecutionStatus.

An AWS SAM release needs validation before traffic moves and rollback on application errors. What should be configured?

AutoPublishAlias and DeploymentPreference with a PreTraffic hook and CloudWatch alarms. Failed validation or a triggered configured alarm rolls back the deployment.

An AWS SAM canary is configured, but clients call a numbered Lambda version directly. Will they receive the alias traffic split?

No. Clients must invoke the alias qualifier to participate in traffic shifting.

What does Canary10Percent10Minutes do in an AWS SAM deployment?

Move 10 percent of traffic to the new version immediately, then move the rest after 10 minutes if the deployment continues successfully.

How many Lambda versions can a weighted alias use during an AWS SAM release, and can one be $LATEST?

At most two published versions. The alias cannot point to $LATEST.

You finished every topic of DOP-C02!

Your badge turned gold. Next step: book the exam on AWS. Once you pass, add yourself to Who passed.

Share

Share that you finished this certification

I finished every topic of AWS Certified DevOps Engineer - Professional (DOP-C02) on Easy Review. Come study too!