> ## 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.

# Production Hardening Safe Deployment Advanced

> Guide to hardening AWS deployments by removing plaintext secrets, adding AWS WAF, enforcing least privilege, enabling ECS autoscaling, detecting CloudFormation drift, and using safe reversible deployment patterns.

Your app is live on AWS, but "live" isn't the same as production-ready. Inspecting the running environment reveals five gaps that commonly cause outages or security incidents in production.

The most urgent issues:

* Secrets stored in plaintext inside the `ECS` task definition.
* No web firewall in front of the load balancer (AWS WAF).
* Fixed capacity (two tasks) that can't follow demand.
* In-place deploys with no safety net.
* Drift between the infrastructure template and the running environment.

These problems may not show up in a demo, but they will appear in production. We'll close them one-by-one and keep the change sequence and diagrams intact.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_CTwEK7wfhlq4R-D/images/Learn-By-Doing-Building-AI-Agents-with-Claude-Agent-SDK/Capstone/Production-Hardening-Safe-Deployment-Advanced/live-not-prod-ready-3tier-aws.jpg?fit=max&auto=format&n=_CTwEK7wfhlq4R-D&q=85&s=703bc6e28cbc7653042ec5096b81662c" alt="A presentation slide titled &#x22;Live, but Not Production-Ready&#x22; with a &#x22;3-Tier App • Live on AWS&#x22; badge. It lists four production issues—Secrets in Plain Text, No Web Firewall, Fixed Capacity, and In-Place Deploys—each with a short explanation." width="1920" height="1080" data-path="images/Learn-By-Doing-Building-AI-Agents-with-Claude-Agent-SDK/Capstone/Production-Hardening-Safe-Deployment-Advanced/live-not-prod-ready-3tier-aws.jpg" />
</Frame>

## Strategy: Narrow Ownership, Focused Fixes

Operate like a dedicated hardening specialist: limit the scope, apply minimal changes that materially reduce risk, and automate the checks.

### Quick summary of fixes

| Gap | Why it matters | What we do |
| - | - | - |
| Secrets in plaintext | Exposes credentials if repositories or artifacts leak | Move secrets to `AWS Secrets Manager`, reference by ARN from the ECS task via the `secrets` field |
| No edge firewall | Malicious traffic reaches the LB and containers | Attach an `AWS WAF` web ACL with managed rules to the ALB |
| Fixed capacity | Over- or under-provisioning causes latency or waste | Use ECS Service Auto Scaling with target-tracking on average CPU |
| Unsafe deploys | A bad release can take down service | Use blue/green or rolling deployments with health checks and ECS deployment circuit breaker |
| Stack drift | Manual changes hide divergence from IaC | Run CloudFormation drift detection and reconcile or roll back drifted resources |

<Callout icon="lightbulb" color="#1CB2FE">
  Focus fixes on what reduces the most risk quickly: secrets, edge protection, least-privilege permissions, autoscaling, drift detection, and safe deploys.
</Callout>

## Secrets: Remove plaintext credentials

* Move the database password and any other secrets into AWS Secrets Manager.
* Update the `ECS` task definition so it does not contain secret values. Instead:
  * Reference the secret by ARN and populate runtime environment variables using the task definition’s `secrets` field, or read secrets from Secrets Manager at runtime using the SDK.
* Remove plaintext secrets from source control, CI artifacts, and container images.

References:

