Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 70 ยท Security

Secrets, WAF, Shield and GuardDuty

Learn Secrets Manager rotation, WAF rules, Shield, GuardDuty, Security Hub and Inspector, with a runnable WAF rule matcher and rotation simulator.

Strong identity rules and encryption are the foundation, but real systems also face daily threats: leaked passwords in code, bots hammering a login page, floods of traffic, and compromised credentials quietly mining cryptocurrency. AWS offers a family of managed services for each of these jobs. Some prevent attacks (WAF, Shield), some detect them (GuardDuty, Inspector) and some gather the findings in one place (Security Hub).

This lesson gives you a map of those services and shows the two with the most day-to-day work for developers: Secrets Manager (keeping credentials out of code and rotating them) and WAF (filtering web requests). It builds on Encryption and KMS and on Parameter Store and Secrets Manager basics.

None of these services can run on this site, so every AWS command, rule and JSON document here is read-only. The runnable examples are Python stand-ins that follow the same ideas (a rotation sequence and a rule matcher). They are far simpler than the real services, and the lesson says where.

Secrets Manager and rotation

A secret is a value you must keep private: a database password, an API key, an OAuth token. The cardinal rule is that secrets never live in source code, container images or plain environment variables checked into a repository. Secrets Manager stores them encrypted with KMS, controls access with IAM and records every read in CloudTrail.

Create and read a secret (read-only commands):

Terminal
aws secretsmanager create-secret \
  --name prod/orders/db \
  --secret-string '{"username":"orders","password":"change-me"}'

aws secretsmanager get-secret-value --secret-id prod/orders/db

In application code (boto3, read-only here) you fetch it at start-up and cache it; do not call the API on every request, since each call costs money and adds latency:

Python
import json, boto3

client = boto3.client("secretsmanager")
secret = json.loads(client.get_secret_value(SecretId="prod/orders/db")["SecretString"])
db_password = secret["password"]

The feature that makes Secrets Manager more than an encrypted notebook is automatic rotation. A rotation Lambda function changes the password in the database and in the secret on a schedule, for example every 30 days. For RDS databases AWS supplies ready-made rotation functions. Rotation follows four steps, each with an AWS version label on the secret:

  1. createSecret: generate a new password and store it as the pending version (AWSPENDING).
  2. setSecret: change the password in the database to the new value.
  3. testSecret: log in with the new password to prove it works.
  4. finishSecret: move the label AWSCURRENT to the new version; the old one becomes AWSPREVIOUS.

Because the old password stays readable as AWSPREVIOUS, applications that cached it keep working briefly, and a failed rotation can roll back. The stand-in below walks through those steps with plain dictionaries:

Python Secrets Manager rotation with version labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real service also needs network access from the rotation Lambda to the database, and a secret that two systems share needs a "multi-user" rotation so there is no gap while credentials change. Parameter Store can hold secrets too (as SecureString), but it has no built-in rotation; that is the usual reason to pick Secrets Manager.

AWS WAF: filtering web requests

AWS WAF (Web Application Firewall) inspects HTTP requests that reach CloudFront, an Application Load Balancer, API Gateway or AppSync, and decides to allow, block, count or challenge each one. It works at layer 7: it can look at paths, headers, query strings, the body, the client's IP address and country. Security groups cannot do that; they only see ports and addresses.

A WAF setup has:

  • A web ACL attached to a resource, containing ordered rules.
  • A default action (allow or block) used when no rule matches.
  • Rules that can be your own (match statements), rate-based (block an IP that sends more than N requests in five minutes) or managed rule groups written by AWS or vendors, such as common web exploits or known bad inputs (SQL injection, cross-site scripting).

Rules run in priority order; the first rule with a terminating action (Allow or Block) decides. Count does not stop evaluation, which makes it the right mode for testing a new rule against real traffic before you turn blocking on.

flowchart LR
    C["Client"] --> W{"Web ACL<br/>rules in priority order"}
    W -- "first Block or Allow wins" --> D["Decision"]
    W -- "no rule matches" --> F["Default action"]
    D -- Allow --> O["Origin (ALB, API, CloudFront)"]
    F -- Allow --> O
    D -- Block --> X["403 response"]

A request walks the rules from the lowest priority number up; the first terminating match decides.

A rule that blocks requests from one IP range (JSON as WAF stores it, read-only):

