Orchestrating Infrastructure as Code: Terraform Integration with Bitbucket Cloud and Data Center

The convergence of Infrastructure as Code (IaC) and DevOps pipelines has become the standard for modern cloud operations. At the center of this workflow lies Terraform, the leading tool for provisioning complex cloud infrastructure. For teams utilizing Atlassian’s Bitbucket as their source control system, the challenge is not merely running Terraform, but integrating it seamlessly with the version control lifecycle, access controls, and approval gates. This article details the architectural and operational requirements for integrating Terraform with both Bitbucket Cloud and Bitbucket Data Center, covering the nuances of Bitbucket Pipelines, OAuth configurations, SSH key management, and the critical deprecations affecting Bitbucket Server.

Fundamental Concepts and Architectural Divergence

When integrating Terraform with Bitbucket, it is essential to distinguish between the two primary environments: Bitbucket Cloud and Bitbucket Data Center. Each environment requires a distinct integration strategy based on how they expose their APIs and handle authentication. Bitbucket Cloud, being a fully managed service, relies heavily on OAuth 2.0 for application integrations and native CI/CD services like Bitbucket Pipelines. In contrast, Bitbucket Data Center is a self-hosted solution that often operates behind corporate firewalls, requiring specific network configurations and SSH-based authentication for external tools like HCP Terraform or Terraform Enterprise to pull repository contents.

A critical operational shift has occurred regarding the legacy "Bitbucket Server." Atlassian ended support for Bitbucket Server on February 15, 2024. Consequently, the ecosystem has migrated to Bitbucket Data Center (version 8.0 or newer) and Bitbucket Cloud. For Terraform users, this migration is not optional. HCP Terraform ended support for Bitbucket Server on August 15, 2024, and Terraform Enterprise will cease support in version v202410. Teams still running Bitbucket Server must upgrade to Bitbucket Data Center to maintain integration with modern Terraform platforms. This deprecation eliminates the older, less secure integration methods and enforces the use of modern authentication protocols like OAuth and standardized SSH keys.

Implementing Terraform in Bitbucket Pipelines

For teams using Bitbucket Cloud, the most native integration path is Bitbucket Pipelines. This service runs builds in Docker containers, eliminating the need for dedicated build agents. A robust Terraform workflow in Pipelines typically separates the plan and apply phases to introduce manual approval gates, ensuring that infrastructure changes are reviewed before execution.

The configuration begins by enabling Pipelines in the repository settings and creating a bitbucket-pipelines.yml file in the repository root. The following configuration demonstrates a basic plan-and-apply workflow using the hashicorp/terraform:1.7.5 Docker image. This setup ensures that Terraform is initialized without a backend for validation, then performs a plan that is saved as an artifact. The apply step is configured as a manual trigger, preventing automatic execution and requiring human intervention.

```yaml

bitbucket-pipelines.yml

Basic Terraform pipeline

image: hashicorp/terraform:1.7.5
pipelines:
default:
- step:
name: Validate
script:
- cd terraform
- terraform init -backend=false
- terraform validate
- terraform fmt -check
branches:
main:
- step:
name: Plan
script:
- cd terraform
- terraform init -input=false
- terraform plan -out=tfplan -input=false
- terraform show -no-color tfplan > plan.txt
artifacts:
- terraform/tfplan
- terraform/plan.txt
- step:
name: Apply
trigger: manual
deployment: production
script:
- cd terraform
- terraform init -input=false
- terraform apply -input=false tfplan
```

In this configuration, the default pipeline runs on all branches to validate syntax and formatting. The main branch triggers a specific sequence: first, a Plan step initializes the Terraform state, generates a plan file (tfplan), and exports a human-readable diff to plan.txt. Both files are declared as artifacts to persist for the next step. The subsequent Apply step is marked with trigger: manual and deployment: production, which integrates with Bitbucket’s deployment tracking features. This allows teams to view the deployment status within the Bitbucket interface and requires an explicit "Run" action to execute the apply command.

Managing Secrets and Variables

Security in CI/CD pipelines depends on proper secret management. Cloud provider credentials, such as AWS keys, should never be hardcoded in the pipeline definition or committed to the repository. Instead, they must be stored as repository variables. In Bitbucket, these are accessed via Repository Settings under "Repository variables." For example, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY should be stored as secured variables. The pipeline then references these variables, and Bitbucket injects them as environment variables during step execution. This ensures that sensitive data remains encrypted at rest and is only available to the pipeline steps that require it.

Connecting HCP Terraform to Bitbucket Cloud

For organizations using HCP Terraform or Terraform Enterprise, the integration with Bitbucket Cloud is primarily handled via OAuth 2.0 and SSH keys. This approach allows HCP Terraform to monitor repository changes, trigger runs on commits, and fetch configuration files.

OAuth Configuration for Bitbucket Cloud

The OAuth connection begins by navigating to the VCS Providers page in HCP Terraform. After selecting Bitbucket Cloud, the user is prompted to log into Bitbucket with the account that HCP Terraform will act as. It is best practice to use a dedicated service account with administrative access to the relevant repositories, as creating webhooks requires admin permissions.

The integration requires the creation of an OAuth consumer in Bitbucket. This is done by navigating to https://bitbucket.org/<YOUR_WORKSPACE_NAME>/workspace/settings/oauth-consumers/new. The following table outlines the specific values that must be entered into the Bitbucket OAuth consumer form, which are provided by the HCP Terraform interface.

Field Value
Name HCP Terraform (<YOUR ORGANIZATION NAME>)
Description Any description of your choice
Callback URL https://app.terraform.io/<YOUR CALLBACK URL>
URL https://app.terraform.io (or Terraform Enterprise instance URL)

