Vercel Terraform Provider

repository·main·Indexed 41 days ago

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

The Vercel Terraform Provider allows developers to manage Vercel resources, including projects, domains, integrations, and Blob stores, using Terraform HCL. It provides data sources for retrieving metadata on access groups, aliases, attack challenge modes, and blob objects.

Tokens
85.6K
Snippets
239
Records
318
Agent score
50%

What's inside Vercel Terraform Provider

  1. Manage Custom SSL Certificates with vercel_custom_certificate

    main

    The vercel_custom_certificate resource allows Enterprise teams to upload their own custom SSL certificates to Vercel. While Vercel provides SSL certificates by default for all domains, this resource is used when you need to provide your own specific certificate and private key.

    For more detailed information on how Vercel handles these certificates, refer to the Vercel documentation.

  2. Manage project-level bulk redirects with vercel_bulk_redirects

    main

    The vercel_bulk_redirects resource manages the live, project-level bulk redirects for a Vercel project.

    Important Behavior: Each Terraform apply stages the entire configured set of redirects and promotes that version to production. This means the redirects attribute represents the full, complete set of redirects you want active for the project; you cannot use this resource to incrementally add single redirects without defining the existing ones in your configuration.

    resource "vercel_project" "example" {
      name = "example-project"
    }
    
    resource "vercel_bulk_redirects" "example" {
      project_id = vercel_project.example.id
    
      redirects = [
        {
          source                = "/old-path"
          destination           = "/new-path"
          status_code           = 307
          case_sensitive        = false
          query                 = false
        },
        {
          source                = "/blog"
          destination           = "https://example.com/blog"
          status_code           = 308
          case_sensitive        = true
          query                 = true
        },
      ]
    }
  3. Manage Feature Flag SDK Keys with vercel_feature_flag_sdk_key

    main

    The vercel_feature_flag_sdk_key resource allows you to create and manage project-scoped, environment-specific SDK keys for Vercel Feature Flags.

    Important Security Note: Vercel only returns the cleartext sdk_key and the connection_string at the moment of creation. Because subsequent reads from the Vercel API omit these sensitive values, this Terraform resource is designed to preserve the existing sensitive values in your Terraform state during later refreshes/reads.

    resource "vercel_project" "example" {
      name = "feature-flag-sdk-key-example"
    }
    
    resource "vercel_feature_flag_sdk_key" "example" {
      project_id  = vercel_project.example.id
      environment = "production"
      type        = "server"
      label       = "backend-sdk"
    }
    
    resource "vercel_project_environment_variable" "example" {
      project_id = vercel_project.example.id
      target     = ["production"]
      sensitive  = true
      key        = "FLAGS_CONNECTION_STRING"
      value      = vercel_feature_flag_sdk_key.example.connection_string
    }
  4. Map Identity Provider groups to Vercel IDs using vercel_dsync_groups

    main

    The vercel_dsync_groups data source provides a map attribute. This attribute is a map where the keys are the group names from your Identity Provider and the values are the corresponding Vercel group IDs. You can use the Terraform lookup function to dynamically retrieve these IDs for use in other resources, such as vercel_team_config.

    resource "vercel_team_config" "example" {
      id   = "team_xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      saml = {
        enforced = true
        roles = {
          # Use lookup to find the Vercel ID for the 'admin' group from the IdP
          lookup(vercel_dsync_groups.example.map, "admin") = {
            role = "OWNER"
          }
        }
      }
    }
  5. Create a Project Protection Bypass

    main

    A vercel_project_protection_bypass resource creates an automation bypass token. This token allows automation services (like CI/CD pipelines) to bypass Deployment Protection on a vercel_project by providing the token in the x-vercel-protection-bypass HTTP header.

    Key Behaviors

    • Multiple Bypasses: You can create multiple bypasses per project.
    • Environment Variable Exposure: Exactly one bypass per project can have is_env_var = true. This bypass is automatically exposed as the VERCEL_AUTOMATION_BYPASS_SECRET environment variable on deployments.
    • Automatic Promotion/Demotion: If you set is_env_var = true on a new bypass, Vercel automatically demotes the previous default bypass.
    • Default Requirement: Vercel requires exactly one default bypass per project. If a bypass is the only one on a project, it is automatically treated as the default. Setting is_env_var = false on a solo bypass is invalid.
    resource "vercel_project" "example" {
      name = "example-project"
    }
    
    # A bypass for a CI pipeline. Because this is the first bypass on the project,
    # Vercel automatically marks it as the env-var default (is_env_var = true),
    # and the secret is exposed as VERCEL_AUTOMATION_BYPASS_SECRET on deployments.
    resource "vercel_project_protection_bypass" "ci" {
      project_id = vercel_project.example.id
      note       = "ci pipeline"
    }
    
    # A second bypass for QA, with a caller-supplied secret.
    resource "vercel_project_protection_bypass" "qa" {
      project_id = vercel_project.example.id
      note       = "preview QA"
      secret     = "abcdefghijklmnopqrstuvwxyz123456"
    }
  6. Use the vercel_project_directory data source

    main

    The vercel_project_directory data source recursively reads files within a specified directory on your local filesystem. It provides metadata (such as file size and hashes) that is required to create a vercel_deployment via Terraform. This allows Terraform to detect file changes and trigger new deployments.

    Note: To prevent specific files from being included in the deployment metadata, use a .vercelignore file in your project directory.

    data "vercel_project_directory" "example" {
      path = "../ui"
    }
  7. Use existence operators for headers and cookies

    main

    When defining firewall conditions, you can check for the presence or absence of headers or cookies using existence operators.

    • ex (exists): Checks if a header or cookie is present.
    • nex (not exists): Checks if a header or cookie is absent.

    Important: When using existence operators (ex or nex), the value field is not required.

    rule {
      name        = "Require Authorization header"
      description = "Block requests without Authorization header"
      condition_group = [{
        conditions = [{
          type  = "header"
          key   = "Authorization"
          op    = "nex"
        }]
      }]
      action = {
        action = "deny"
      }
    }
  8. Manage Vercel Deployments with vercel_deployment

    main

    The vercel_deployment resource creates a Deployment for a Vercel Project. A Deployment is the result of building a Project and making it available via a live URL. Once the build step completes, a new, immutable deployment is available at a preview URL.

    Deployment Strategies

    Depending on your workflow, you can deploy in three ways:

    1. File-based Deployment: Uploading specific files using vercel_file or vercel_project_directory data sources. This is required if git_source is not set.
    2. Git-based Deployment: Deploying a specific branch or commit hash (ref) for projects connected to a Git repository.
    3. Prebuilt Deployment: Uploading the output of a pre-existing build process.

    Best Practices

    • Layering: If you manage both preview and production deployments, consider 'layering' your Terraform by creating the vercel_project in one configuration and the vercel_deployment in another.
    • Retention: Deployments are retained indefinitely by default. To hard delete a deployment when the Terraform resource is destroyed, set delete_on_destroy = true. Note that deleted deployments are not recoverable.
    # Example: File-based deployment
    data "vercel_project_directory" "files_example" {
      path = "../ui"
    }
    
    data "vercel_project" "files_example" {
      name = "my-awesome-project"
    }
    
    resource "vercel_deployment" "files_example" {
      project_id  = data.vercel_project.files_example.id
      files       = data.vercel_project_directory.files_example.files
      path_prefix = data.vercel_project_directory.files_example.path
      production  = true
    
      environment = {
        FOO = "bar"
      }
    }
  9. Wait for domain verification with `wait_for_ready`

    main

    If you have resources that depend on a domain being fully functional, set wait_for_ready = true. This tells Terraform to wait until the domain is verified and has a valid DNS configuration before proceeding.

    Note: You must configure DNS records independently before enabling this, as Terraform will pause until the DNS configuration is detected as valid.

    resource "vercel_project_domain" "example_wait_for_ready" {
      project_id     = vercel_project.example.id
      domain         = "i-wait.vercel.app"
      wait_for_ready = true
    }
  10. Manage project-level routing rules with vercel_project_route

    main

    The vercel_project_route resource manages a single live project-level routing rule for a Vercel project.

    Key Behaviors

    • Immediate Promotion: Each mutation stages a new routing-rules version and promotes it immediately. This ensures Terraform state reflects actual production traffic behavior.
    • Targeting: Reads and imports target the live version and ignore unpublished staged drafts created outside of Terraform.
    • Ordering: Use the position block to control rule placement. For deterministic ordering across multiple rules, use before or after in conjunction with reference_route_id.
    • Constraint: This resource will refuse to mutate a project if it currently has an unpublished staged routing-rules version. You must publish, restore, or discard the existing draft before using this resource.

    Important Note on Position

    The Vercel API does not return placement metadata for individual rules. While Terraform preserves the configured position during normal reads, imported routes will start with no position in the state.

    resource "vercel_project_route" "example" {
    	project_id = vercel_project.example.id
    	name       = "redirect-legacy-docs"
    	position = {
    		placement = "start"
    	}
    	route = {
    		src    = "/docs/:path*"
    		dest   = "/guides/:path*"
    		status = 308
    	}
    }
  11. Connect a Git repository to a Vercel project

    main

    To enable automatic deployments on every branch push or merge, use the git_repository block within vercel_project. This requires the corresponding Vercel plugin (GitHub, GitLab, or Bitbucket) to be installed. You can also specify a production_branch to override the default main branch behavior.

    resource "vercel_project" "git_connected" {
      name = "my-git-project"
    
      git_repository {
        repo             = "vercel/next.js"
        type             = "github"
        production_branch = "main"
    
        deploy_hooks {
          name = "on-push"
          ref  = "main"
        }
      }
    }