Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Google Cloud (GCP)›Cloud Armor and Security Policies

Networking and Operations

Cloud Armor and Security Policies

Cloud Armor is a web application firewall (WAF) service that inspects traffic at the Google Cloud edge before it reaches your backend infrastructure. It is essential for protecting against Layer 7 attacks, including SQL injection, cross-site scripting (XSS), and large-scale distributed denial-of-service (DDoS) events. You reach for Cloud Armor whenever you need to secure HTTP/S load-balanced applications, implement geo-fencing, or enforce fine-grained access control based on client IP reputation.

The Anatomy of a Security Policy

At its core, a Cloud Armor security policy is an ordered collection of rules that act as a gateway for your backend services. When traffic arrives at the Google Cloud global load balancer, the load balancer consults the attached security policy to decide whether to allow, deny, or redirect the incoming request. The policy uses a prioritized list, meaning the rule with the lowest priority number is evaluated first. If a request matches a rule, the associated action is taken immediately, and subsequent rules are ignored. This is the fundamental mechanism that allows for granular control: you can whitelist specific internal corporate IP ranges at high priority, while blocking known malicious actors or broad geographic regions at lower priority. By operating at the edge, Cloud Armor prevents malicious traffic from ever consuming your backend compute resources, which saves money and prevents performance degradation during attacks.

# Create a policy with a default 'deny all' rule to ensure a secure baseline
gcloud compute security-policies create policy-restrictive \
    --description "Restrictive policy to prevent unauthorized access"

Rule Matching Logic and Priorities

Understanding the evaluation order in Cloud Armor is critical for avoiding accidental outages. Rules are processed in ascending numerical order, where priority 0 is evaluated before priority 1000. When designing a policy, you must place your most specific 'Allow' rules at the top, followed by more general 'Deny' rules. If your default rule (usually priority 2147483647) is set to 'deny,' any traffic not explicitly matching an 'allow' rule will be rejected. This design ensures that security is 'fail-closed' by default. Furthermore, every rule must contain a match condition—often defined using Common Expression Language (CEL)—which inspects request headers, IP addresses, or request paths. Because Cloud Armor works at the edge, you can use these rules to filter out traffic patterns that are clearly signatures of vulnerability scanners before they ever reach your virtual machines or containerized workloads, effectively acting as an intelligent shield for your application endpoints.

# Add a rule to allow internal developer IP addresses specifically
gcloud compute security-policies rules create 100 \
    --security-policy policy-restrictive \
    --expression "inIpRange('192.168.1.0/24')" \
    --action "allow"

Mitigating Layer 7 Attacks with Preconfigured Rules

Beyond simple IP filtering, Cloud Armor provides preconfigured WAF rules that leverage Google's extensive threat intelligence. These rules are designed to detect common attack vectors like SQL injection (SQLi) and cross-site scripting (XSS) by analyzing the structure of incoming HTTP request parameters, headers, and cookies. Instead of writing complex regex patterns yourself, which can be prone to false positives or bypasses, you can apply these managed rule sets directly to your security policy. When you enable these rules, Cloud Armor inspects the request body and metadata for known malicious patterns. If a pattern matches, you can choose to log the event, return a 403 Forbidden status, or trigger a custom response. Because these rules are updated by Google to track emerging vulnerabilities, they are the most efficient way to maintain a robust security posture against evolving threats without requiring constant manual adjustment of your own custom logic.

# Enable preconfigured SQLi protection rules for higher security
gcloud compute security-policies rules create 500 \
    --security-policy policy-restrictive \
    --expression "evaluatePreconfiguredExpr('sqli-stable')" \
    --action "deny-403"

Rate Limiting and Adaptive Protection

Rate limiting is a primary defense mechanism against brute-force attacks and volumetric traffic spikes that could overwhelm your infrastructure. In Cloud Armor, rate limiting allows you to restrict the number of requests per client, defined by their IP address or a custom header. When a user exceeds the threshold you set, you can either throttle the traffic or block it entirely for a specified duration. Advanced Cloud Armor tiers introduce 'Adaptive Protection,' which uses machine learning to baseline your application's normal traffic patterns. When an anomaly is detected, it automatically proposes a protective rule to help you block malicious traffic without impacting legitimate users. By combining static rate limits for known interfaces with adaptive, behavior-based protection, you create a dynamic security layer that adapts to the shifting nature of internet traffic, ensuring your application remains resilient even under unexpected load or targeted denial-of-service attempts.

# Set a rate limit to prevent brute force on login endpoints
gcloud compute security-policies rules create 1000 \
    --security-policy policy-restrictive \
    --src-ip-ranges "0.0.0.0/0" \
    --rate-limit-threshold-count 100 \
    --rate-limit-threshold-interval-sec 60 \
    --action "rate-limit-based-on-ban"

Geo-fencing and Operational Best Practices

