> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Serverless Deployments and Traffic Shifting With Lambda

> Explains AWS Lambda versioning, aliases, and traffic-shifting strategies (blue/green, canary, linear), demoing rollouts, weighted aliases, and using CodeDeploy for automated deployments and rollbacks

In this lesson we cover Lambda versioning, aliases, and traffic-shifting strategies commonly used in serverless deployments: blue/green, canary, and linear rollouts. These topics are essential for controlled releases, fast rollbacks, and are exam-relevant for the AWS Certified DevOps Engineer — Professional.

We use the demo function `devops-020-320-function` to demonstrate how published versions and aliases enable predictable rollouts and safe production traffic management.

## Lambda versioning basics

Key concepts:

* Publishing a version creates an immutable snapshot of the function's code and configuration.
* The special identifier `\$LATEST` is mutable — it always refers to the most recent un-published state (it changes whenever you update code or configuration).
* A qualified ARN (for example, `arn:aws:lambda:us-east-1:598274344262:function:devops-020-320-function:6`) targets a specific published version.
* Aliases are named pointers to versions (for example, `prod`, `staging`, `live`) and provide stable references for production invocations.

When you publish versions you can:

* Pin an alias to a single version (100% traffic to that version).
* Configure a weighted alias to split traffic between two versions (Lambda supports weights for up to two versions per alias).

![A screenshot of the AWS Lambda console on the "Versions" tab for the function named "devops-020-320-function," showing multiple published versions (3–6) with an alias "live." The page also shows buttons to add a trigger/destination and options to publish or delete versions.](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/aws-lambda-versions-alias-live.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=5fc2dba67fa19771b3b157ed06655ece)

Important operational note: once a version is published it cannot be modified. Invoking a function without a qualifier calls whatever `\$LATEST` currently is, which is unstable for production.

Do not rely on `\$LATEST` for production traffic. Use aliases pinned to specific published versions for stable, auditable invocations and reproducible rollbacks.

### Quick CLI examples

* Publish a new version:

```bash theme={null}
aws lambda publish-version --function-name devops-020-320-function --description "release v6"
```

* Create an alias that points to a specific version:

```bash theme={null}
aws lambda create-alias --function-name devops-020-320-function --name prod --function-version 6 --description "Production"
```

* Create a weighted alias (90% to v6, 10% to v5):

```bash theme={null}
aws lambda update-alias --function-name devops-020-320-function --name prod \
  --routing-config '{"AdditionalVersionWeights":{"5":0.1}}' --function-version 6
```

## Aliases and weighted traffic shifting

Aliases are friendly, stable names that point to either a single version or a weighted split between two versions. Common uses:

* Pin an alias to a single published version for stable traffic (example: `prod -> 6`).
* Split traffic between two versions via weights for gradual rollouts (example: `prod -> 90% v6, 10% v5`).

Lambda supports weighting for up to two versions per alias; for more advanced patterns or automated scheduling you should use CodeDeploy.

![A screenshot of the AWS Lambda console for the function "devops-020-320-function" showing the Aliases tab and a green banner saying "Alias prod was successfully created." The aliases table lists "live" and "prod," with prod routing traffic to versions 6 and 5 (weights 90% and 10%).](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/lambda-prod-alias-traffic-split.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=d40e6cb62539dd9b61083e424bebdf2e)

Monitor alias-level metrics in CloudWatch (invocations, errors, duration, throttles) to validate a rollout. If the new version behaves poorly, re-pin the alias to the previous version to roll back instantly.

## When to use CodeDeploy for Lambda

Manual alias updates or scripted changes work for simple rollouts, but CodeDeploy provides robust, automated traffic-shifting strategies and integrates with CI/CD pipelines (CodePipeline). Benefits include:

* Built-in patterns: canary (small initial percentage), linear (incremental steps), and all-at-once.
* Lifecycle hooks: `BeforeAllowTraffic` and `AfterAllowTraffic` to run validation or smoke-tests.
* Alarm-based rollback: integrate CloudWatch alarms to trigger automatic rollbacks.
* Detailed deployment history, tags, and deployment-group management for traceability.

Use CodeDeploy when you need automated canary/linear deployments, lifecycle hooks, or alarm-driven rollbacks as part of a production-ready CI/CD pipeline.

To use CodeDeploy with Lambda you supply an AppSpec file that describes the Lambda function, alias, current/target versions, and hooks. Example AppSpec YAML:

```yaml theme={null}
version: 0.0
Resources:
  - devops-demo-function:
      Type: AWS::Lambda::Function
      Properties:
        Name: "devops-demo-function"
        Alias: "prod"
        CurrentVersion: "5"
        TargetVersion: "6"
Hooks:
  BeforeAllowTraffic:
    - "arn:aws:lambda:us-east-1:123456789012:function:ValidateNewVersion"
  AfterAllowTraffic:
    - "arn:aws:lambda:us-east-1:123456789012:function:RunSmokeTests"
```

