
In last month’s SAP Developers News featuring Code Connect, SAPInsider Challenge, Reuse Access Controls, BTP Terraform | SAP Developer News - YouT... I encountered the new Terraform provider designed for SAP BTP.
Consequently, I considered experimenting with it and documenting the process using my trial account.
Before jumping into the steps, please go through the following links for the details.
I am going to set up the account in two steps:
N.B: Please use two separate folders for the step 1 & 2.
Step 1: Setting up the BTP sub-account.
Here, I will use three files: provider.tf (which contains the provider details), main.tf (the config file), and trial-account.tfvars (which contains the variables) for setting up the BTP sub-account using Terraform.
Please check the below section for the details.
terraform {
required_providers {
btp = {
source = "SAP/btp"
version = "1.2.0"
}
}
}
provider "btp" {
globalaccount = "{global account id}-ga"
username = "your BTP email ID"
password = "your BTP password"
}
# variable declarations
variable "parent-dir-name" {
type = string
description = "This is a parent directory"
}
variable "child-dir-name" {
type = string
description = "This is a child directory"
}
variable "sub-account-name" {
type = string
description = "This is a sub-account"
}
variable "sub-account-cf-env-name" {
type = string
description = "This is cloud foundry environment"
}
variable "sub-account-role-collection-admin" {
type = string
description = "This is the sub-account admin role collection"
}
# setting up the btp parent directory
resource "btp_directory" "parent-dir-name" {
name = var.parent-dir-name
description = "This is a parent directory"
}
# setting up the btp sub directory with ENTITLEMENT and AUTHORIZATIONS features enabled
resource "btp_directory" "child-dir-name" {
parent_id = btp_directory.parent-dir-name.id
name = var.child-dir-name
description = "This is a child directory"
features = ["DEFAULT", "ENTITLEMENTS", "AUTHORIZATIONS"]
}
# setting up thr btp sub-account
resource "btp_subaccount" "sub-account-name" {
name = var.sub-account-name
subdomain = btp_directory.child-dir-name.id
region = "us10" #"ap21"
parent_id = btp_directory.child-dir-name.id
}
# creates a cloud foundry environment in a given account
resource "btp_subaccount_environment_instance" "cloudfoundry" {
subaccount_id = btp_subaccount.sub-account-name.id
name = var.sub-account-cf-env-name
environment_type = "cloudfoundry"
service_name = "cloudfoundry"
plan_name = "trial"
# some regions offer multiple environments of a kind and you must explicitly select the target environment in which
# the instance shall be created.
# available environments can be looked up using the btp_subaccount_environments datasource
parameters = jsonencode({
instance_name = var.sub-account-cf-env-name
})
}
# setting up the role collection
resource "btp_subaccount_role_collection" "sub-account-role-collection-admin" {
subaccount_id = btp_subaccount.sub-account-name.id
name = var.sub-account-role-collection-admin
description = "custom sub-account Administrator"
roles = [
{
name = "Subaccount Admin" # role name
role_template_app_id = "cis-local!b4" # application identifier
role_template_name = "Subaccount_Admin" # role template
},
{
name = "Subaccount Service Administrator" # role name
role_template_app_id = "service-manager!b1476" # application identifier
role_template_name = "Subaccount_Service_Administrator" # role template
},
{
name = "Destination Administrator" # role name
role_template_app_id = "destination-xsappname!b62" # application identifier
role_template_name = "Destination_Administrator" # role template
}
]
}
# assign a single ser to a role collection on subaccount level
resource "btp_subaccount_role_collection_assignment" "custom-sub-account-admin-role-col" {
subaccount_id = btp_subaccount.sub-account-name.id
role_collection_name = var.sub-account-role-collection-admin
user_name = "email id of the user"
}
# trial-account.tfvars contents
parent-dir-name = "test-parent-dir"
child-dir-name = "test-child-dir"
sub-account-name = "test-sub-account"
sub-account-cf-env-name = "test-cf-instance"
sub-account-role-collection-admin = "Custom Admin Role Collection"
# command
terraform apply -var-file="trial-account.tfvars"
The above codes will create the BTP sub-account shown in the screenshot below.
Step 2: Setting up the Cloud Foundry environment.
Here, I will use three files: provider.tf (which contains the provider details), main.tf (the config file), and terraform.tfvars (which contains the variables) for setting up the BTP sub-account using Terraform.
terraform {
required_providers {
cloudfoundry = {
source = "cloudfoundry-community/cloudfoundry"
version = "0.50.4"
}
}
}
provider "cloudfoundry" {
api_url = "https://api.cf.us10-xxx.hana.ondemand.com" # BTP CF API Endpoint
user = "your BTP email ID"
password = "your BTP password"
}
# variable declarations
variable "managers" {
type = list(any)
}
variable "developers" {
type = list(any)
}
variable "auditors" {
type = list(any)
}
# setting up the quota
# resource "cloudfoundry_org_quota" "large" {
# name = "large"
# allow_paid_service_plans = false
# instance_memory = 2048
# total_memory = 51200
# total_app_instances = 100
# total_routes = 50
# total_services = 200
# total_route_ports = 5
# }
# resource "cloudfoundry_org" "test-cf-instance" {
# name = "test-cf-instance"
# quota = cloudfoundry_org_quota.large.id
# }
# creating space
resource "cloudfoundry_space" "cf-devspace" {
name = "devspace"
org = "org id"
}
# creating space users
resource "cloudfoundry_space_users" "cf-devspace-users" {
space = cloudfoundry_space.cf-devspace.id
managers = var.managers
developers = var.developers
auditors = var.auditors
}
# creating services
data "cloudfoundry_service" "application-logs" {
name = "application-logs"
}
resource "cloudfoundry_service_instance" "application-logs-srv" {
name = "app-logs-srv"
space = cloudfoundry_space.cf-devspace.id
service_plan = data.cloudfoundry_service.application-logs.service_plans["lite"]
depends_on = [cloudfoundry_space_users.cf-devspace-users]
}
data "cloudfoundry_service" "destination" {
name = "destination"
}
resource "cloudfoundry_service_instance" "destination-service" {
name = "destination-service"
space = cloudfoundry_space.cf-devspace.id
service_plan = data.cloudfoundry_service.destination.service_plans["lite"]
depends_on = [cloudfoundry_space_users.cf-devspace-users]
}
data "cloudfoundry_service" "connectivity" {
name = "connectivity"
}
resource "cloudfoundry_service_instance" "connectivity-service" {
name = "connectivity-service"
space = cloudfoundry_space.cf-devspace.id
service_plan = data.cloudfoundry_service.destination.service_plans["lite"]
depends_on = [cloudfoundry_space_users.cf-devspace-users]
}
# contents
managers = ["xxx@gmail.com"]
auditors = ["xxx@gmail.com", "xxx@live.com"]
developers = ["xxx@gmail.com", "xxx@live.com"]
# command
terraform apply
The above codes will make changes to the BTP CF shown in the screenshot below.
That's it. 😀 I hope that you have found this simple blogpost helpful.
If I miss anything, please feel free to add in the comments.
Happy coding! 😀
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 |