Geo-fencing is often requested for compliance or risk reduction, where an application only needs to serve users in specific countries. Cloud Armor makes this trivial by providing built-in geographic classification for incoming requests based on the client IP's origin. By creating a policy rule that denies all traffic except from allowed country codes, you can significantly reduce your attack surface by eliminating traffic from regions where you do not operate. Operationally, you should always monitor security policy logs using Cloud Logging to identify false positives. If you accidentally block legitimate traffic, the logs will show exactly which rule triggered the action, allowing you to refine your expression or priority levels. Always test new policies in 'preview' mode if available, or apply them to a staging load balancer, to verify that your security rules are not interfering with standard application workflows before moving them to production traffic.

# Deny access from countries not explicitly authorized
gcloud compute security-policies rules create 2000 \
    --security-policy policy-restrictive \
    --expression "!origin.region_code == 'US'" \
    --action "deny-403"

Key points

  • Cloud Armor policies function as a priority-ordered list of rules that intercept traffic at the Google Cloud edge.
  • The priority field in security rules determines the evaluation order, with lower numbers processed before higher ones.
  • A 'fail-closed' strategy is implemented by setting the default security rule to deny all traffic not matching an allow rule.
  • Preconfigured WAF rules protect against complex vulnerabilities like SQL injection and XSS without requiring manual signature updates.
  • Rate limiting provides a vital defense against brute-force attacks by restricting the frequency of requests per client.
  • Adaptive Protection uses machine learning to detect traffic anomalies and suggest rules to block malicious behavior automatically.
  • Geo-fencing capabilities allow for simple, effective blocking of traffic based on the geographic origin of a client IP.
  • Monitoring security logs is essential for diagnosing false positives and ensuring your rules are performing as intended.

Common mistakes

  • Mistake: Configuring a security policy but forgetting to attach it to the backend service. Why it's wrong: A policy is just a configuration object until it is explicitly associated with a backend; without attachment, traffic remains unprotected. Fix: Always verify that the security policy is linked to the specific backend service in the Load Balancer configuration.
  • Mistake: Assuming that Cloud Armor rules apply to internal traffic within a VPC. Why it's wrong: Cloud Armor policies operate exclusively at the edge, specifically for external traffic hitting HTTP(S) Load Balancers. Fix: Use VPC firewall rules or Service Directory to manage internal traffic security.
  • Mistake: Setting the default rule to 'Allow' while attempting to whitelist specific IPs. Why it's wrong: If the default rule is 'Allow', any traffic not explicitly blocked by a higher-priority rule will pass, making a whitelist ineffective. Fix: Set the default rule to 'Deny' and explicitly create 'Allow' rules for trusted IP ranges.
  • Mistake: Creating multiple conflicting rules with the same priority. Why it's wrong: Google Cloud requires unique priorities for each rule; identical priorities cannot coexist in a single policy. Fix: Assign distinct integer values for priorities and plan gaps (e.g., 100, 200, 300) to allow for future rule insertions.
  • Mistake: Neglecting to test policies in Preview mode. Why it's wrong: Deploying a strict rule immediately can cause legitimate traffic to be dropped, resulting in downtime. Fix: Always deploy rules in 'Preview' mode first, monitor the logs to verify only unwanted traffic is blocked, then switch to 'Enforce' mode.

Interview questions

What is the primary function of Google Cloud Armor, and how does it fit into a GCP security strategy?

Google Cloud Armor is a web application firewall (WAF) service built directly into the Google Cloud infrastructure. Its primary function is to provide defense-in-depth against distributed denial-of-service (DDoS) attacks and common web vulnerabilities like SQL injection or cross-site scripting (XSS). It operates at the edge of Google's network, intercepting traffic before it reaches your backend resources, ensuring that only clean, authorized traffic is permitted.

Can you explain how to configure a basic security policy in Google Cloud Armor using the gcloud command line?

To configure a security policy, you first create the policy, add rules to it, and then attach it to a backend service. For example, to create a policy named 'my-policy', you run 'gcloud compute security-policies create my-policy --description="Block bad actors"'. Then, you add a rule: 'gcloud compute security-policies rules create 1000 --security-policy=my-policy --expression="inIpRange('1.2.3.4/32')" --action="deny-403"'. Finally, associate it: 'gcloud compute backend-services update [BACKEND_NAME] --security-policy=my-policy'. This process effectively offloads traffic inspection to Google's edge, preventing unauthorized access before it impacts your virtual machine instances.

What is the difference between Google Cloud Armor security policies and IAM roles in the context of securing a GCP load balancer?

The primary difference lies in their operational layer. IAM roles manage access control for resources, determining who can configure or modify the load balancer, but they do not filter incoming traffic requests from users. Conversely, Cloud Armor security policies function at the application layer to inspect and filter the actual traffic payload and IP origins. While IAM secures the administrative plane, Cloud Armor secures the data plane, protecting the public-facing endpoints from malicious activity.

Compare using IP-based allowlisting in Cloud Armor versus using signed URLs/Cookies for content restriction.