* AWS Secrets Manager: [https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)
* ECS task secrets field: [https://docs.aws.amazon.com/AmazonECS/latest/developerguide/specifying-sensitive-data.html](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/specifying-sensitive-data.html)

## Firewall: Block bad traffic at the edge

* Create an AWS WAF web ACL and attach it to your load balancer (ALB).
* Add an AWS-managed ruleset to block common malicious requests so most bad traffic stops before it reaches containers.

References:

* AWS WAF web ACL: [https://docs.aws.amazon.com/waf/latest/developerguide/web-acl.html](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl.html)
* AWS managed rule groups: [https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups.html](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups.html)

## Permissions: Apply least privilege

* Restrict the ECS task role to only the IAM actions the application needs (e.g., read a specific secret, publish metrics, read S3 objects if required).
* Avoid broad policies such as `*` or `AdministratorAccess` for runtime roles.

## Capacity: Let load drive the task count

Autoscale your ECS service using target-tracking on average CPU to keep utilization near a chosen target (example below uses 70%). With target tracking, you declare the objective rather than the exact task count.

* Example policy: target-tracking on metric `ECSServiceAverageCPUUtilization`, target 70%, minimum 2 tasks, maximum 10 tasks.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_CTwEK7wfhlq4R-D/images/Learn-By-Doing-Building-AI-Agents-with-Claude-Agent-SDK/Capstone/Production-Hardening-Safe-Deployment-Advanced/scale-with-load-ecs-autoscaling-cpu70.jpg?fit=max&auto=format&n=_CTwEK7wfhlq4R-D&q=85&s=6b15d97b03f24d0c1fe01c7e0eafe2ac" alt="A UI infographic titled &#x22;Scale With the Load&#x22; showing ECS Service auto-scaling settings (metric ECSServiceAverageCPUUtilization, target 70%, min 2, max 10 tasks). Below are three status cards: Calm (2 tasks, CPU 32% below target), Busy (scale out, CPU 74% over target), and Peak (10 tasks, CPU near 70% at max)." width="1920" height="1080" data-path="images/Learn-By-Doing-Building-AI-Agents-with-Claude-Agent-SDK/Capstone/Production-Hardening-Safe-Deployment-Advanced/scale-with-load-ecs-autoscaling-cpu70.jpg" />
</Frame>

You don't pick exact task counts—AWS adjusts capacity to meet the objective and follow real traffic.

## Drift detection: Keep IaC and live infra aligned

* Manual edits during incidents cause drift, which leads to configuration sprawl and fragile environments.
* Run CloudFormation drift detection (for example via a lightweight CloudFormation MCP server) to compare live resources against your stack templates.
* Drifted resources are flagged so you can decide to incorporate the hand change into the template or revert the change.

Typical output:

* Resources that match the template: IN\_SYNC
* Resources that differ: DRIFTED

Example output from a simple drift detection run:

```bash theme={null}
$ python detect_drift.py
☒ cfn-mcp CloudFormation MCP server ready
☒ Bash aws cloudformation detect-stack-drift --stack-name prod-3tier
describe-stack-drift-detection-status → DETECTION_COMPLETE
ALB · web tier        IN_SYNC
ECS service · app tier IN_SYNC
RDS instance · db tier IN_SYNC
Security group        DRIFTED  # port 22 opened by hand
StackDriftStatus: DRIFTED (1 of 4 resources)
```

Reference: CloudFormation drift detection — [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html)

## Safe deployments: Make rollouts reversible

Never replace a running version blindly. Use deployment patterns and features that ensure a failed release does not cause downtime:

* Bring the new version up alongside the old version.
* Let the load balancer perform health checks on the new tasks.
* Only shift traffic after the new tasks are healthy.
* If a deployment fails, ECS’s deployment circuit breaker can halt the rollout and automatically roll back.
* The old version continues serving users until the new version is verified.

Example CLI to enable ECS deployment circuit breaker and safe deployment parameters:

```bash theme={null}
aws ecs update-service \
  --deployment-configuration deploymentCircuitBreaker={enable=true,rollback=true} \
  --maximumPercent 200 \
  --minimumHealthyPercent 100
```

<Callout icon="warning" color="#FF6B6B">
  Always test deployment behavior in a staging environment that mirrors production health checks and scaling behavior before applying to production.
</Callout>

### The production loop

Every change should follow a short, repeatable loop:

1. Validate the change (safety checks, policy, automated tests).
2. Deploy safely (bring up new tasks alongside the old version).
3. Verify health with real checks (ALB health checks and application-level probes).
4. Keep the change if healthy, or roll back if not.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_CTwEK7wfhlq4R-D/images/Learn-By-Doing-Building-AI-Agents-with-Claude-Agent-SDK/Capstone/Production-Hardening-Safe-Deployment-Advanced/production-loop-validate-deploy-verify-rollback.jpg?fit=max&auto=format&n=_CTwEK7wfhlq4R-D&q=85&s=b2f9c489d471c4d072f050240b628923" alt="The image is a dark-themed diagram titled &#x22;The Production Loop&#x22; showing four sequential boxes: Validate (check the change is safe), Deploy (bring up the new version), Verify Health (ALB health checks), and Keep or Roll Back (keep if healthy, revert if failing). It includes small arrows between steps and a © KodeKloud credit." width="1920" height="1080" data-path="images/Learn-By-Doing-Building-AI-Agents-with-Claude-Agent-SDK/Capstone/Production-Hardening-Safe-Deployment-Advanced/production-loop-validate-deploy-verify-rollback.jpg" />
</Frame>

## Put it all together

A production-hardened system includes:

* Secrets stored in `AWS Secrets Manager` and removed from plaintext artifacts.
* A web firewall (AWS WAF) protecting the edge.
* Tight task-role permissions (least privilege).
* Capacity that auto-scales with demand.
* Drift detection that surfaces manual changes immediately.
* Safe, reversible deployments that avoid downtime.

Production must survive the unexpected. Applying these focused, automated controls turns a demo into a resilient, production-ready system.

## Links & References

* AWS Secrets Manager: [https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)
* ECS task definitions and secrets: [https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task\_definitions.html](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html)
* AWS WAF web ACLs: [https://docs.aws.amazon.com/waf/latest/developerguide/web-acl.html](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl.html)
* ECS Service Auto Scaling: [https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-auto-scaling.html](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-auto-scaling.html)
* CloudFormation drift detection: [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-drift.html)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/learn-by-doing-building-ai-agents-with-claude-agent-sdk/module/84badd01-a809-4b63-87e9-8496a954f7da/lesson/fe1a5cac-5bd8-4c88-bab5-77be3766eaae" />
</CardGroup>


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