* `BeforeAllowTraffic`: run validations before new traffic is allowed.
* `AfterAllowTraffic`: run smoke tests after traffic starts flowing to the target.

Make sure the hook functions and the specified versions/aliases exist; otherwise deployments will fail and CodeDeploy may trigger a rollback.

![A screenshot of the AWS CodeDeploy "Create deployment" page showing deployment group overrides and rollback configuration options. A red error banner at the bottom says "No Deployment Group found for name: devops-020-20-canry," and the "Create deployment" button is visible.](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/aws-codedeploy-deployment-group-not-found.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=266b0585624eff11f28be1a224634a48)

If the target Lambda, version, or alias is missing or misconfigured, deployments will fail:

![A screenshot of the AWS CodeDeploy console showing deployment d-94B4N5CEI failed. A red error banner states the target Lambda function "devops-demo-function" or the specified version/alias cannot be found, and the Deployment details show status "Failed."](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/aws-codedeploy-deployment-failed-lambda-missing.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=e5de0aec3e26ad6de7a23db908772474)

CodeDeploy deployment groups provide metadata and history so you can trace failures and investigate rollbacks:

![A screenshot of the AWS CodeDeploy console showing Deployment group tags (Environment: learning, Purpose: devops-professional-exam-prep, Demo: 020-320, ManagedBy: terraform) and the deployment history panel listing failed deployments and a CodeDeploy rollback.](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/aws-codedeploy-deployment-group-tags-rollback.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=ef8885536d623d5ff15c8d4977cdafab)

## Practical tips and important limits

* Always invoke stable aliases in production instead of `\$LATEST`.
* Weighted aliases support at most two versions per alias. For multi-step rollouts, use CodeDeploy or manage multiple aliases programmatically.
* Use CodeDeploy for scheduled or automated canary/linear rollouts, hook execution, and alarm-based rollback.
* CloudWatch alarms can trigger rollbacks within CodeDeploy deployment configurations.
* AWS SAM and API Gateway also provide deployment/canary features — choose based on your pipeline and operational needs.
* Consider SnapStart (for supported runtimes like Java) or provisioned concurrency to reduce cold-start latency for heavy runtimes.

You can manage versions, aliases, and many Lambda settings (memory, ephemeral storage, environment variables, permissions, URLs) in the Lambda console. SnapStart publishes cached snapshots of a version which can improve startup latency for supported runtimes.

![Screenshot of the AWS Lambda console on the "Aliases" tab for the function devops-020-320-function, with a green banner stating "Alias test was successfully created and is now pointing to versions 5 and 6." The aliases table lists "live", "prod", and "test" entries (with version numbers and traffic weights) and the function ARN is visible on the right.](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/aws-lambda-aliases-devops-020-320.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=e2bc35d5760be44bc7bb7e45b21e46f0)

![A screenshot of the AWS Lambda console "Edit basic settings" page showing configuration fields (Memory 256 MB, Ephemeral storage 512 MB, SnapStart set to PublishedVersions, Timeout 0:30) and an on-screen green banner saying "Alias test was successfully created."](https://mintcdn.com/kodekloud-c4ac6d9a/deef86LW8w1XLb8X/images/AWS-Certified-DevOps-Engineer-Professional/Domain-1-SDLC-Automation/Demo-Serverless-Deployments-and-Traffic-Shifting-With-Lambda/aws-lambda-edit-settings-alias-created.jpg?fit=max\&auto=format\&n=deef86LW8w1XLb8X\&q=85\&s=2fe4805eff781a15e77a3fe958a93743)

## Deployment patterns — quick comparison

| Pattern | Description | Use case |
| - | - | - |
| Blue/Green (All-at-once) | Deploy new version to separate environment or alias and switch 100% traffic once validated | Fast cutover when rollback is simple and downtime is acceptable |
| Canary | Send a small initial percentage to the new version, then increase to 100% after validation | Safe incremental rollout for critical services |
| Linear | Increase traffic in equal increments over time (e.g., 10% every 5 minutes) | Gradual rollout for steady validation and time-based observation |

## Summary — exam-relevant points

* `\$LATEST` is mutable and changes with every update; do not use it as a production reference.
* Aliases are stable references; use them for production traffic and auditability.
* Weighted aliases support at most two versions per alias for traffic splitting.
* Use CodeDeploy for automated canary/linear deployments, lifecycle hooks, and alarm-based rollback.
* Understand the differences between canary (small then full), linear (incremental), and all-at-once deployments.

Further reading and references:

* [AWS Lambda versioning and aliases](https://docs.aws.amazon.com/lambda/latest/dg/configuration-versions.html)
* [AWS CodeDeploy for Lambda applications](https://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-using-lambda.html)
* [CloudWatch alarms and metrics](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)

Understanding Lambda versioning, aliases, and CodeDeploy traffic-shifting options is both practically useful and commonly tested on the DevOps Professional exam.

> **[Watch Video](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/199ecdf9-a58f-4c2d-b9c9-eac36de111cd/lesson/86256ed6-be78-4a2b-88be-254171346538)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.