JSON
{
  "Name": "BlockBadRange",
  "Priority": 1,
  "Statement": {
    "IPSetReferenceStatement": { "ARN": "arn:aws:wafv2:us-east-1:111122223333:global/ipset/bad-range/abc123" }
  },
  "Action": { "Block": {} },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "BlockBadRange"
  }
}

The next stand-in is a small WAF-like matcher with path and header rules, a rate limit and a default action. It does not implement the real rule language, but the ordering and the Count-versus-Block difference work as described.

Python A mini WAF: ordered rules, count mode and rate limiting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real WAF costs money per web ACL, per rule and per million requests, and managed rule groups add their own fee, so start with a few well-chosen rules. Be careful with rules that block on text alone: they cause false positives. Run new rules in Count mode first and check the sampled requests.

Shield, GuardDuty and the rest of the family

AWS Shield protects against distributed denial-of-service (DDoS) attacks. Shield Standard is automatic and free for every customer and covers common network and transport-layer attacks. Shield Advanced is a paid subscription with larger-attack protection, 24/7 support from the response team, cost protection for scaling during an attack and automatic application-layer mitigation together with WAF. Putting CloudFront in front of your application (see CloudFront CDN) hides the origin and absorbs much attack traffic.

Amazon GuardDuty is threat detection. Enable it with one command and it analyzes CloudTrail events, VPC Flow Logs (flow logs lesson) and DNS logs, with no agents. It reports findings such as an instance contacting a known command-and-control server, credentials used from an unusual country or an S3 bucket suddenly made public. A finding is information; to act on it, send findings to EventBridge and trigger a Lambda function or notify a team.

Terminal
aws guardduty create-detector --enable
aws guardduty list-findings --detector-id 12abc34d567e8fa901bc2d34e56789f0

Amazon Inspector scans for vulnerabilities: software packages on EC2 instances, container images in ECR and Lambda function code, ranking findings by severity and by whether the vulnerable thing is actually reachable from the internet.

AWS Security Hub collects findings from GuardDuty, Inspector, IAM Access Analyzer and other tools into one dashboard, normalized into a common format, and checks your accounts against standards such as the AWS Foundational Security Best Practices and CIS benchmarks. In a multi-account setup you designate an administrator account that sees every member account (multi-account governance).

A quick way to remember the division of labor:

Service Question it answers Type
Secrets Manager Where do credentials live, and can they rotate? Prevent
WAF Should this web request be allowed? Prevent
Shield Can the site survive a flood of traffic? Prevent
GuardDuty Is something suspicious happening right now? Detect
Inspector Which software is vulnerable? Detect
Security Hub What is the overall security picture and compliance status? Aggregate

None of these replace the basics: least-privilege IAM, patching and encryption. They add layers, and layers are cheaper than incidents.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main reason to choose Secrets Manager over a plain encrypted parameter?

    Secrets Manager can rotate secrets on a schedule through a rotation function. Parameter Store SecureString values are encrypted too but have no built-in rotation.
  2. 2. In AWS WAF a rule is set to Count. What does it do to a matching request?

    Count is a non-terminating action. It records the match in metrics and logs, then continues with the next rules, which makes it ideal for testing a new rule before blocking.
  3. 3. Which service analyzes CloudTrail, VPC Flow Logs and DNS logs to raise findings such as traffic to a known malicious address?

    GuardDuty is the managed threat-detection service that reads those log sources without agents and produces findings.
  4. 4. Why can an application keep working briefly after Secrets Manager finishes a rotation?

    After finishSecret the old version moves to AWSPREVIOUS, so clients that cached it can still be supported for a short time while they pick up the new value.

Summary

  • Keep secrets out of code; Secrets Manager stores them encrypted, audits access and can rotate them through a createSecret, setSecret, testSecret, finishSecret sequence with version labels.
  • Fetch secrets at start-up and cache them; every call is billed.
  • WAF filters layer-7 requests with ordered rules in a web ACL; the first Allow or Block decides, Count is for testing, rate-based rules slow abusers and managed rule groups save effort.
  • Shield Standard is free and automatic; Shield Advanced adds extra protection, support and cost protection.
  • GuardDuty detects threats from logs, Inspector finds vulnerabilities, and Security Hub gathers findings and compliance checks in one place.
  • These services add layers; they do not replace least privilege, patching and encryption.

Next: Multi-Account Strategy and Control Tower