Skip to Content

Get Started with the Terraform Provider for BTP

Learn how to use Terraform to manage resources on SAP Business Technology Platform using code.
You will learn
  • How to install and configure the Terraform Provider for BTP.
  • How to manage your BTP resources as code, making them more reproducible, maintainable, and scalable.
  • How to clean up your BTP resources when they are no longer needed, helping you avoid unnecessary costs and resource consumption.
v0lkcChristian VolkMay 7, 2024
Created by
v0lkc
July 28, 2023
Contributors
v0lkc
lechnerc77

Prerequisites

To follow along with this tutorial, ensure you have access to a free BTP trial account and Terraform installed on your machine. You can download it from the official Terraform website.

Terraform is an open-source tool that allows you to define and provide cloud infrastructure using a declarative configuration language. It helps in building, changing, and managing resources in a safe, consistent, and efficient manner.

By combining Terraform with SAP Business Technology Platform (BTP), you can programmatically manage your BTP environments.

  • Step 1

    Create a new directory for the tutorial content. This directory will serve as the workspace for your Terraform configuration files. Afterwards, please open a terminal or command prompt and navigate to the newly created directory.

    To install and configure the Terraform provider for BTP, add the following block to your Terraform configuration file (provider.tf).

    HCL
    Copy
    terraform {
      required_providers {
        btp = {
          source  = "SAP/btp"
          version = "~>1.3.0"
        }
      }
    }
    
    provider "btp" {
      globalaccount = "4605efebtrial-ga"
    }
    

    Replace "4605efebtrial-ga" with the subdomain of your own BTP trial account.

    After setting up the provider configuration, run the following command in the terminal to let Terraform download all necessary dependencies:

    Shell
    Copy
    terraform init
    
    terraform init

    Last but not least, you need to pass credentials to the provider to authenticate and interact with your BTP environments. Please set the environment variables BTP_USERNAME and BTP_PASSWORD.

    Replace <your_username> and <your_password> with your actual BTP username and password.

    You are now ready to use the Terraform provider for BTP to manage your resources.

  • Step 2

    With the provider set up, you can now provision resources on BTP. Let’s first create a subaccount using the btp_subaccount resource. Create a new file main.tf and add the following content:

    HCL
    Copy
    resource "btp_subaccount" "my_project" {
      name      = "My Project"
      subdomain = "my-project"
      region    = "us10"
    }
    

    This code creates a subaccount named “My Project” in the “us10” region. The name, region, and subdomain parameters are all required. Now that you’ve defined your resources, you can apply the configuration using the following command:

    Shell
    Copy
    terraform apply
    

    Terraform will prompt you to confirm the changes. Type yes and press Enter to proceed.

    run terraform apply to create subaccount

    The subaccount has just been created for you.

  • Step 3

    To set an entitlement for the subaccount, you can use the btp_subaccount_entitlement resource. Here’s an example of how to do that, please add the following content to your main.tf file:

    HCL
    Copy
    resource "btp_subaccount_entitlement" "alert_notification_service" {
      subaccount_id = btp_subaccount.my_project.id
      service_name  = "alert-notification"
      plan_name     = "standard"
    }
    

    This code assigns the “standard” plan of the “alert-notification” service to the previously created subaccount. The subaccount_id, service_name, and plan_name parameters are required.

    Once you’ve defined the entitlement, you can apply the changes using the following command:

    Shell
    Copy
    terraform apply
    

    Terraform will prompt you to confirm the changes. Type yes and press Enter to proceed.

    run terraform apply to assign entitlement to subaccount

    The subaccount is now entitled for the alert-notification service.

  • Step 4

    The Terraform Provider for SAP BTP offers a wide range of resources that you can manage using Terraform. In addition to creating subaccounts and setting entitlements, you can provision and manage other resources such as service instances, service bindings, role collections, and more.

    To get a better understanding of the capabilities of the provider and the available resources, check out the provider documentation at https://registry.terraform.io/providers/SAP/btp/latest/docs. It provides detailed information about each resource, including their properties and usage examples.

    What resources can *not* be managed using the Terraform Provider for BTP?

  • Step 5

    You may want to destroy the resources created in the previous examples when they are no longer needed. Terraform provides a convenient way to destroy resources using the terraform destroy command:

    Shell
    Copy
    terraform destroy
    

    Terraform will prompt you to confirm the destruction of the resources. Type yes and press Enter to proceed.

    run terraform destroy to destroy resources

    Terraform will then destroy the subaccount and removing any entitlements associated with it.

Back to top