Skip to content

update readme for SIT hands-on #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion released/SAP-Inside-Tracks/SITBLR_DEC_2024/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SITBLR DECEMBER 2024 - HandsOn SAP Terraform Provider for SAP BTP

## Goal of this HandsOn 🎯
## Goal of this Hands-on 🎯

In this hands-on exercise you will learn how to use the [Terraform Provider for SAP BTP](https://registry.terraform.io/providers/SAP/btp/latest/docs) to provision and manage resources in SAP BTP. The level of the exercises is beginner. You don't need any prior knowledge about Terraform or the Terraform Provider for SAP BTP. We will guide you through the exercises step by step.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ terraform {

provider "btp" {
globalaccount = var.globalaccount
idp = var.idp
}
```

What have we done? First we defined which provider we want to use and which version of the provider we want to use. In this case we want to use the provider `sap/btp` in version `1.8.0` (including potential patch versions). Then we defined the provider configuration. In this case we only need to provide the `globalaccount` parameter where we reference a variable. We will define this variable in the next step.
What have we done? First we defined which provider we want to use and which version of the provider we want to use. In this case we want to use the provider `sap/btp` in version `1.8.0` (including potential patch versions). Then we defined the provider configuration. In this case we need to provide the `globalaccount` and `idp` parameters where we reference a variable. We will define this variable in the next step.

> [!NOTE]
> We do not need any authentication information in this file. We provided the authentication information via environment variables.
Expand All @@ -49,18 +50,26 @@ variable "globalaccount" {
type = string
description = "The subdomain of the SAP BTP global account."
}
variable "idp" {
type = string
description = "Orgin key of Identity Provider"
default = null
}
```

We have now defined the variable `globalaccount` which is required for the provider configuration. We will provide the value for this variable via the `terraform.tfvars` file. Open
We have now defined the variable `globalaccount` and `idp` which is required for the provider configuration. We will provide the value for this variable via the `terraform.tfvars` file. Open
the file `terraform.tfvars` and add the following content:

```terraform
globalaccount = "<YOUR GLOBAL ACCOUNT SUBDOMAIN>"
idp = null
```

The SAP BTP Global Account Subdomain can be found in the SAP BTP Cockpit as shown below
<img width="600px" src="assets/trial-account.png" alt="SAP BTP Global Account Subdomain">

The `idp` (Identity Provider Orgin Key ) is set to null. If a [Custom Identity Provider](https://help.sap.com/docs/btp/sap-business-technology-platform/log-on-with-custom-identity-provider-to-sap-btp-cockpit) is used to login to SAP BTP this value is set to Orgin Key of the Custom Identity Provider`

> [!NOTE]
> We are using here a naming convention of Terraform to define the variable values. The file `terraform.tfvars` is used to define the variable values. The file is not checked into the source code repository. This is important to keep sensitive information out of the source code repository. When you run Terraform, it will automatically load the variable values from this file.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ As we have all variables in place, you should save the changes now.
> [!NOTE]
> As you can see you have a lot of possibilities to validate the user input in Terraform in this way ensure that the input is correct and meets your corporate requirements.

We have defined the `project_name` which is required for the subaccount creation. We will provide the value for this variable via the `terraform.tfvars` file. Open
the file `terraform.tfvars` and append the following content to the end of the file:

```terraform
project_name = "<YOUR LAST NAME>"
```

### Step 2: Local values

Now we want to leverage the input variables to create a subaccount name that follows a specific naming convention. Here are the conditions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ variable "bas_plan" {
}
```

We define a complex variable type, which is a [list](https://developer.hashicorp.com/terraform/language/expressions/types#lists-tuples) of [objects](https://developer.hashicorp.com/terraform/language/expressions/types#maps-objects). Each object has three attributes: `name`, `plan`, and `amount`. The `name` and `plan` attributes are strings, and the `amount` attribute is a number. We define a default value for the variable, which is an empty list.We will provide values for this parameter via the file `terraform.tfvars` in the next step. Save the changes.
We define a complex variable type, which is a [list](https://developer.hashicorp.com/terraform/language/expressions/types#lists-tuples) of [objects](https://developer.hashicorp.com/terraform/language/expressions/types#maps-objects). Each object has three attributes: `name` and `plan`. The `name` and `plan` attributes are strings. We define a default value for the variable, which is an empty list.We will provide values for this parameter via the file `terraform.tfvars` in the next step. Save the changes.

## Step 2: Add the variable to the tfvars file

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ variable "cf_landscape_label" {
description = "The region where the project account shall be created in."
default = "cf-us10-001"
}
variable "cf_plan" {
type = string
description = "Plan name for Cloud Foundry Runtime."
default = "standard"
}
```

Then add the following code to the `main.tf`
Expand All @@ -35,7 +40,7 @@ resource "btp_subaccount_environment_instance" "cloudfoundry" {
landscape_label = var.cf_landscape_label
environment_type = "cloudfoundry"
service_name = "cloudfoundry"
plan_name = "trial"
plan_name = var.cf_plan
parameters = jsonencode({
instance_name = local.project_subaccount_cf_org
})
Expand All @@ -46,8 +51,15 @@ resource "btp_subaccount_environment_instance" "cloudfoundry" {
}
}
```
### Step 2: Add the variables to tfvar file

Add following variables to your `tfvars` file to configure the CloudFoundry Plan.

### Step 2: Adjust the output variables
```terraform
cf_plan = "trial"
```
Save the changes.
### Step 3: Adjust the output variables

As we are using the output variables, we need to adjust the output variables in the `outputs.tf` file. Open the `outputs.tf` file and add the following code:

Expand All @@ -57,7 +69,7 @@ output "cloudfoundry_org_name" {
description = "The name of the cloudfoundry org connected to the project account."
}
```
### Step 3: Apply the changes
### Step 4: Apply the changes

1. Plan the Terraform configuration to see what will be created:

Expand All @@ -76,6 +88,13 @@ output "cloudfoundry_org_name" {
```

You will be prompted to confirm the creation of the environment. Type `yes` and press `Enter` to continue.
The output should look like this:

<img width="600px" src="assets/ex7_3.png" alt="executing terraform apply with cloud foundry provider">

You can also check that everything is in place via the SAP BTP cockpit. You should see the Cloud Foundry environment in the subaccount:

<img width="600px" src="assets/ex7_4.png" alt="SAP BTP Cockpit with Cloud Foundry environment">
## Summary

You've now successfully created a Cloud Foundry environment instance as well as a Cloud Foundry space in SAP BTP.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,6 @@

In this exercise you will learn how to use the [Terraform Provider for CloudFoundry](https://registry.terraform.io/providers/cloudfoundry/cloudfoundry/latest/docs) and create a space.

### Step 3: Adjust the provider configuration

As we are using an additional provider we must make Terraform aware of this in the `provider.tf` file. Open the `provider.tf` file and add the following code to the `required_provider` block:

```terraform
cloudfoundry = {
source = "cloudfoundry/cloudfoundry"
version = "1.1.0"
}
```

To configure the Cloud Foundry provider add the following lines at the end of the file:

```terraform
provider "cloudfoundry" {
api_url = "https://api.cf.${var.region}-001.hana.ondemand.com"
}
```
Save your changes.

> [!WARNING]
> We assume that the Cloud Foundry environment is deployed to the extension landscape 001. If this is not the case the authentication might fail. In a real-world scenario you would probably have a different boundary of content to the module.

To fulfill all requirements for the authentication against the Cloud Foundry environment you must export the following environment variables:

- Windows:
Expand All @@ -43,57 +20,39 @@ To fulfill all requirements for the authentication against the Cloud Foundry env
export CF_PASSWORD='<your SAP BTP password>'
```

> [!NOTE]
> Although we do not use the Cloud Foundry part of the module namely the assignment of users to the organization, Terraform will initialize the Cloud Foundry provider and try to authenticate against the Cloud Foundry environment. This is why we need to define the configuration and provide the credentials.

### Step 3: Apply the changes
### Step 1: Adjust the provider configuration

As we have a new provider in place, we need to re-initialize the setup to download the required provider and module. Run the following command:
As we are using an additional provider we must make Terraform aware of this in the `provider.tf` file. Open the `provider.tf` file and add the following code to the `required_provider` block:

```bash
terraform init
```terraform
cloudfoundry = {
source = "cloudfoundry/cloudfoundry"
version = "1.1.0"
}
```

The output should look like this:

<img width="600px" src="assets/ex7_1.png" alt="executing terraform init with cloud foundry provider">

> [!NOTE]
> There is also a command parameter called `--upgrade` for the `terraform init` command. This parameter will *upgrade* the provider to the latest version. As we are adding new providers, we do not need to use this parameter.

You know the drill by now:

1. Plan the Terraform configuration to see what will be created:

```bash
terraform plan
```

The output should look like this:

<img width="600px" src="assets/ex7_2.png" alt="executing terraform plan with cloud foundry">

2. Apply the Terraform configuration to create the environment:

```bash
terraform apply
```

You will be prompted to confirm the creation of the environment. Type `yes` and press `Enter` to continue.
To configure the Cloud Foundry provider add the following lines at the end of the file:

The result should look like this:
```terraform
provider "cloudfoundry" {
api_url = "https://api.cf.${var.region}-001.hana.ondemand.com"
origin = var.idp
}
```
Save your changes after editing the file.
The `origin` (Identity Provider Orgin Key ) is set to null. If a [Custom Identity Provider](https://help.sap.com/docs/btp/sap-business-technology-platform/log-on-with-custom-identity-provider-to-cloud-foundry-environment-using-cloud-foundry-command-line-interface) is used to login to SAP BTP CloudFoundry Environment, this value is set to Orgin Key of the Custom Identity Provider. The `api_url ` is the API URL of SAP BTP CloudFoundry Environment.

<img width="600px" src="assets/ex7_3.png" alt="executing terraform apply with cloud foundry provider">

You can also check that everything is in place via the SAP BTP cockpit. You should see the Cloud Foundry environment in the subaccount:
> [!WARNING]
> We assume that the Cloud Foundry environment is deployed to the extension landscape 001. If this is not the case the authentication might fail. In a real-world scenario you would probably have a different boundary of content to the module.

<img width="600px" src="assets/ex7_4.png" alt="SAP BTP Cockpit with Cloud Foundry environment">

## Creation of a Cloud Foundry space
> [!NOTE]
> Although we do not use the Cloud Foundry part of the module namely the assignment of users to the organization, Terraform will initialize the Cloud Foundry provider and try to authenticate against the Cloud Foundry environment. This is why we need to define the configuration and provide the credentials.

As a last task we also want to add a Cloud Foundry space to the Cloud Foundry environment.

### Step 1: Add the variable to the configuration for Space creation
### Step 2: Add the variable to the configuration for Space creation

First we need to add more variable in the `variables.tf` file. Open the `variables.tf` file and add the following code:

Expand Down Expand Up @@ -131,7 +90,7 @@ variable "cf_space_auditors" {

This allows us to specify the name of the Cloud Foundry space. We also define a default value (`dev`) for the variable. Save the changes.

### Step 2: Cloudfoundry Space Creation and Role Assignments
### Step 3: Cloudfoundry Space Creation and Role Assignments

To trigger the creation of a Cloud Foundry space and space roles, Open the `main.tf` file and add the following code:

Expand Down Expand Up @@ -173,7 +132,7 @@ resource "cloudfoundry_space_role" "cf_space_auditors" {
}
```

### Step 3: Add the variables to tfvar file
### Step 4: Add the variables to tfvar file

Now we can add `space developers` and `space managers` to the space we created, Add following variables to your `tfvars` file.

Expand All @@ -183,21 +142,26 @@ cf_space_developers = ["john.doe@test.com"]
```
Save the changes.

### Step 4: Apply the changes

### Step 5: Apply the changes
As we have all prerequisites already in place when it comes to provider configuration and authentication, we can proceed with applying the changes.

1. Plan the Terraform configuration to see what will be created:
1. As we have a new provider in place, we need to re-initialize the setup to download the required provider and module. Run the following command:

```bash
terraform init
```

2. Plan the Terraform configuration to see what will be created:

```bash
terraform plan
```

The output should look like this:

<img width="600px" src="assets/ex7_6.png" alt="executing terraform plan for cloud foundry space creation">
<img width="600px" src="assets/ex7_2.png" alt="executing terraform plan for cloud foundry space creation">

2. Apply the Terraform configuration to create the space:
3. Apply the Terraform configuration to create the space:

```bash
terraform apply
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading