Cloudflare Terraform Provider

repository·main·Indexed 23 days ago

https://github.com/cloudflare/terraform-provider-cloudflare

The Cloudflare Terraform Provider enables infrastructure-as-code management of Cloudflare resources by interfacing with the Cloudflare REST API. It supports multiple authentication methods including API tokens, API keys, and user service keys, and requires Terraform CLI version 1.0 or later.

Tokens
343.1K
Snippets
592
Records
1.6K
Agent score
77%

What's inside terraform-provider-cloudflare

  1. Manage Cloudflare Zero Trust Access Short-Lived Certificates

    main

    The cloudflare_zero_trust_access_short_lived_certificate resource allows you to manage short-lived certificates for Cloudflare Zero Trust Access. This resource requires specific API permissions to function:

    • Access: Apps and Policies Read
    • Access: Apps and Policies Write

    To use this resource, you must provide a valid app_id (the UUID of the application). You can optionally scope the resource to a specific account_id or zone_id, but these two fields are mutually exclusive.

    resource "cloudflare_zero_trust_access_short_lived_certificate" "example_zero_trust_access_short_lived_certificate" {
      app_id = "f174e90a-fafe-4643-bbbc-4a0ed4fc8415"
      zone_id = "zone_id"
    }
  2. Manage Cloudflare Zero Trust Device Posture Rules

    main

    The cloudflare_zero_trust_device_posture_rule resource allows you to define rules that check the security posture of devices (e.g., checking for specific files, OS versions, or third-party security agent statuses) before granting access via Cloudflare Zero Trust.

    Required Permissions:

    • Zero Trust Write
    resource "cloudflare_zero_trust_device_posture_rule" "example_zero_trust_device_posture_rule" {
      account_id = "699d98642c564d2e855e9661899b7252"
      name = "Admin Serial Numbers"
      type = "file"
      description = "The rule for admin serial numbers"
      expiration = "1h"
      input = {
        operating_system = "linux"
        path = "/bin/cat"
        exists = true
        sha256 = "https://api.us-2.crowdstrike.com"
        thumbprint = "0aabab210bdb998e9cf45da2c9ce352977ab531c681b74cf1e487be1bbe9fe6e"
      }
      match = [{
        platform = "windows"
      }]
      schedule = "1h"
    }
  3. Manage Cloudflare Zero Trust Access Policies

    main

    The cloudflare_zero_trust_access_policy resource allows you to manage Access policies within Cloudflare Zero Trust. These policies define how users and devices are authorized to access protected applications.

    To manage these resources, your API token must have the following permissions:

    • Access: Apps and Policies Read
    • Access: Apps and Policies Write
  4. Manage Cloudflare Waiting Room Events with cloudflare_waiting_room_event

    main

    The cloudflare_waiting_room_event resource allows you to define specific event configurations for a Cloudflare Waiting Room. Events can override various Waiting Room properties (like custom_page_html, session_duration, or queueing_method) while they are active.

    To use this resource, you must have the following permissions:

    • Waiting Rooms Read
    • Waiting Rooms Write
    resource "cloudflare_waiting_room_event" "example_waiting_room_event" {
      zone_id = "023e105f4ecef8ad9ca31a8372d0c353"
      waiting_room_id = "699d98642c564d2e855e9661899b7252"
      event_end_time = "2021-09-28T17:00:00.000Z"
      event_start_time = "2021-09-28T15:30:00.000Z"
      name = "production_webinar_event"
      custom_page_html = "{{#waitTimeKnown}} {{waitTime}} mins {{/waitTimeKnown}} {{^waitTimeKnown}} Event is prequeueing / Queue all enabled {{/waitTimeKnown}}"
      description = "Production event - DO NOT MODIFY"
      disable_session_renewal = true
      new_users_per_minute = 200
      prequeue_start_time = "2021-09-28T15:00:00.000Z"
      queueing_method = "random"
      session_duration = 1
      shuffle_at_event_start = true
      suspended = true
      total_active_users = 200
      turnstile_action = "log"
      turnstile_mode = "off"
    }
  5. Manage Cloudflare Pages projects with cloudflare_pages_project

    main

    The cloudflare_pages_project resource allows you to manage Cloudflare Pages projects via Terraform. This includes configuring build settings, deployment environments (production and preview), and various service bindings (like KV, D1, R2, etc.).

    Important Requirement: If you use a source block configuration (e.g., connecting to GitHub or GitLab), you must first have a connected account in the Cloudflare dashboard. See the Getting Started with Pages documentation for details.

    Required Permissions:

    • Pages Read
    • Pages Write
    resource "cloudflare_pages_project" "example_pages_project" {
      account_id = "023e105f4ecef8ad9ca31a8372d0c353"
      name = "my-pages-app"
      production_branch = "main"
      build_config = {
        build_caching = true
        build_command = "npm run build"
        destination_dir = "build"
        root_dir = "/"
        web_analytics_tag = "cee1c73f6e4743d0b5e6bb1a0bcaabcc"
        web_analytics_token = "021e1057c18547eca7b79f2516f06o7x"
      }
      deployment_configs = {
        preview = {
          # ... preview configuration
        }
        production = {
          # ... production configuration
        }
      }
      source = {
        config = {
          deployments_enabled = true
          owner = "my-org"
          owner_id = "12345678"
          path_excludes = ["string"]
          path_includes = ["string"]
          pr_comments_enabled = true
          preview_branch_excludes = ["string"]
          preview_branch_includes = ["string"]
          preview_deployment_setting = "all"
          production_branch = "main"
          production_deployments_enabled = true
          repo_id = "12345678"
          repo_name = "my-repo"
        }
        type = "github"
      }
    }
  6. Manage Cloudflare D1 Databases with cloudflare_d1_database

    main

    The cloudflare_d1_database resource allows you to manage Cloudflare D1 databases.

    Warning: When a D1 Database is replaced, all data is lost. Ensure you have a backup of your data before performing replacement operations.

    Required Permissions:

    • D1 Read
    • D1 Write
    resource "cloudflare_d1_database" "example_d1_database" {
      account_id = "023e105f4ecef8ad9ca31a8372d0c353"
      name = "my-database"
      jurisdiction = "eu"
      primary_location_hint = "wnam"
    }
  7. Manage Cloudflare Zero Trust DNS Locations

    main

    The cloudflare_zero_trust_dns_location resource allows you to configure secure DNS locations within Cloudflare Zero Trust. You can define destination endpoints (DoH, DoT, IPv4, IPv6), specify allowed source networks, and control DNS response TTL capping.

    Required Permissions:

    • Cloudflare Zero Trust Secure DNS Locations Write
    • Zero Trust Read
    • Zero Trust Write
    resource "cloudflare_zero_trust_dns_location" "example_zero_trust_dns_location" {
      account_id = "699d98642c564d2e855e9661899b7252"
      name = "Austin Office Location"
      client_default = false
      dns_destination_ips_id = "0e4a32c6-6fb8-4858-9296-98f51631e8e6"
      ecs_support = false
      endpoints = {
        doh = {
          enabled = true
          networks = [{
            network = "2001:85a3::/64"
          }]
          require_token = true
        }
        dot = {
          enabled = true
          networks = [{
            network = "2001:85a3::/64"
          }]
        }
        ipv4 = {
          enabled = true
        }
        ipv6 = {
          enabled = true
          networks = [{
            network = "2001:85a3::/64"
          }]
        }
      }
      max_ttl = {
        mode = "override"
        ttl_secs = 3600
      }
      networks = [{
        network = "192.0.2.1/32"
      }]
    }
  8. Understand Load Balancer steering policies

    main

    The steering_policy attribute determines how Cloudflare selects a pool for incoming requests. Available values include:

    • "off": Uses only default_pools.
    • "geo": Uses region_pools, country_pools, or pop_pools (Enterprise only) for location-based steering.
    • "random": Selects a pool randomly based on weights.
    • "dynamic_latency": Selects the closest pool in default_pools based on round trip time (requires health checks).
    • "proximity": Uses latitude/longitude to select the closest pool.
    • "least_outstanding_requests": Scales pool selection based on the number of pending requests and weights.
    • "least_connections": Scales pool selection based on the number of open connections and weights.
    • "": Maps to "geo" if regional/country/pop pools are configured, otherwise maps to "off".
  9. Identify missing DNS records for Email Routing

    main
    When using the cloudflare_email_routing_dns data source, you can identify which DNS records are required to enable Email Routing by inspecting the result.errors[].missing attribute. This attribute provides a list of the specific records (name, type, content, ttl, etc.) that must be present in your DNS configuration for the service to function.
  10. Understand Email Routing rule actions and matchers

    main

    When inspecting the result of cloudflare_email_routing_rules, you can drill down into the specific logic of each rule via nested schemas:

    Actions (result.actions)

    • type (String): The type of action. Supported values: drop, forward, worker.
    • value (List of String): The values associated with the action (e.g., destination addresses for a forward action).

    Matchers (result.matchers)

    • field (String): The field for the matcher. Supported value: to.
    • type (String): The type of matcher. Supported values: all, literal.
    • value (String): The specific value used for the matcher.
  11. Understand the rate limit action schema

    main

    The action attribute defines what happens when the rate limit threshold is met. It is a read-only attribute with the following nested fields:

    • mode (String): The action to perform. Available values: simulate, ban, challenge, js_challenge, managed_challenge.
    • timeout (Number): The time in seconds during which Cloudflare performs the mitigation. Must be an integer $\ge$ the period.
      • Note: If mode is challenge, managed_challenge, or js_challenge, do not provide this value; Cloudflare uses the zone's Challenge Passage time.
    • response (Attributes): A custom response to return.
      • Note: If mode is challenge, managed_challenge, or js_challenge, do not provide this object; Cloudflare uses zone challenge pages.
      • Nested response fields:
        • body (String): The response body (must match content_type).
        • content_type (String): Must be one of text/plain, text/xml, or application/json.