Pulumi Cloudflare Provider

repository·master·Indexed 41 days ago

https://github.com/pulumi/pulumi-cloudflare

Manage Cloudflare resources, such as DNS records and rulesets, using Infrastructure as Code in TypeScript, Python, Go, .NET, and Java. This provider is a derived work of the Cloudflare Terraform provider and supports authentication via API Tokens, API Keys, or User Service Keys.

Tokens
36.3K
Snippets
82
Records
151
Agent score
99%

What's inside Pulumi Cloudflare

  1. Overview of the Pulumi Cloudflare provider

    master
    The Pulumi Cloudflare provider is a package designed for creating and managing Cloudflare cloud resources using Pulumi's Infrastructure as Code (IaC) capabilities. It allows you to define and provision Cloudflare configurations (such as DNS records, rulesets, and other cloud resources) using familiar programming languages like C#, Python, Go, TypeScript, and Java.
  2. Use the Pulumi Cloudflare provider

    master
    The pulumi-cloudflare package allows you to create and manage Cloudflare cloud resources using Pulumi's Infrastructure as Code (IaC) capabilities. It is a derived work of the Cloudflare Terraform provider, providing access to Cloudflare's extensive API surface through Pulumi's programming models in .NET, Python, Go, TypeScript, and Java.
  3. Install the Cloudflare provider

    master

    The Cloudflare provider is available as a package for all supported Pulumi languages. Choose the package corresponding to your runtime:

    • JavaScript/TypeScript: @pulumi/cloudflare
    • Python: pulumi-cloudflare
    • Go: github.com/pulumi/pulumi-cloudflare/sdk/v6/go/cloudflare
    • .NET: Pulumi.Cloudflare
    • Java: com.pulumi/cloudflare
  4. Install the Cloudflare Pulumi package

    master

    Install the Cloudflare provider package using the package manager corresponding to your programming language. Ensure you have the Pulumi CLI installed first.

    ### Node.js (Java/TypeScript)
    $ npm install @pulumi/cloudflare
    # or
    $ yarn add @pulumi/cloudflare
    
    ### Python
    $ pip install pulumi_cloudflare
    
    ### Go
    $ go get github.com/pulumi/pulumi-cloudflare/sdk/v6
    
    ### .NET
    $ dotnet add package Pulumi.Cloudflare
  5. Migrate from version 0.x to 1.0.0

    master

    The release of version 1.0.0 introduced several breaking changes to provider configuration and environment variables. If you are upgrading from a version prior to 1.0.0, you must apply the following changes:

    ### BREAKING CHANGES (v1.0.0):
    
    * provider configuration option `token` has been renamed to `api_key`.
    * provider configuration option `org_id` has been renamed to `account_id` and is now required.
    * provider configuration option `use_org_from_zone` has been removed.
    * environment variable `CLOUDFLARE_TOKEN` has been renamed to `CLOUDFLARE_API_TOKEN`.
    * environment variable `CLOUDFLARE_ORG_ID` has been renamed to `CLOUDFLARE_ACCOUNT_ID`.
    * environment variable `CLOUDFLARE_ORG_ZONE` has been removed.
    * `zone_id` is now a required parameter in all resources
  6. Disable provider preview functionality

    master

    In version 2.7.0 and later, the provider includes improved preview functionality that provides a more accurate understanding of actual changes. If you need to revert to the previous behavior, you can disable this feature by setting the PULUMI_DISABLE_PROVIDER_PREVIEW environment variable to 1 or false.

    export PULUMI_DISABLE_PROVIDER_PREVIEW=1
  7. Configure Cloudflare authentication

    master

    The Cloudflare provider requires authentication to perform operations. You can provide credentials via Pulumi configuration or environment variables.

    Important Authentication Rules:

    • You must provide exactly one of cloudflare:apiKey, cloudflare:apiToken, or cloudflare:apiUserServiceKey.
    • If using cloudflare:apiKey, you must also provide cloudflare:email.
    • cloudflare:apiToken is the recommended modern method; cloudflare:apiKey is considered legacy.
    Config KeyEnv VarDescription
    cloudflare:apiKeyCLOUDFLARE_API_KEYLegacy API key. Requires cloudflare:email.
    cloudflare:apiTokenCLOUDFLARE_API_TOKENRecommended API Token.
    cloudflare:apiUserServiceKeyCLOUDFLARE_API_USER_SERVICE_KEYRestricted key for specific endpoints.
    cloudflare:emailCLOUDFLARE_EMAILRequired when using apiKey.
  8. Configure Cloudflare provider credentials

    master

    The Cloudflare provider requires authentication to interact with Cloudflare resources. You must provide exactly one of the following authentication methods. You can set these via Pulumi configuration or environment variables.

    Authentication Options

    • apiToken (Recommended): The API Token for operations. Use the CLOUDFLARE_API_TOKEN environment variable or set via Pulumi config.
    • apiKey: The legacy API key. Requires the email field to be set. Use the CLOUDFLARE_API_KEY environment variable.
    • apiUserServiceKey: A special restricted API key. Use the CLOUDFLARE_API_USER_SERVICE_KEY environment variable.

    Configuration via Pulumi.yaml

    To set the apiToken using Pulumi configuration, add the following to your Pulumi.yaml:

    config:
        cloudflare:apiToken:
            value: 'YOUR_API_TOKEN'
    config:
        cloudflare:apiToken:
            value: 'TODO: var.cloudflare_api_token'
  9. Configure Cloudflare provider advanced settings

    master

    Use the following configuration keys to customize the HTTP client behavior for the Cloudflare provider.

    Config KeyEnv VarTypeDescription
    cloudflare:baseUrlCLOUDFLARE_BASE_URLStringOverrides the default HTTP client base URL.
    cloudflare:userAgentOperatorSuffixCLOUDFLARE_USER_AGENT_OPERATOR_SUFFIXStringAppends a value to the HTTP User Agent. Defaults to pulumi/<version>. Use an empty string to remove the Pulumi identifier.
  10. Resource ID mapping and delegation

    master

    The Cloudflare provider uses specific logic to compute resource IDs, often delegating to internal fields to ensure consistency with Cloudflare's API.

    Some resources use specific fields as their primary Pulumi ID:

    • cloudflare_ai_search_instance: Uses aiSearchInstanceId.
    • cloudflare_image: Uses imageId.
    • cloudflare_zero_trust_access_ai_controls_mcp_portal: Uses zeroTrustAccessAiControlsMcpPortalId.
    • cloudflare_zero_trust_access_ai_controls_mcp_server: Uses zeroTrustAccessAiControlsMcpServerId.
    • cloudflare_zone_setting: Uses settingId.
    • cloudflare_snippet: Uses snippetName.

    For resources where the ID is not explicitly provided by the API, the provider attempts to construct it using available context, such as combining accountId and zoneId.

  11. Migrate Cloudflare resources from v5 to v6

    master

    The Cloudflare provider includes PreStateUpgradeHook and TransformFromState logic to handle migrations from version 5 to version 6. If you are upgrading your stack, the following resources have specific schema transformations applied automatically:

    • cloudflare_ruleset: Migrates actionParameters.headers from an array to a MapNestedAttribute and updates actionParameters.rules from a map[string]string to a map[string][]string.
    • cloudflare_list: Flattens the v5 value wrapper (containing asn, ip, hostnames, redirects) into top-level fields (asn, ip, hostname, redirect).
    • cloudflare_zone: Transforms zone to name, accountId to account.id, and plan to plan.id.
    • cloudflare_zero_trust_access_application: Migrates cors_headers from a list to an object and policies from a list of strings to a list of objects containing id.
    • cloudflare_zero_trust_access_policy: Migrates boolean and string properties into nested objects (e.g., includes.certificate becomes an object).
    • cloudflare_workers_route: Ensures the id field is populated from the pattern field if id is missing.
  12. Create a DNS record

    master

    You can manage Cloudflare DNS records using the DnsRecord resource. Below are examples for various languages.

    import * as pulumi from "@pulumi/pulumi";
    import * as cloudflare from "@pulumi/cloudflare";
    
    // Create a DNS record
    const www = new cloudflare.DnsRecord("www", {});