Prerequisites
Terraform version
The Microsoft 365 provider requires:- Terraform >= 1.14.x
Microsoft 365 requirements
You’ll need:- A Microsoft 365 tenant with appropriate licenses
- Global Administrator or sufficient permissions to create Entra ID app registrations
- Access to the Azure Portal or Entra ID Admin Center
Step 1: Add the provider to your configuration
Create or update your Terraform configuration to include the provider:Replace
~> 0.40.0 with the latest version from the Terraform Registry. The provider follows Semantic Versioning.Step 2: Initialize Terraform
Run the following command to download and install the provider:Step 3: Create an Entra ID application
The provider uses an Entra ID (formerly Azure AD) application registration for authentication.Create application via Azure Portal (recommended for beginners)
Create application via Azure Portal (recommended for beginners)
- Sign in to the Azure Portal
- Navigate to Microsoft Entra ID (formerly Azure Active Directory)
- Select App registrations from the left menu
- Click + New registration
- Configure the application:
- Name:
Terraform Microsoft 365 Provider(or your preferred name) - Supported account types: Select “Accounts in this organizational directory only”
- Redirect URI: Leave blank for client credentials flow
- Name:
- Click Register
- Application (client) ID - You’ll use this as
client_id - Directory (tenant) ID - You’ll use this as
tenant_id
Create application via Azure CLI
Create application via Azure CLI
Create application via PowerShell
Create application via PowerShell
Step 4: Create a client secret or certificate
You need to create credentials for authentication. You can use either a client secret (easier) or a certificate (more secure).- Client Secret
- Client Certificate
Create a client secret
Via Azure Portal
- In your application’s page, select Certificates & secrets
- Under Client secrets, click + New client secret
- Add a description (e.g., “Terraform Provider Secret”)
- Select an expiration period:
- 180 days (6 months) - Recommended for testing
- 365 days (12 months) - For development
- 730 days (24 months) - For production (with rotation plan)
- Click Add
- Important: Copy the secret Value immediately - it will only be shown once
Via Azure CLI
Via PowerShell
Step 5: Grant API permissions
Your application needs Microsoft Graph API permissions to manage Microsoft 365 resources.Required permissions
The specific permissions depend on which resources you’ll manage. Here are common permission sets:Basic user and group management
Basic user and group management
Application permissions (recommended for automation):
User.ReadWrite.All- Create, read, update, and delete usersGroup.ReadWrite.All- Create, read, update, and delete groupsGroupMember.ReadWrite.All- Manage group membershipsDirectory.ReadWrite.All- Read and write directory data
Conditional Access and security
Conditional Access and security
Application permissions:
Policy.ReadWrite.ConditionalAccess- Manage conditional access policiesPolicy.ReadWrite.AuthenticationMethod- Manage authentication methodsRoleManagement.ReadWrite.Directory- Manage directory rolesApplication.ReadWrite.All- Manage applications and service principals
Intune and device management
Intune and device management
Application permissions:
DeviceManagementConfiguration.ReadWrite.All- Manage device configurationsDeviceManagementManagedDevices.ReadWrite.All- Manage devicesDeviceManagementApps.ReadWrite.All- Manage mobile appsDeviceManagementServiceConfig.ReadWrite.All- Manage service configuration
Microsoft Teams
Microsoft Teams
Application permissions:
TeamSettings.ReadWrite.All- Manage team settingsTeamsApp.ReadWrite.All- Manage Teams apps
Add permissions via Azure Portal
- In your application’s page, select API permissions
- Click + Add a permission
- Select Microsoft Graph
- Choose Application permissions (for service-to-service authentication)
- Search for and select the permissions you need
- Click Add permissions
- Important: Click Grant admin consent for [Your Organization]
Add permissions via Azure CLI
Add permissions via PowerShell
Step 6: Configure the provider
Now you can configure the provider with your credentials. The provider supports 11 authentication methods.Method 1: Client Secret (most common)
Method 2: Client Certificate
Method 3: Managed Identity (for Azure VMs)
Method 4: Azure Developer CLI
Ideal for local development:Method 5: GitHub Actions OIDC
For CI/CD pipelines:View all 11 authentication methods
View all 11 authentication methods
The provider supports these authentication methods:
- azure_developer_cli - Uses Azure Developer CLI identity
- azure_cli - Uses Azure CLI identity
- client_secret - Client ID and secret
- client_certificate - Client certificate (.pfx)
- device_code - Device code flow (interactive)
- interactive_browser - Opens browser for login
- managed_identity - Azure managed identity
- workload_identity - Kubernetes workload identity
- oidc - Generic OpenID Connect
- oidc_github - GitHub Actions OIDC
- oidc_azure_devops - Azure DevOps OIDC
Environment variables reference
All provider configuration can be set via environment variables:| Environment Variable | Description | Example |
|---|---|---|
M365_TENANT_ID | Microsoft 365 tenant ID | 00000000-0000-0000-0000-000000000000 |
M365_CLIENT_ID | Application (client) ID | 11111111-1111-1111-1111-111111111111 |
M365_CLIENT_SECRET | Client secret value | your-secret-value |
M365_AUTH_METHOD | Authentication method | client_secret, client_certificate, etc. |
M365_CLOUD | Cloud environment | public, gcc, gcchigh, dod, china |
M365_CLIENT_CERTIFICATE_FILE_PATH | Path to certificate file | /path/to/cert.pfx |
M365_CLIENT_CERTIFICATE_PASSWORD | Certificate password | cert-password |
M365_TELEMETRY_OPTOUT | Disable telemetry | true or false |
M365_DEBUG_MODE | Enable debug logging | true or false |
M365_USE_PROXY | Enable HTTP proxy | true or false |
M365_PROXY_URL | Proxy server URL | http://proxy.example.com:8080 |
Environment variables override values set in the provider configuration block.
Advanced configuration
Client options
Configure HTTP client behavior:Proxy configuration
For environments requiring HTTP proxy:Multi-cloud support
For US Government or other clouds:public- Microsoft Azure Public Cloud (default)gcc- US Government Cloudgcchigh- US Government High Clouddod- US Department of Defense Cloudchina- Microsoft Cloud Chinaex- EagleX Cloudrx- Secure Cloud (RX)
Troubleshooting
Provider not found
If you see “provider not found” errors:- Verify the source is
deploymenttheory/microsoft365 - Run
terraform initto download the provider - Check the version constraint is valid
Authentication failures
Error: Invalid client secret- Verify the client secret value is correct
- Check if the secret has expired
- Create a new secret if needed
- Verify API permissions are granted
- Ensure admin consent has been granted
- Check the application has the required roles
Permission errors
Error: Forbidden (403)- The application lacks required API permissions
- Add the necessary permissions and grant admin consent
- Wait a few minutes for permission changes to propagate
Enable debug logging
For detailed troubleshooting:Security best practices
Recommended security measures
- Use certificate-based authentication instead of client secrets for production
- Rotate secrets regularly - Set short expiration periods and automate rotation
- Grant least privilege - Only assign required API permissions
- Use managed identity when running on Azure infrastructure
- Enable debug mode only for troubleshooting - It logs sensitive information
- Monitor application usage - Review sign-in logs in Entra ID
Secret rotation
Plan for credential rotation:Next steps
Try the quick start
Deploy your first resources with the Quick start guide
Explore resources
Browse available resources in the Terraform Registry
Get help
Discord community
Ask questions and get help from other users
GitHub issues
Report bugs or request features
