Convert Figma logo to code with AI

cloudflare logoterraform-provider-cloudflare

Cloudflare Terraform Provider

1,087
721
1,087
138

Top Related Projects

The AWS Provider enables Terraform to manage AWS resources.

Terraform Provider for Google Cloud Platform

Terraform provider for Azure Resource Manager

Quick Overview

The Cloudflare Terraform Provider is an official plugin for Terraform that allows users to manage Cloudflare resources using infrastructure-as-code principles. It provides a way to automate the creation, modification, and deletion of Cloudflare services and configurations through Terraform.

Pros

  • Enables version-controlled, reproducible infrastructure management for Cloudflare resources
  • Supports a wide range of Cloudflare products and features
  • Regularly updated to include new Cloudflare capabilities
  • Well-documented with extensive examples and community support

Cons

  • Learning curve for users new to Terraform or infrastructure-as-code concepts
  • Some advanced Cloudflare features may not be fully supported or require workarounds
  • Potential for rate limiting when managing large numbers of resources
  • Occasional breaking changes in major version updates

Code Examples

  1. Creating a DNS record:
resource "cloudflare_record" "example" {
  zone_id = var.cloudflare_zone_id
  name    = "example.com"
  value   = "203.0.113.10"
  type    = "A"
  proxied = true
}
  1. Configuring a Page Rule:
resource "cloudflare_page_rule" "example" {
  zone_id = var.cloudflare_zone_id
  target  = "example.com/api/*"
  actions {
    ssl = "strict"
    cache_level = "aggressive"
  }
}
  1. Setting up a Worker route:
resource "cloudflare_worker_route" "example" {
  zone_id     = var.cloudflare_zone_id
  pattern     = "example.com/api/*"
  script_name = cloudflare_worker_script.example.name
}

resource "cloudflare_worker_script" "example" {
  name    = "api-worker"
  content = file("${path.module}/worker.js")
}

Getting Started

  1. Install Terraform and set up your Cloudflare API credentials.
  2. Create a new Terraform configuration file (e.g., main.tf) and add the Cloudflare provider:
terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 3.0"
    }
  }
}

provider "cloudflare" {
  api_token = var.cloudflare_api_token
}
  1. Define your Cloudflare resources using the provider's resource types.
  2. Run terraform init to initialize the working directory and download the provider.
  3. Use terraform plan to preview changes and terraform apply to apply them to your Cloudflare account.

Competitor Comparisons

The AWS Provider enables Terraform to manage AWS resources.

Pros of terraform-provider-aws

  • Broader scope and more comprehensive coverage of AWS services
  • Larger community and more frequent updates
  • Better integration with other AWS-specific tools and services

Cons of terraform-provider-aws

  • More complex and potentially overwhelming for users new to AWS
  • Slower release cycle due to the extensive range of services covered
  • May include unnecessary resources for users focused on specific AWS services

Code Comparison

terraform-provider-aws:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "Example Instance"
  }
}

terraform-provider-cloudflare:

resource "cloudflare_record" "example" {
  zone_id = "your_zone_id"
  name    = "example"
  value   = "203.0.113.10"
  type    = "A"
  proxied = true
}

The AWS provider example creates an EC2 instance, while the Cloudflare provider example creates a DNS record. This reflects the different focus areas of each provider, with AWS covering a wide range of cloud services and Cloudflare specializing in DNS and CDN-related resources.

Terraform Provider for Google Cloud Platform

Pros of terraform-provider-google

  • Broader ecosystem support with extensive GCP resource coverage
  • More mature and established provider with a larger community
  • Better integration with other Google Cloud services

Cons of terraform-provider-google

  • More complex configuration due to the wide range of GCP resources
  • Steeper learning curve for users new to GCP
  • Potentially slower release cycles due to the larger codebase

Code Comparison

terraform-provider-google:

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "e2-medium"
  zone         = "us-central1-a"
}

terraform-provider-cloudflare:

