Deployment, MLOps, and Security › Operating AI in Production › Day 337
Hands-on lab — Day 337: Deploying to a Cloud Service
- ← Back to the Day 337 lesson
- Open the hands-on files on GitHub — clone or download them from the public labs repository
- Local path in your clone:
labs/sections/deployment-mlops-and-security/day-337-deploying-to-a-cloud-service/
Commands
Setup
pip install -r requirements/requirements.txt Run
python examples/cloud_deployment.py Test
bash tests/run_tests.sh File tree
examples/cloud_deployment.py expected-output/examples-run.txt expected-output/FIELDS.md expected-output/measured-values.txt expected-output/run-output.txt expected-output/starter-run.txt expected-output/test-output.txt expected-output/test-run.txt metadata.yml README.md requirements/requirements.txt security.md starter/cloud_deployment.py tests/run_tests.sh tests/test_cloud_deployment.py troubleshooting.md
Lab README
Day 337 Lab: Deploying to a Cloud Service
Lesson
- Lesson title: Deploying to a Cloud Service
- Day number: 337 of 365
- Lesson article: https://ai-roadmap-365.github.io/day-337-deploying-to-a-cloud-service
- Lab files: everything you need is in this directory — follow “How to run” below.
- Browse the course locally: from the repository root, this lab also appears in the course website at
/labs/day-337-deploying-to-a-cloud-servicewhen the site is running.
Purpose
Build a Cloud Deployment Orchestration Simulator in Python managing Blue-Green revisions, automated health check verification, zero-downtime cutovers, and rollback protection.
Learning objectives
- Track Blue-Green revision states and traffic allocation.
- Execute health verification probes prior to cutover.
- Shift traffic with zero downtime upon successful validation.
- Abort cutover and preserve active traffic upon failure.
Prerequisites
- Python 3.10+ installed
- pytest installed
Supported operating systems
- macOS, Linux, Windows WSL2
Hardware requirements
- Standard CPU, 512MB RAM
Required software
- Python 3.10+, pytest
Free and open-source options
- Python Standard Library, Pytest
Installation
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements/requirements.txt
File structure
starter/cloud_deployment.py: Starter implementation skeletonexamples/cloud_deployment.py: Verified reference implementationtests/test_cloud_deployment.py: Test suiteexpected-output/: Captured execution logs
How to run
python3 examples/cloud_deployment.py
What the commands do
- Executes Blue-Green deployments, validates health checks, and tests rollbacks.
Expected output
{'status': 'DEPLOYMENT_SUCCESS', 'active_env': 'GREEN', 'deployed_image': 'v2.0.0', 'timestamp': 1788138002.104724}
Validation steps
Run test runner:
bash tests/run_tests.sh
Tests
The test suite validates:
- Initial state with Blue active at 100% traffic
- Successful deployment to Green with full traffic cutover
- Subsequent deployment flipping traffic back to Blue
- Failed health check aborting deployment and preserving active traffic
- Deployment history audit logging
Cleanup
rm -rf .pytest_cache __pycache__ .venv
Troubleshooting
Ensure traffic percentages sum to 100%.
Security notes
Runs completely offline on local CPU.
Extension exercises
Implement a progressive canary rollout state machine.
Navigation
Day number: 337 of 365
Expected output
FIELDS.md
# Measured Values
- `deployments_executed`: 4
- `cutover_time_ms`: 50
examples-run.txt
# Captured from a real run. Identifiers and timings below vary between
# runs -- compare the SHAPE of the output, not the exact values.
{'status': 'DEPLOYMENT_SUCCESS', 'active_env': 'GREEN', 'deployed_image': 'v2.0.0', 'timestamp': 1788138002.104724}
measured-values.txt
deployments_executed=4
cutover_time_ms=50
run-output.txt
# Captured from a real run. Identifiers and timings below vary between
# runs -- compare the SHAPE of the output, not the exact values.
{'status': 'DEPLOYMENT_SUCCESS', 'active_env': 'GREEN', 'deployed_image': 'v2.0.0', 'timestamp': 1788138002.104724}
starter-run.txt
An untouched starter fails the suite. Captured from a real run:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <examples.cloud_deployment.CloudDeploymentOrchestrator object at 0x10a9d5f30>
new_image_tag = 'v2.0.0', simulate_health_pass = True
def deploy_new_revision(self, new_image_tag: str, simulate_health_pass: bool=True) -> Dict[str, Any]:
> raise NotImplementedError('TASK 1: implement deploy_new_revision.')
E NotImplementedError: TASK 1: implement deploy_new_revision.
examples/cloud_deployment.py:19: NotImplementedError
=========================== short test summary info ============================
FAILED tests/test_cloud_deployment.py::test_successful_blue_to_green_cutover
FAILED tests/test_cloud_deployment.py::test_subsequent_green_to_blue_cutover
FAILED tests/test_cloud_deployment.py::test_failed_health_check_aborts_cutover
FAILED tests/test_cloud_deployment.py::test_deployment_history_logging - NotI...
========================= 4 failed, 1 passed in 0.03s ==========================
Implement each TASK in starter/, copy it over examples/, and the same
suite goes green.
test-output.txt
============================= test session starts ==============================
platform darwin -- Python 3.14.0, pytest-9.1.1, pluggy-1.6.0 -- <repo>/.venv-tools/bin/python3.14
cachedir: .pytest_cache
rootdir: <repo>/labs/sections/deployment-mlops-and-security/day-337-deploying-to-a-cloud-service
plugins: cov-7.1.0, anyio-4.14.2
collecting ... collected 5 items
tests/test_cloud_deployment.py::test_initial_deployment_state PASSED [ 20%]
tests/test_cloud_deployment.py::test_successful_blue_to_green_cutover PASSED [ 40%]
tests/test_cloud_deployment.py::test_subsequent_green_to_blue_cutover PASSED [ 60%]
tests/test_cloud_deployment.py::test_failed_health_check_aborts_cutover PASSED [ 80%]
tests/test_cloud_deployment.py::test_deployment_history_logging PASSED [100%]
============================== 5 passed in 0.01s ===============================
test-run.txt
============================= test session starts ==============================
platform darwin -- Python 3.14.0, pytest-9.1.1, pluggy-1.6.0 -- <repo>/.venv-tools/bin/python3.14
cachedir: .pytest_cache
rootdir: <repo>/labs/sections/deployment-mlops-and-security/day-337-deploying-to-a-cloud-service
plugins: cov-7.1.0, anyio-4.14.2
collecting ... collected 5 items
tests/test_cloud_deployment.py::test_initial_deployment_state PASSED [ 20%]
tests/test_cloud_deployment.py::test_successful_blue_to_green_cutover PASSED [ 40%]
tests/test_cloud_deployment.py::test_subsequent_green_to_blue_cutover PASSED [ 60%]
tests/test_cloud_deployment.py::test_failed_health_check_aborts_cutover PASSED [ 80%]
tests/test_cloud_deployment.py::test_deployment_history_logging PASSED [100%]
============================== 5 passed in 0.01s ===============================
Source files
examples/cloud_deployment.py (1865 bytes)
import time
from typing import Dict, Any, List, Optional
class CloudDeploymentOrchestrator:
def __init__(self):
self.active_environment = "BLUE"
self.revisions = {
"BLUE": {"image_tag": "v1.0.0", "healthy": True, "active_traffic_pct": 100},
"GREEN": {"image_tag": "v1.0.0", "healthy": False, "active_traffic_pct": 0}
}
self.deployment_history: List[Dict[str, Any]] = []
def deploy_new_revision(self, new_image_tag: str, simulate_health_pass: bool = True) -> Dict[str, Any]:
target_env = "GREEN" if self.active_environment == "BLUE" else "BLUE"
current_env = self.active_environment
self.revisions[target_env]["image_tag"] = new_image_tag
self.revisions[target_env]["healthy"] = False
if simulate_health_pass:
self.revisions[target_env]["healthy"] = True
self.revisions[target_env]["active_traffic_pct"] = 100
self.revisions[current_env]["active_traffic_pct"] = 0
self.active_environment = target_env
event = {
"status": "DEPLOYMENT_SUCCESS",
"active_env": target_env,
"deployed_image": new_image_tag,
"timestamp": time.time()
}
self.deployment_history.append(event)
return event
else:
self.revisions[target_env]["healthy"] = False
event = {
"status": "DEPLOYMENT_FAILED_HEALTH_CHECK",
"active_env": current_env,
"rolled_back_to": current_env,
"timestamp": time.time()
}
self.deployment_history.append(event)
return event
if __name__ == "__main__":
o = CloudDeploymentOrchestrator()
print(o.deploy_new_revision("v2.0.0", True))
metadata.yml (403 bytes)
lesson_id: D337
day: 337
kind: python
languages:
- python
setup_commands:
- pip install -r requirements/requirements.txt
run_commands:
- python examples/cloud_deployment.py
test_commands:
- bash tests/run_tests.sh
cleanup_commands:
- rm -rf __pycache__ .pytest_cache
requires_network: false
requires_api_key: false
estimated_minutes: 45
last_executed: '2026-08-30'
executed_on: macOS 15.0 Apple Silicon
requirements/requirements.txt (14 bytes)
pytest>=7.0.0
starter/cloud_deployment.py (972 bytes)
"""STARTER -- implement each TASK below, then run the tests.
Every function that raises NotImplementedError is yours to write. The
imports, constants and data structures are given; the logic is not.
Run `bash tests/run_tests.sh` to see which tasks are still outstanding.
"""
import time
from typing import Dict, Any, List, Optional
class CloudDeploymentOrchestrator:
def __init__(self):
self.active_environment = 'BLUE'
self.revisions = {'BLUE': {'image_tag': 'v1.0.0', 'healthy': True, 'active_traffic_pct': 100}, 'GREEN': {'image_tag': 'v1.0.0', 'healthy': False, 'active_traffic_pct': 0}}
self.deployment_history: List[Dict[str, Any]] = []
def deploy_new_revision(self, new_image_tag: str, simulate_health_pass: bool=True) -> Dict[str, Any]:
raise NotImplementedError('TASK 1: implement deploy_new_revision.')
if __name__ == '__main__':
o = CloudDeploymentOrchestrator()
print(o.deploy_new_revision('v2.0.0', True))
tests/run_tests.sh (253 bytes)
#!/usr/bin/env bash
set -e
export PYTHONPATH=examples:.
if [ -n "$PYTEST" ]; then
$PYTEST tests/test_*.py -v
elif command -v pytest &> /dev/null; then
pytest tests/test_*.py -v
else
python3 -m unittest discover -s tests -p 'test_*.py' -v
fi
tests/test_cloud_deployment.py (2456 bytes)
import pytest
from examples.cloud_deployment import CloudDeploymentOrchestrator
def test_initial_deployment_state():
orchestrator = CloudDeploymentOrchestrator()
assert orchestrator.active_environment == "BLUE"
assert orchestrator.revisions["BLUE"]["active_traffic_pct"] == 100
assert orchestrator.revisions["GREEN"]["active_traffic_pct"] == 0
def test_successful_blue_to_green_cutover():
orchestrator = CloudDeploymentOrchestrator()
res = orchestrator.deploy_new_revision("v2.0.0", simulate_health_pass=True)
assert res["status"] == "DEPLOYMENT_SUCCESS"
assert orchestrator.active_environment == "GREEN"
assert orchestrator.revisions["GREEN"]["image_tag"] == "v2.0.0"
assert orchestrator.revisions["GREEN"]["active_traffic_pct"] == 100
assert orchestrator.revisions["BLUE"]["active_traffic_pct"] == 0
def test_subsequent_green_to_blue_cutover():
orchestrator = CloudDeploymentOrchestrator()
orchestrator.deploy_new_revision("v2.0.0", simulate_health_pass=True) # Now GREEN
res = orchestrator.deploy_new_revision("v3.0.0", simulate_health_pass=True) # Back to BLUE
assert res["status"] == "DEPLOYMENT_SUCCESS"
assert orchestrator.active_environment == "BLUE"
assert orchestrator.revisions["BLUE"]["image_tag"] == "v3.0.0"
assert orchestrator.revisions["BLUE"]["active_traffic_pct"] == 100
assert orchestrator.revisions["GREEN"]["active_traffic_pct"] == 0
def test_failed_health_check_aborts_cutover():
orchestrator = CloudDeploymentOrchestrator()
# Attempt deployment to GREEN that fails health probe
res = orchestrator.deploy_new_revision("v2.0.0-buggy", simulate_health_pass=False)
assert res["status"] == "DEPLOYMENT_FAILED_HEALTH_CHECK"
assert orchestrator.active_environment == "BLUE" # Preserves BLUE
assert orchestrator.revisions["BLUE"]["active_traffic_pct"] == 100
assert orchestrator.revisions["GREEN"]["active_traffic_pct"] == 0
assert orchestrator.revisions["GREEN"]["healthy"] is False
def test_deployment_history_logging():
orchestrator = CloudDeploymentOrchestrator()
orchestrator.deploy_new_revision("v2.0.0", True)
orchestrator.deploy_new_revision("v2.1.0-bad", False)
assert len(orchestrator.deployment_history) == 2
assert orchestrator.deployment_history[0]["status"] == "DEPLOYMENT_SUCCESS"
assert orchestrator.deployment_history[1]["status"] == "DEPLOYMENT_FAILED_HEALTH_CHECK"