# OpenStack Terraform Example

This guide provides an example of how to use Terraform with OpenStack service.

## Prerequisites

- An OpenStack project
- A Service Account with the `OpenStack Editor` permission

## Steps

We will use Terraform in this example. Please refer to the [official installation instructions](<https://developer.hashicorp.com/terraform/install>) to install Terraform.

1. Create a new directory for your Terraform configuration:

```bash
git clone https://github.com/syseleven/terraform-examples.git
```

2. Navigate to the `simple-instance` directory:

```bash
cd terraform-examples/simple-instance
```

Here, execute the following command to patch the example:

```bash
perl -i -pe 's/m1.tiny/SCS-1V-2-50n/' instances-*.tf # for cross system compatibility
```

3. Configure OpenStack Access

```bash
unset HISTFILE
unset $(set | grep OS_ | sed 's/=.*//')
export KEYSTONE_ORIGIN="https://keystone.cloud.syseleven.net:5000"
export OS_AUTH_URL="$KEYSTONE_ORIGIN/v3"
export OS_REGION_NAME=dus2 # or ham1
export OS_AUTH_TYPE=v3applicationcredential
export OS_APPLICATION_CREDENTIAL_ID=s11auth:${PROJECT_ID}
export OS_APPLICATION_CREDENTIAL_SECRET="s11_orgsa_<uuid>_<secret>"
```

Of course, you can configure the OpenStack Provider via Terraform Files, too. Please refer to [this documentation](<https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs#example-usage>) for more details. You will be using `application_credential_id` and `application_credential_secret` in the `provider "openstack"` block.

4. Initialize Terraform

```bash
terraform init
```

5. Apply the Terraform configuration

```bash
terraform apply
```

When prompted, input your public SSH key. You should see the output of the Terraform configuration. Verify in the [Dashboard](<https://dashboard.syseleven.de>) that the instances have been created.

6. Destroy the Terraform configuration

```bash
terraform destroy
```

> [!TIP]
>
> **Example Configuration**
>
> Here's an example Terraform configuration file using application credentials for authentication with the OpenStack provider, which you can use as a starting point for your own configurations.
>
> ```hcl
> variable "project_id" {
>   description = "Project ID"
>   type        = string
>   default     = "c86fbc858f9949e0ab3e8f8c3d40104b"
> }
>
> variable "s11_service_account" {
>   description = "Service Account credentials for authentication"
>   type        = string
>   default     = "s11_orgsa_<uuid>_<secret>"
> }
>
> variable "region" {
>   description = "Region"
>   type        = string
>   default     = "dus2"
> }
>
> # Define required providers
> terraform {
>   required_version = ">= 0.14.0"
>   required_providers {
>     openstack = {
>       source  = "terraform-provider-openstack/openstack"
>       version = "~> 1.53.0"
>     }
>   }
> }
>
> # Configure the OpenStack Provider
> provider "openstack" {
>   auth_url    = "https://keystone.cloud.syseleven.net:5000/v3"
>   region      = var.region
>   application_credential_id     = "s11auth:${var.project_id}"
>   application_credential_secret = var.s11_service_account
> }
>
> # Create a web server (example resource)
> resource "openstack_compute_instance_v2" "test-server" {
>   # ... (resource configuration)
> }
> ```
