Provider Configuration

3 minute read

Module Overview

Provider configuration is the foundation of your Terraform provider. It defines how users authenticate, configure endpoints, and customize provider behavior.

In this module, you’ll learn how to design and implement robust provider configuration that’s secure, flexible, and user-friendly.

What is Provider Configuration?

Provider configuration is specified in Terraform code like this:

provider "mycloud" {
  api_key  = var.mycloud_api_key
  endpoint = "https://api.mycloud.com"
  region   = "us-east-1"
  timeout  = 30
}

Your provider code defines the schema and handles the configuration.

What You’ll Learn

Core Concepts

  1. Provider Schema - Define configuration attributes
  2. Authentication - Handle API keys, tokens, certificates
  3. Client Initialization - Create and configure API clients
  4. Configuration Sources - Environment variables, files, defaults
  5. Validation - Ensure valid configuration
  6. Best Practices - Security, flexibility, user experience

Practical Skills

  • Design provider schemas
  • Implement multiple authentication methods
  • Initialize and share API clients
  • Handle configuration precedence
  • Validate provider configuration
  • Test provider configuration

Module Structure

This module contains 8 chapters:

Chapter 1: Provider Schema Design

Time: 30 minutes

Learn how to design provider configuration schemas:

  • Provider vs resource schemas
  • Configuration attributes
  • Optional vs required provider configuration
  • Defaults and precedence

Chapter 2: Authentication Methods

Time: 45 minutes

Implement various authentication strategies:

  • API keys and tokens
  • Username/password
  • OAuth flows
  • Certificate-based authentication
  • Multiple authentication methods

Chapter 3: Client Configuration

Time: 40 minutes

Initialize and configure API clients:

  • HTTP client setup
  • Timeout configuration
  • Retry logic
  • Custom headers
  • Base URL configuration

Chapter 4: Configuration Sources

Time: 35 minutes

Handle configuration from multiple sources:

  • Environment variables
  • Configuration files
  • Terraform variables
  • Provider defaults
  • Precedence rules

Chapter 5: Shared Client Pattern

Time: 30 minutes

Share clients across resources efficiently:

  • Provider data model
  • Client initialization
  • Passing clients to resources
  • Configuration updates

Chapter 6: Configuration Validation

Time: 30 minutes

Validate provider configuration:

  • Required configuration checks
  • Cross-field validation
  • Connection testing
  • Error handling

Chapter 7: Provider Configuration Lab

Time: 60 minutes

Hands-on: Build a complete provider with authentication:

  • Implement provider schema
  • Add multiple authentication methods
  • Initialize API client
  • Test configuration

Chapter 8: Practice Questions

Time: 30 minutes

Test your understanding with comprehensive questions covering all module topics.

Prerequisites

Before starting this module, you should have:

  • ✅ Completed Module 3 (Development Setup)
  • ✅ Completed Module 4 (Schema Design)
  • ✅ Understanding of Go structs and interfaces
  • ✅ Basic understanding of HTTP clients
  • ✅ Familiarity with API authentication

Learning Objectives

By the end of this module, you will be able to:

  • ✅ Design comprehensive provider configuration schemas
  • ✅ Implement multiple authentication methods
  • ✅ Initialize and configure API clients
  • ✅ Handle configuration from multiple sources
  • ✅ Share clients efficiently across resources
  • ✅ Validate provider configuration
  • ✅ Follow security best practices
  • ✅ Build production-ready provider configuration

Key Concepts Preview

Provider Schema

func (p *MyCloudProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            "api_key": schema.StringAttribute{
                Optional:  true,
                Sensitive: true,
            },
            "endpoint": schema.StringAttribute{
                Optional: true,
            },
            "region": schema.StringAttribute{
                Optional: true,
            },
        },
    }
}

Configuration Model

type MyCloudProviderModel struct {
    APIKey   types.String `tfsdk:"api_key"`
    Endpoint types.String `tfsdk:"endpoint"`
    Region   types.String `tfsdk:"region"`
}

Client Initialization

func (p *MyCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
    var config MyCloudProviderModel
    diags := req.Config.Get(ctx, &config)
    resp.Diagnostics.Append(diags...)
    
    // Initialize client
    client := api.NewClient(api.Config{
        APIKey:   config.APIKey.ValueString(),
        Endpoint: config.Endpoint.ValueString(),
        Region:   config.Region.ValueString(),
    })
    
    // Share with resources
    resp.ResourceData = client
    resp.DataSourceData = client
}

Real-World Example

Here’s what users will write:

# Multiple authentication options
provider "mycloud" {
  # Option 1: API key (from environment)
  api_key = var.api_key
  
  # Option 2: Username/password
  # username = "admin"
  # password = "secret"
  
  # Configuration
  endpoint = "https://api.mycloud.com"
  region   = "us-east-1"
  timeout  = 30
  
  # Advanced settings
  max_retries = 3
  debug       = false
}

# Use resources with configured provider
resource "mycloud_environment" "prod" {
  name = "production"
}

Your provider handles all the configuration complexity behind the scenes.

Common Provider Patterns

1. API Key Authentication

"api_key": schema.StringAttribute{
    Optional:            true,
    Sensitive:           true,
    MarkdownDescription: "API key for authentication. Can also be set via MYCLOUD_API_KEY environment variable.",
}

2. Endpoint Configuration

"endpoint": schema.StringAttribute{
    Optional:            true,
    MarkdownDescription: "API endpoint URL. Defaults to https://api.mycloud.com",
}

3. Timeout Settings

"timeout": schema.NumberAttribute{
    Optional:            true,
    MarkdownDescription: "Request timeout in seconds. Defaults to 30.",
}

Estimated Time

  • Reading: 3-4 hours
  • Hands-on Lab: 1 hour
  • Practice Questions: 30 minutes
  • Total: 4.5-5.5 hours

Get Started

Ready to build provider configuration? Let’s begin!

Start: Provider Schema Design →


Previous Module: ← Schema Design

Next Module: Resource Implementation →

Last updated: June 30, 2026

Updated: