Overview and What You Will Learn
Cloud Armor is Google Cloud's web application firewall (WAF) and DDoS protection service, and it evaluates each request against a security policy at the load balancer before the request reaches your backend. In this lab you will build a small external load balancer, add a preconfigured SQL injection rule and a rate-limiting rule, test the SQL injection rule in preview mode, enforce it, and confirm the results with real requests.
- Building a minimal global external Application Load Balancer
- Creating a security policy with ordered rules
- Adding a preconfigured SQL injection rule in preview mode
- Adding a per-IP rate-limiting rule
- Enforcing rules and reading Cloud Armor logs
Why This Matters in Production
Rate limiting at the edge keeps a flood from consuming the compute that real customers need. Picture a public API at a company like Hotstar during a major match: one source sending thousands of valid-looking requests per minute can exhaust backend capacity even though no single request is malicious. A Cloud Armor rate-based rule stops that pattern before it touches the backend, and a preconfigured WAF rule blocks known injection attacks at the same point.
Core Principles
A Cloud Armor policy is an ordered list of rules. Each rule has a priority number, a match expression and an action. The lowest number is evaluated first, the first matching rule decides the outcome, and a default rule at priority 2147483647 handles everything else.
Two rule types do most of the work. Rate-based rules limit how many requests one client key, such as an IP address, can send in an interval. Preconfigured WAF rules are maintained by Google and match known attack signatures such as SQL injection and cross-site scripting.
RememberA rate-based rule that matches every request and allows conforming traffic ends evaluation for all of those requests. Put specific deny rules at lower priority numbers than a catch-all rate rule, or they will never run.
Detailed Step-by-Step Practical Lab
- Create the project and link billing:
gcloud projects create mumbai-armor-lab-2026 --name="Cloud Armor Lab"gcloud billing projects link mumbai-armor-lab-2026 \ --billing-account=0A1B2C-3D4E5F-6A7B8Cgcloud config set project mumbai-armor-lab-2026gcloud services enable compute.googleapis.com logging.googleapis.com- Build a minimal global external Application Load Balancer. The backend has no VMs attached, so allowed requests return a 5xx error. That is fine here, because Cloud Armor acts before the backend is reached:
gcloud compute addresses create armor-lab-ip --globalgcloud compute health-checks create http health-check-armor --port=80 gcloud compute backend-services create backend-armor-lab \ --protocol=HTTP --port-name=http \ --health-checks=health-check-armor \ --load-balancing-scheme=EXTERNAL_MANAGED \ --global gcloud compute url-maps create armor-lab-map \ --default-service=backend-armor-lab gcloud compute target-http-proxies create armor-lab-proxy \ --url-map=armor-lab-map gcloud compute forwarding-rules create armor-lab-fr \ --load-balancing-scheme=EXTERNAL_MANAGED \ --network-tier=PREMIUM \ --address=armor-lab-ip \ --global \ --target-http-proxy=armor-lab-proxy \ --ports=80- Create the security policy:
gcloud compute security-policies create armor-policy-lab \ --description="SQL injection and rate limiting for the lab backend"- Add the preconfigured SQL injection rule at priority 1000, in preview mode first. Preview logs what the rule would block without blocking anything:
gcloud compute security-policies rules create 1000 \ --security-policy=armor-policy-lab \ --expression="evaluatePreconfiguredExpr('sqli-v33-stable')" \ --action=deny-403 \ --preview- Add the rate-limiting rule at priority 2000. A client IP above 100 requests per minute receives HTTP 429, and a client above 300 requests per minute is banned for 5 minutes. It comes after the SQL injection rule so attack requests are checked first:
gcloud compute security-policies rules create 2000 \ --security-policy=armor-policy-lab \ --expression="true" \ --action=rate-based-ban \ --rate-limit-threshold-count=100 \ --rate-limit-threshold-interval-sec=60 \ --ban-threshold-count=300 \ --ban-threshold-interval-sec=60 \ --ban-duration-sec=300 \ --conform-action=allow \ --exceed-action=deny-429 \ --enforce-on-key=ip- Attach the policy to the backend service, turn on request logging, and review the rules:
gcloud compute backend-services update backend-armor-lab \ --security-policy=armor-policy-lab \ --enable-logging --logging-sample-rate=1.0 \ --global gcloud compute security-policies rules list \ --security-policy=armor-policy-lab- Wait a few minutes for the load balancer to propagate, then send a normal request and a SQL injection attempt. While rule 1000 is in preview, both reach the backend and return the same 5xx status:
LB_IP=$(gcloud compute addresses describe armor-lab-ip --global \ --format="value(address)") curl -s -o /dev/null -w "%{http_code}\n" "http://$LB_IP/"curl -s -o /dev/null -w "%{http_code}\n" "http://$LB_IP/?id=1%27%20OR%20%271%27%3D%271"- Check the logs to see what the preview rule would have blocked:
gcloud logging read \ 'jsonPayload.previewSecurityPolicy.name="armor-policy-lab"' \ --limit=5 --freshness=30m- Enforce the SQL injection rule, wait about a minute, and repeat the attack request. It should now return 403:
gcloud compute security-policies rules update 1000 \ --security-policy=armor-policy-lab \ --no-preview sleep 60curl -s -o /dev/null -w "%{http_code}\n" "http://$LB_IP/?id=1%27%20OR%20%271%27%3D%271"- Trigger the rate limit with a burst of 150 requests. The last requests in the burst and the follow-up check should return 429. The 300-request ban threshold is not reached:
for i in $(seq 1 150); do curl -s -o /dev/null "http://$LB_IP/"; donecurl -s -o /dev/null -w "%{http_code}\n" "http://$LB_IP/"- Clean up in reverse order of creation. The load balancer bills while it exists, so do not skip this:
gcloud compute forwarding-rules delete armor-lab-fr --global --quietgcloud compute target-http-proxies delete armor-lab-proxy --quietgcloud compute url-maps delete armor-lab-map --quietgcloud compute backend-services delete backend-armor-lab --global --quietgcloud compute security-policies delete armor-policy-lab --quietgcloud compute health-checks delete health-check-armor --quietgcloud compute addresses delete armor-lab-ip --global --quietgcloud projects delete mumbai-armor-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakePutting a public load balancer in front of an application with no Cloud Armor policy attached. A preconfigured WAF rule and a basic rate limit take only minutes to add and block the most common web attacks at the edge.
TipCreate new rules with
--previewfirst and read the policy's request logs in Cloud Logging. Enforce a rule with--no-previewonly after confirming it does not match legitimate traffic. For rate limits, start with a generous threshold and tighten it gradually.
SecurityCloud Armor only protects traffic that passes through a supported load balancer. A VM with its own external IP bypasses the policy entirely, so close that path with firewall rules that accept traffic only from the load balancer.
- Rely on Google-maintained WAF rules first. Rule sets such as
sqli-v33-stableare updated as attack patterns change, similar to AWS WAF managed rule groups or Azure WAF managed rule sets. Use them before writing custom expressions for well-known attacks. - Tune sensitivity to cut false positives. Preconfigured rules support sensitivity levels, and you can exclude individual signature IDs that break a legitimate endpoint.
- Plan rule order deliberately. Keep specific allow and deny rules at lower numbers than broad rules, and leave gaps (1000, 2000, 3000) so you can insert rules later.
- Always set the enforcement key. Rate-limit rules without
--enforce-on-key=ipcount all traffic together, so one noisy client can trigger limits for everyone. - Enable backend service logging. Without it, Cloud Armor decisions do not appear in Cloud Logging and preview results cannot be reviewed.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute security-policies create NAME |
Create a Cloud Armor security policy |
gcloud compute security-policies rules create PRIORITY --action=rate-based-ban |
Add a rate-limiting rule |
gcloud compute security-policies rules create PRIORITY --expression="evaluatePreconfiguredExpr('sqli-v33-stable')" |
Add a preconfigured WAF rule |
gcloud compute security-policies rules update PRIORITY --no-preview |
Enforce a rule that was in preview mode |
gcloud compute backend-services update NAME --security-policy=POLICY --global |
Attach a policy to a backend service |
gcloud compute security-policies rules list --security-policy=POLICY |
Review active rules and priorities |