In addition to the text fields, specific permissions must be checked in the OAuth consumer settings. The "This is a private consumer" option must be selected. The required permission checkboxes are:
- Account: Write
- Repositories: Admin
- Pull requests: Write
- Webhooks: Read and write

After saving the consumer, the user retrieves the Key and Secret from the OAuth settings page. These credentials are then entered into the HCP Terraform interface to complete the connection. The final step is authorizing the app by clicking "Grant access" on the Bitbucket prompt.

SSH Key Requirements for Bitbucket Cloud

While OAuth handles the API communication, HCP Terraform also requires an SSH key to clone the repository content. The SSH key must be generated on a secure workstation with an empty passphrase. HCP Terraform cannot use SSH keys that require a passphrase because the automation process cannot interactively prompt for the passphrase.

The public key is added to the SSH Keys settings page of the Bitbucket Cloud account being used. The private key is then pasted into the "Add SSH Key" field in the HCP Terraform VCS provider configuration. This dual authentication model (OAuth for API actions, SSH for git operations) ensures secure and audited access to the infrastructure code.

Integrating Terraform Enterprise with Bitbucket Data Center

Connecting Terraform Enterprise to Bitbucket Data Center involves more complex network and configuration requirements due to the self-hosted nature of the product. The integration process requires OAuth authentication credentials for Bitbucket Data Center and specific network connectivity.

Network and Port Requirements

HCP Terraform and Terraform Enterprise must be able to contact Bitbucket Data Center over both SSH and HTTP/HTTPS during setup and normal operation. If the Bitbucket Data Center instance is behind a firewall, the necessary ports must be opened to the Terraform platform. The default ports for Bitbucket Data Center are:
- Port 7999 for SSH
- Port 7990 for HTTP

If Bitbucket returns a 500 error instead of the authorization screen, it is often an indication that Terraform was unable to reach the Bitbucket Data Center instance. Troubleshooting this connectivity issue is a primary step in successful integration.

SSH Key Generation and Management

For Bitbucket Data Center, the SSH key generation process is similar to Cloud but requires strict attention to the key format. The following example demonstrates generating an RSA key in PEM format on a Linux system.

bash $ ssh-keygen -t rsa -m PEM -f "/Users/<NAME>/.ssh/service_terraform" -C "service_terraform_enterprise"

The command creates two files: service_terraform (the private key) and service_terraform.pub (the public key). It is critical that no passphrase is specified during this generation. If a passphrase is set, the integration will fail because Terraform cannot decrypt the key automatically.

The setup procedure for Bitbucket Data Center includes the following steps:
1. Add a new VCS provider to HCP Terraform or Terraform Enterprise.
2. Create a new application link in Bitbucket Data Center.
3. Create the SSH key pair as described above.
4. Add the public SSH key to Bitbucket Data Center.
5. Add the private SSH key to Terraform.

User Authentication and Permissions

A common pitfall in Data Center integration is the user account used for authorization. When connecting to HCP Terraform, the user must log in to Bitbucket with the specific account that Terraform will use to access the repository. Logging in as a Bitbucket administrator is not recommended for this step; instead, a dedicated service account should be used. If the user is logged in as an administrator, they must log out before proceeding with the authorization.

Furthermore, the user performing the integration must have permission to manage VCS settings for the organization. This permission ensures that the application link and webhook configurations are valid. The OAuth credentials for Bitbucket Data Center must also be provided to Terraform to facilitate the API interactions required for triggering runs and updating statuses.

Advanced Configuration and Scoping

Once the basic connection is established, administrators can fine-tune the integration through Advanced Settings. In HCP Terraform, the "Scope of VCS Provider" allows organizations to configure which workspaces or Stacks can use repositories from this VCS provider. This is particularly useful in large organizations where different teams manage different sets of infrastructure. By scoping the VCS provider, administrators can prevent unrelated teams from accessing sensitive infrastructure code repositories, thereby adhering to the principle of least privilege.

For Bitbucket Data Center, the configuration also allows for the management of which repositories are visible to the Terraform instance. This is crucial for maintaining security boundaries in multi-tenant environments. The integration supports the tracking of deployments, allowing teams to see the status of Terraform runs directly within the Bitbucket interface, alongside code reviews and pull requests.

Conclusion

The integration of Terraform with Bitbucket is a multi-faceted process that depends heavily on whether the organization uses Bitbucket Cloud or Bitbucket Data Center. For Bitbucket Cloud, the integration is streamlined through native Pipelines and standard OAuth configurations, offering a robust path for CI/CD workflows with manual approval gates. The bitbucket-pipelines.yml configuration demonstrates how to enforce security by separating plan and apply steps, ensuring that infrastructure changes are reviewed before execution.

For Bitbucket Data Center, the integration requires a more hands-on approach involving specific network port configurations (7999 and 7990), careful SSH key generation without passphrases, and precise user permission management. The deprecation of Bitbucket Server is a significant factor that teams must address, as HCP Terraform and Terraform Enterprise have ended support for the legacy platform, necessitating a migration to Bitbucket Data Center or Cloud.

In both environments, the security of the integration relies on the proper management of secrets and credentials. Using dedicated service accounts, securing repository variables, and enforcing empty passphrases for SSH keys are not just best practices but requirements for a stable and secure Terraform deployment. By adhering to these technical specifications, organizations can achieve a seamless, auditable, and secure infrastructure automation pipeline that scales with their cloud operations.

Sources

  1. How to Set Up Terraform in Bitbucket Pipelines
  2. Bitbucket Data Center VCS Provider Configuration
  3. Bitbucket Cloud VCS Provider Configuration

Related Posts