Skip to content

Latest commit

 

History

History
147 lines (94 loc) · 7.07 KB

README.md

File metadata and controls

147 lines (94 loc) · 7.07 KB

Exercise 2 - Create a Subaccount

In this exercise, we will use Terraform to create a subaccount in your global account.

Note

You find the solution for this exercise in the solutions/ex2 folder.

Exercise 2.1 - Create the Terraform configuration

After completing these steps, you will have defined the configuration for the subaccount creation using Terraform.

To setup a subaccount we must identify the appropriate Terraform resource. The official documentation will help us with this.

As we want to create a subaccount the resource we are looking for is btp_subaccount. In the documentation we find all the required and optional parameters we can use to configure the subaccount. We want to use the following parameters for our subaccount:

  • name - the name of the subaccount. As we do not want to hard code the name, we will be using a variable for this.
  • subdomain - The value for the subdomain. To keep this unique we want to define this as a combination of a user-provided prefix plus a random UUID. No matter what the user provides as prefix, we will always transform the value to lowercase and replace all _ with -.
  • region - the region where the subaccount should be created. We want to be flexible, so we will use a variable for this. To make it easy for the user we ensure that no matter how the variable is provided, we always transform the value to lowercase.

Let's do that.

  1. First we define the variables that we later use in the configuration. Open the file variables.tf and add the following code to define the three variables for subaccount domain prefix, subaccount name and region:

    variable "subaccount_domain_prefix" {
      type        = string
      description = "The prefix used for the subaccount domain"
      default     = "TechEd2024-TF"
    }
    
    variable "subaccount_name" {
      type        = string
      description = "The subaccount name."
      default     = "TechEd2024-experiments"
    }
    
    variable "region" {
      type        = string
      description = "The region where the subaccount shall be created in."
      default     = "us10"
    }

    It is best practice to provide a meaningful description for the variables. As you can see we also provided default values for the variables. Feel free to change them to your liking. Save your changes once you are finished.

    As we have defined the necessary input, we can now start with the actual configuration.

  2. As we need to construct the value of the subaccount domain we must use a few features of Terraform to achieve this, namely:

    Let us bring this together. Open the main.tf file and add the following code to fetch a random UUID and construct the subaccount domain:

    resource "random_uuid" "uuid" {}
    
    locals {
      subaccount_domain = lower(replace("${var.subaccount_domain_prefix}-${random_uuid.uuid.result}", "_", "-"))
    }
  3. With this we can continue to define the actual subaccount resource. Add the following code to the main.tf file:

    resource "btp_subaccount" "sa_build" {
      name      = var.subaccount_name
      subdomain = local.subaccount_domain
      region    = lower(var.region)
    }

    We are using the variables we defined before and the local variable to fill the required parameters of the resource. We are also using the lower function inline to ensure that the region is always in lowercase.

    Save your changes.

Exercise 2.2 - Execute Terraform

After completing these steps, you will have executed the Terraform configuration and successfully created a subaccount in your global account.

Ideally you need to be in the same Terminal window you used before.

This is especially the case if you are not using a devcontainer.env file in conjunction with a dev container, and have exported the BTP_USERNAME and BTP_PASSWORD environment variables in the shell session. If you do have to start a new Terminal window, make sure you export the two environment variables again.

  1. Ensure you're in the terraform_build/ directory that you already created.

  2. First we want to make sure that everything is properly formatted. The Terraform CLI supports us with this.

    terraform fmt

    This command executes the formatting of your configuration so that it is compliant with the Terraform best practices.

  3. As we are using Terraform for the first time, we must initialize the setup. We do so via the command:

    terraform init

    Your should see the output similar to the following in the terminal:

    Output of the Terraform init command

    This will initialize the setup and download the required provider. You can also check your file system to see that a .terraform directory and a file called .terraform.lock.hcl have been created.

    Terraform init file changes

  4. Now we want to make a static check of our configuration to make sure that the configuration at design time is syntactically correct. We do so via the the following command:

    terraform validate

    Outout terraform validate

    That looks good. So time to take the real actions.

  5. Next we execute the so called planning to see what Terraform wants to change:

    terraform plan -out=tfplan

    This reuslts in the following output:

    Outout terraform plan

    As expected it shows that the subaccount will be created. We saved the plan via the -out parameter to a file for using it in the apply. As the result matches our expectations we apply the configuration:

    terraform apply tfplan

    This results in the following output:

    Outout terraform apply

    As a result you see in the output that Terraform successfully created the subaccount. You can also check the SAP BTP cockpit to see the newly created subaccount:

    New subaccount in the SAP BTP Cockpit

Summary

You've now made the first step in defining your SAP BTP Infrastructure as Code namely a subaccount. Let's add some more resources to the setup now!

Continue to - Exercise 3 - Setup of SAP Build Code