resource "cloudflare_record" "example" {
  zone_id = "example.com"
  name    = "test"
  value   = "203.0.113.10"
  type    = "A"
  ttl     = 3600
}

The Google provider example shows creating a compute instance, while the Cloudflare provider example demonstrates creating a DNS record. This highlights the different focus areas of each provider, with Google covering a broader range of cloud resources and Cloudflare specializing in DNS and edge services.

Terraform provider for Azure Resource Manager

Pros of terraform-provider-azurerm

  • Broader scope: Covers the entire Azure ecosystem, offering more comprehensive cloud infrastructure management
  • Deeper integration with Azure services: Provides access to a wider range of Azure-specific features and configurations
  • More frequent updates: Generally receives more regular updates and feature additions due to Azure's rapid development

Cons of terraform-provider-azurerm

  • Higher complexity: Managing Azure resources often requires more configuration and understanding of the Azure ecosystem
  • Steeper learning curve: Due to the breadth of Azure services, it may take longer to become proficient with this provider
  • Potentially slower performance: Managing a larger set of resources across Azure can lead to longer apply times in some cases

Code Comparison

terraform-provider-azurerm:

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

terraform-provider-cloudflare:

resource "cloudflare_record" "example" {
  zone_id = var.cloudflare_zone_id
  name    = "example"
  value   = "203.0.113.10"
  type    = "A"
  proxied = true
}

The Azure provider example shows creating a virtual network, demonstrating its focus on cloud infrastructure. The Cloudflare provider example shows creating a DNS record, highlighting its emphasis on edge network and DNS management.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Cloudflare Terraform Provider

The Cloudflare Terraform provider provides convenient access to the Cloudflare REST API from Terraform.

Requirements

This provider requires Terraform CLI 1.0 or later. You can install it for your system on Hashicorp's website.

Usage

Add the following to your main.tf file:

# Declare the provider and version
terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 5.8.2"
    }
  }
}

# Initialize the provider
provider "cloudflare" {
  # The preferred authorization scheme for interacting with the Cloudflare API. [Create a token](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/).
  api_token = "Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" # or set CLOUDFLARE_API_TOKEN env variable
  # The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.
  api_key = "144c9defac04969c7bfad8efaa8ea194" # or set CLOUDFLARE_API_KEY env variable
  # The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.
  api_email = "user@example.com" # or set CLOUDFLARE_EMAIL env variable
  # Used when interacting with the Origin CA certificates API. [View/change your key](https://developers.cloudflare.com/fundamentals/api/get-started/ca-keys/#viewchange-your-origin-ca-keys).
  user_service_key = "v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" # or set CLOUDFLARE_API_USER_SERVICE_KEY env variable
}

# Configure a resource
resource "cloudflare_zone" "example_zone" {
  account = {
    id = "023e105f4ecef8ad9ca31a8372d0c353"
  }
  name = "example.com"
  type = "full"
}

Initialize your project by running terraform init in the directory.

Additional examples can be found in the ./examples folder within this repository, and you can refer to the full documentation on the Terraform Registry.

Provider Options

When you initialize the provider, the following options are supported. It is recommended to use environment variables for sensitive values like access tokens. If an environment variable is provided, then the option does not need to be set in the terraform source.

PropertyEnvironment variableRequiredDefault value
user_service_keyCLOUDFLARE_API_USER_SERVICE_KEYfalse—
api_tokenCLOUDFLARE_API_TOKENfalse—
api_keyCLOUDFLARE_API_KEYfalse—
api_emailCLOUDFLARE_EMAILfalse—

Semantic versioning

This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:

  1. Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
  2. Changes that we do not expect to impact the vast majority of users in practice.

We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.

We are keen for your feedback; please open an issue with questions, bugs, or suggestions.

Maintenance

This SDK is actively maintained, however, many issues are tracked outside of GitHub on internal Cloudflare systems. Members of the community are welcome to join and discuss your issues during our weekly triage meetings. For urgent issues, please contact Cloudflare support.

Contributing

See the contributing documentation.