IP-based allowlisting is a perimeter-focused approach ideal for internal tools or B2B connections where the client origin is static and known. It is simple to implement but lacks flexibility for mobile users. In contrast, signed URLs or cookies provide granular, session-based access control. They are superior when you need to grant temporary access to private content stored in Cloud Storage buckets, regardless of the user's IP address. Signed mechanisms are more secure for global users, while IP allowlisting is strictly for static network perimeters.

How does Google Cloud Armor utilize adaptive protection to mitigate complex DDoS attacks?

Adaptive Protection is an advanced machine learning feature that builds a baseline profile of your application’s typical traffic patterns. When it detects anomalies that deviate from this profile, it automatically suggests or applies WAF rules to block malicious traffic. This is essential for defending against Layer 7 DDoS attacks, which often masquerade as legitimate user traffic. By identifying these patterns dynamically, it provides proactive security that a static rule set cannot achieve, allowing administrators to act on suggested signatures before an attack overwhelms the backend.

How would you design a GCP security architecture to handle a scenario where you must block high-traffic malicious bots while allowing legitimate regional traffic using rate limiting?

To architect this, I would implement a Cloud Armor security policy with tiered rules. First, I would create a 'Rate Limiting' rule using the 'rate-limit-based' action to enforce a threshold, such as 100 requests per minute per client IP. I would set this rule at a higher priority than the default allow rule. Then, I would layer a 'Preconfigured WAF rule' on top to specifically target bot signatures. By applying these policies to the global external HTTP(S) load balancer, we ensure traffic is throttled at the Google edge before consuming backend compute resources. This approach maintains performance for legitimate users while effectively suppressing high-frequency automated bot traffic.

All Google Cloud (GCP) interview questions →

Check yourself

1. An administrator needs to restrict access to a web application based on the user's geographic location. Which feature of Cloud Armor should be used?

  • A.VPC Firewall Rules
  • B.Geo-based restriction rules within a Cloud Armor security policy
  • C.Identity-Aware Proxy (IAP) access levels
  • D.Cloud DNS response policies
Show answer

B. Geo-based restriction rules within a Cloud Armor security policy
Cloud Armor natively supports geo-based blocking or allowing using rule conditions. VPC Firewalls do not handle geo-data. IAP is for authentication, not geo-filtering. Cloud DNS policies manage name resolution, not traffic content filtering.

2. When a security policy rule is set to 'Preview' mode, what is the impact on actual incoming traffic?

  • A.The traffic is logged but no action is taken based on the rule.
  • B.The traffic is automatically blocked if it matches the rule.
  • C.The traffic is re-routed to a sandboxed environment for analysis.
  • D.The rule is applied only to non-HTTPS traffic.
Show answer

A. The traffic is logged but no action is taken based on the rule.
Preview mode logs match events so administrators can evaluate rule effectiveness without actually affecting traffic flow. Blocking occurs only in 'Enforce' mode.

3. A Cloud Armor security policy has a rule with priority 500 set to 'Deny' and a rule with priority 1000 set to 'Allow'. What happens to traffic that matches both rules?

  • A.The traffic is allowed because 1000 is a higher priority number.
  • B.The traffic is denied because Cloud Armor uses the lowest integer as the highest priority.
  • C.The traffic is allowed because the most specific rule takes precedence.
  • D.The traffic is denied due to an automatic rule conflict error.
Show answer

B. The traffic is denied because Cloud Armor uses the lowest integer as the highest priority.
Cloud Armor evaluates rules in ascending order of priority, where the lowest number has the highest precedence. Therefore, priority 500 is evaluated before priority 1000.

4. Why is it recommended to use a default 'Deny' rule in a Cloud Armor policy?

  • A.It improves the performance of the Load Balancer by reducing state lookups.
  • B.It prevents accidental exposure of backend services to unauthorized traffic.
  • C.It is required by Google Cloud compliance standards for all deployments.
  • D.It forces users to authenticate before they can see the website.
Show answer

B. It prevents accidental exposure of backend services to unauthorized traffic.
A default 'Deny' rule implements a 'least privilege' security model, ensuring that only explicitly permitted traffic is allowed, which prevents accidental exposure. Performance, compliance, and authentication are separate concerns.

5. Which type of traffic can be protected by a Cloud Armor security policy?

  • A.Traffic sent to a Compute Engine instance via an internal IP address.
  • B.Traffic flowing between two microservices within the same GKE cluster.
  • C.External HTTP(S) traffic destined for a Global External Application Load Balancer.
  • D.Traffic passing through a Cloud VPN tunnel to an on-premises network.
Show answer

C. External HTTP(S) traffic destined for a Global External Application Load Balancer.
Cloud Armor is an edge security service designed specifically to protect applications behind Google Cloud Load Balancers. Internal IP traffic, inter-service traffic, and VPN traffic are managed by other VPC-level controls.

Take the full Google Cloud (GCP) quiz →

← PreviousCloud Monitoring and Cloud LoggingNext →GCP Interview Questions

Google Cloud (GCP)

20 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app