You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: _source/_posts/2023-07-28-terraform-workshop.md
+40-40
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ The steps in this workshop are also demonstrated in the accompanying video, so t
27
27
28
28
{% include toc.md %}
29
29
30
-
## Terraform automates manual work
30
+
## 1: Terraform automates manual work
31
31
32
32
Terraform is a general-purpose Infrastructure As Code tool. Okta's Terraform module lets you write code to accomplish tasks that would otherwise require manual work in the Okta Admin Console. Moving administration tasks to Terraform offers several benefits over doing them by hand:
33
33
@@ -39,27 +39,27 @@ Terraform is a general-purpose Infrastructure As Code tool. Okta's Terraform mod
39
39
40
40
If you're new to Terraform, start with Hashicorp's [Terraform tutorials](https://developer.hashicorp.com/terraform/tutorials). This workshop assumes that you already [have Terraform installed](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) on your computer, and that you have a free [Okta Developer Account](https://developer.okta.com/login/).
41
41
42
-
## Key Terraform concepts
42
+
## 2: Key Terraform concepts
43
43
44
44
Terraform code describes a desired state for cloud infrastructure. After you write your Terraform code, the `terraform plan` command predicts what changes will be required to make the infrastructure match the code, and the `terraform apply` command changes the infrastructure.
45
45
46
46
A Terraform project is a directory containing `.tf` files. These Terraform files contain providers, resources, and sometimes variables and data sources too. After running `terraform init` in a Terraform project, Terraform downloads all required providers to the `.terraform` directory in that project.
47
47
48
48
Terraform also stores data about the state of the infrastructure by creating the file `terraform.tfstate` in the project. Every piece of infrastructure should be represented in only one `terraform.tfstate` file. If several collaborators work on the same Terraform codebase together, it's best to use Terraform Cloud or a similar service to share a single `terraform.tfstate` file.
49
49
50
-
## Set up the Okta Terraform provider
50
+
## 3: Set up the Okta Terraform provider
51
51
52
52
Before you can use Terraform to manage resources in your Okta organization, you must provide it with appropriate credentials. The following steps will connect a Terraform project on your local system to your Okta Developer account.
53
53
54
-
## Manage secrets appropriately
54
+
### 3.1: Manage secrets appropriately
55
55
56
56
Setting up any Terraform provider involves working with some secrets that can grant access to your infrastructure. The Okta terraform provider uses a client ID and private key. Anyone who has these secrets can act on behalf of Terraform in your Okta organization. Secrets are replaced by placeholder values in the code examples that follow.
57
57
58
58
When deciding how to handle any secret information, consider the potential impact if it was misused. The impact of leaking a credential to a sandbox account where you test your code may be relatively low, whereas mishandling production secrets could cause extensive downtime and permanent reputation loss for your company.
59
59
60
60
When you work with Terraform, you must manage secrets as carefully as you protect every other means of accessing your infrastructure. If you store your Terraform code in source control, avoid committing secrets. If you're running Terraform in automation, find an appropriate secrets management solution to handle the sensitive credentials.
61
61
62
-
### Create Terraform files
62
+
### 3.2: Create Terraform files
63
63
64
64
Create a new directory to start your Terraform project in. Pick a name that makes sense: `okta-terraform-workshop` is a safe bet. In the terminal, you can do this with `mkdir okta-terraform-workshop; cd okta-terraform-workshop`.
65
65
@@ -101,7 +101,7 @@ All of those fields are mandatory, and you will fill out the missing values in t
101
101
102
102
Scopes describe categories of changes that Terraform is allowed to make with the Okta Provider. This list of scopes will appear twice: Once here in the provider definition, and again in the Okta App Integration. The 3 scopes provided will be enough to do the exercises in this workshop. Later on, you will explore what would happen if the scopes had been set up wrong at this step.
103
103
104
-
### Create the Okta app integration
104
+
### 3.3: Create the Okta app integration
105
105
106
106
In a web browser, log in to your Okta Developer Account at [developer.okta.com](developer.okta.com). If you don't have an account yet, now is a great time to create one. Developer accounts are a safe place to experiment without the risk of accidentally changing your production infrastructure. Developer accounts don't have a time limit, so you can always test your code before taking it to production.
107
107
@@ -123,7 +123,7 @@ In the terminal, `cd okta-terraform-workshop` and convert the PKCS-1 key to an R
123
123
$ openssl rsa -in pkcs.pem -out rsa.pem
124
124
```
125
125
126
-
### Configure the Okta Terraform provider
126
+
### 3.4: Configure the Okta Terraform provider
127
127
128
128
After setting up the app integration, you have found all the values required by the provider configuration! In `main.tf`, check that your provider block contains the `org_name`, `client_id`, and `private_key` file location. Tell it where to find the private key that you converted. With all the values filled out, the provider configuration looks like this:
129
129
@@ -137,13 +137,13 @@ provider "okta" {
137
137
}
138
138
```
139
139
140
-
## Manage users and groups
140
+
## 4: Manage users and groups
141
141
142
142
With the provider configured, Terraform is ready to make changes to your Okta org. In the Okta Admin Console in your browser, take one last look at the empty Users list (found under the Directory heading in the left sidebar), because you're about to create some users with Terraform!
143
143
144
144
In keeping with the Terraform theme, this workshop's examples will create users for some plants and animals, and assign them to a garden group. However, the exercises will work just as well if you choose a different theme, so feel free to customize your users and groups to keep things interesting.
145
145
146
-
### Creating your first user
146
+
### 4.1: Creating your first user
147
147
148
148
When creating a new Okta object with Terraform, you will always follow the same basic steps:
149
149
@@ -154,7 +154,7 @@ When creating a new Okta object with Terraform, you will always follow the same
154
154
155
155
In this workshop, the first thing you'll create is a user.
156
156
157
-
### Write Terraform for a user
157
+
### 4.2: Write Terraform for a user
158
158
159
159
Resources in Terraform code describe infrastructure, in this case Okta objects, that Terraform will manage. The first resource you create will describe a user, and applying the Terraform will create that user in the Okta Developer Account whose app integration credentials were passed to the provider configuration.
160
160
@@ -173,7 +173,7 @@ resource "okta_user" "bird" {
173
173
174
174
Add that resource after the Terraform provider in `main.tf`. Notice how the type of the resource is `okta_user`, and the name of this one particular resource is `"bird"`? The name `"bird"` is only used within your Terraform code to refer to this particular user. You'll use it later when adding the user to a group.
175
175
176
-
## Plan and apply the Terraform
176
+
### 4.3: Plan and apply the Terraform
177
177
178
178
Save `main.tf`, then run `terraform plan` in your `terraform-workshop` directory to see what will change once you run `terraform apply`:
179
179
@@ -237,7 +237,7 @@ Enter 'yes' to confirm that the plan is still what you want, and Terraform creat
237
237
238
238
In the admin console of your Okta Developer Account, go to Directory in the left column and select People. Do you see the newly created user in the list? Note that the resource name "bird" is only used within Terraform, and doesn't show up to the user or in the admin interface.
239
239
240
-
### Create another user
240
+
### 4.4: Create another user
241
241
242
242
Now you can make a second user with Terraform! Try creating an Okta user resource that Terraform will internally call `"butterfly"`, with the name `Papilio zelicaon`, and the login and email `swallowtail.butterfly@example.com`.
Just like when creating a user, the [Okta Terraform Provider docs](https://registry.terraform.io/providers/okta/okta/latest/docs/resources/group) offer guidance on which fields are available, and which are required, when creating a group.
260
260
@@ -291,7 +291,7 @@ You added 1 new group resource, and Terraform plans to add 1 new group. The plan
291
291
292
292
When you inspect the new group, you may notice that it has no users assigned to it. Let's fix that!
293
293
294
-
### Add users to the group
294
+
### 4.6: Add users to the group
295
295
296
296
Group memberships are managed with the `okta_group_memberships` resource. Where would you look for information on what fields an `okta_group_memberships` resource requires? The [provider docs](https://registry.terraform.io/providers/okta/okta/latest/docs/resources/group_memberships) are always a good place to start.
Plan and apply the Terraform, then look at the group in your admin console. Are the garden creatures assigned to the garden group now?
311
311
312
-
## Explore Terraform errors
312
+
## 5: Explore Terraform errors
313
313
314
314
Now that you're getting more comfortable with basic Terraform operations, let's pause and look at some ways that things might have gone wrong in the previous steps.
315
315
316
-
### Multiple keys in the application
316
+
### 5.1: Multiple keys in the application
317
317
318
318
To cause this error, navigate to your Terraform application in the Okta Admin Console, and add a second keypair in the Public Keys section of the General configuration tab.
319
319
@@ -323,7 +323,7 @@ Try setting your first keypair, the one you created the RSA key from, to "inacti
323
323
324
324
To fix this error, set the keypair that Terraform is using to active, and the unused keypair to inactive. Suddenly, the plan succeeds again! Once the unused keypair is set to inactive, you can delete it.
325
325
326
-
### Missing scopes
326
+
### 5.2: Missing scopes
327
327
328
328
To see the impact of a missing scope error, try creating a user when the Terraform provider's scopes are set up incorrectly. Let's add a flower to `main.tf`:
329
329
@@ -354,7 +354,7 @@ When the `okta.users.manage` scope is granted in both places, you can successful
354
354
355
355
How would you add the flower to the garden group?
356
356
357
-
### Resolving clock confusion
357
+
### 5.3: Resolving clock confusion
358
358
359
359
For secure communication between Terraform and Okta, the system where you're running Terraform has to agree with Okta about what time it is.
360
360
@@ -365,7 +365,7 @@ You'll see the error `the API returned an error: The client_assertion token has
365
365
Fix this error by configuring the system where you're running Terraform to automatically update its clock from a trusted time server. Forcing your system clock to synchronize to its timeserver can also resolve this issue.
366
366
367
367
368
-
## Remove resources
368
+
## 6: Remove resources
369
369
370
370
Terraform can remove infrastructure as well as create it.
371
371
@@ -391,17 +391,17 @@ To successfully remove a resource, all references to it must also be removed. In
391
391
392
392
In the Okta console, you'll see that the bird's user account is not removed until you run `terraform apply`.
393
393
394
-
## Build intermediate Terraform skills
394
+
## 7: Build intermediate Terraform skills
395
395
396
396
As you use Terraform to describe the complexity of the real world, you'll need more of its features. In the next steps of this workshop, you'll practice intermediate Terraform skills as they apply to the Okta provider.
397
397
398
-
### Use Terraform variables
398
+
### 7.1: Use Terraform variables
399
399
400
400
The simple examples so far have used a lot of hardcoded strings. One in particular is the `example.com` domain that all of the users have their logins and IDs at. What if you expected that to change, and wanted to move to a new email domain?
401
401
402
402
Terraform's [variables](https://developer.hashicorp.com/terraform/language/values/variables) can help.
403
403
404
-
### Variable email domain
404
+
### 7.2: Variable email domain
405
405
406
406
To declare a variable, in this case a string representing the email domain, tell Terraform the variable type and optionally a default value. This variable definition can go in `main.tf` while the project is still small:
Can you create and use a list variable to replace the hardcoded list in your `okta_group_memberships` resource?
436
436
437
-
### Key file management techniques
437
+
### 7.3: Key file management techniques
438
438
439
439
You've already used a built-in Terraform variable to describe the current file path, when telling the Okta provider where to look for the the private key:
440
440
@@ -444,15 +444,15 @@ You've already used a built-in Terraform variable to describe the current file p
444
444
445
445
How else could you have provided that key to your code?
446
446
447
-
### Import file to Terraform
447
+
### 7.4: Import file to Terraform
448
448
449
449
You could specify the private key with Terraform's builtin `file` function:
450
450
451
451
```
452
452
private_key = file("rsa.pem")
453
453
```
454
454
455
-
### Read environment variables from Terraform
455
+
### 7.5: Read environment variables from Terraform
456
456
457
457
If your secrets management solution puts the key file into an environment variable, you can relay it to your Terraform. Here's how that would look if your Terraform can access the key through an environment variable `SECRET_PKEY`:
This pattern of reading secrets and variables from the shell where Terraform runs is especially common in continuous integration, continuous deployment, and other automation.
470
470
471
-
### Use multi-line strings
471
+
### 7.6: Use multi-line strings
472
472
473
473
So far you've used lists, strings, and string interpolation. What would you do if you needed to set a variable to a value with several lines?
474
474
@@ -488,33 +488,33 @@ POEM
488
488
489
489
Notice how `<<` and a custom delimiter, in this case `POEM`, start the multi-line string. The same delimiter, in this case, `POEM` on its own line ends the string.
490
490
491
-
### Refactor Terraform files
491
+
### 7.7: Refactor Terraform files
492
492
493
493
As you configure more resources with Terraform, it gets harder to keep track of everything in one big file. Fortunately, Terraform recognizes all `.tf` files in a directory as belonging to the same project.
494
494
495
495
To see this in action, try moving both of the remaining user resources, the butterfly and the flower, from `main.tf` into a new file called `users.tf`. After this change, `terraform plan` will report "No changes. Your infrastructure matches the configuration."
496
496
497
497
As your Terraform project grows more complex, you'll use more features of the language. You might use [modules](https://developer.hashicorp.com/terraform/language/modules) to reuse similar configurations. You'll probably use more variables to take inputs to Terraform from the environment where it's running.
498
498
499
-
### Format Terraform code
499
+
### 7.8: Format Terraform code
500
500
501
501
If you're using an editor or IDE that doesn't automatically fix your indentation and other formatting details, run the command `terraform fmt` from the command line in your Terraform project directory to clean up the formatting of all `.tf` files.
502
502
503
503
Try adding some ugly whitespace to one of your Terraform files, and then use `terraform fmt` to clean it up!
504
504
505
-
## Import resources
505
+
## 8: Import resources
506
506
507
507
What if a user existed in Okta before you started working with Terraform, and you wanted to create a Terraform resource to represent them? [Terraform import](https://developer.hashicorp.com/terraform/language/import), an experimental feature, does this.
508
508
509
-
### Manually create users
509
+
### 8.1: Manually create users
510
510
511
511
To try it out, you'll need something to import. Create a user by hand through the Okta admin console.
512
512
513
513
Navigate to Directory, People, Add Person. The garden could use a tree in it, so this example will make the tree `Acer macrophyllum`, `bigleaf.maple@example.com`.
514
514
515
515
View that user in the Okta admin console, and its ID appears at the end of the URL: `https://dev-1234567890-admin.okta.com/admin/user/profile/view/abc123`. Use that ID when importing the resource to the Terraform state.
516
516
517
-
### Import data with Terraform
517
+
### 8.2: Import data with Terraform
518
518
519
519
Create a new Terraform file, `imports.tf`, and add an import block to that file:
520
520
@@ -579,7 +579,7 @@ resource "okta_user" "tree" {
579
579
580
580
After the plan creates `generated.tf`, run `terraform apply` to complete the import.
581
581
582
-
### Clean up after import
582
+
### 8.3: Clean up after import
583
583
584
584
Copy the fields you'd like to manage from `generated.tf` into `users.tf`, and modify them to use any relevant variables. Here's how the tree might look in `main.tf`:
585
585
@@ -596,11 +596,11 @@ Once the user is successfully imported, remove `imports.tf` and `generated.tf`.
596
596
597
597
After this refactor, a `terraform plan` will only show changes that update the manually created user's settings to the terraform resource defaults.
598
598
599
-
## Find and fix configuration drift
599
+
## 9: Find and fix configuration drift
600
600
601
601
Config drift can happen when Okta resources are manually changed through the admin console and no longer match their Terraform configurations. You can introduce configuration drift by manually changing a resource.
602
602
603
-
### Create configuration drift
603
+
### 9.1: Create configuration drift
604
604
605
605
Foxgloves are poisonous, so you might not want them in the garden. In the Okta admin console, remove the foxglove from the garden group and suspend their account.
606
606
@@ -629,33 +629,33 @@ Terraform will perform the following actions:
629
629
Plan: 0 to add, 2 to change, 0 to destroy.
630
630
```
631
631
632
-
### Compare configuration drift solutions
632
+
### 9.2: Compare configuration drift solutions
633
633
634
634
The best way for your organization to handle this drift will depend on your business needs. When Terraform and Okta disagree about a resource, it's up to you to decide which of them is more correct, and change the less correct one so that they match.
635
635
636
-
### Make Terraform match Okta
636
+
#### 9.2.1: Make Terraform match Okta
637
637
638
638
If manual changes need to be reflected by Terraform changes, use `terraform import`, as shown above, to change your Terraform.
639
639
640
-
### Fix the config drift
640
+
#### 9.2.2: Fix the config drift
641
641
642
642
For the purposes of this workshop, assume that Terraform's configuration represents the desired state of the infrastructure. How should you make Okta match Terraform?
643
643
644
644
After you run `terraform apply`, the admin console will show that the foxglove user is back to being active and in the garden group, just how Terraform describes it.
645
645
646
-
## Control API usage
646
+
## 10: Control API usage
647
647
648
648
Every Okta organization has an API rate limit. You can find the current rate limits in [the Okta docs](https://developer.okta.com/docs/reference/rl-additional-limits/#concurrent-rate-limits).
649
649
650
650
If you're using Terraform in an organization with other API integrations, you might want to fine-tune how much of the organization's total rate limit you want Terraform to consume. To do this, go to your Terraform application in the admin console, and move the slider on the Application Rate Limits tab there.
651
651
652
-
## Clean up
652
+
## 11: Clean up
653
653
654
654
If you would like to remove all resources managed by your Terraform configuration from your Okta Developer account, run the command `terraform destroy`. This will remove everything that you created during this workshop, so your account is a clean slate next time you want to test your code in it.
655
655
656
656
Never run `terraform destroy` on infrastructure that you want to keep!
657
657
658
-
## Next steps
658
+
## 12: Next steps
659
659
660
660
In this workshop, you've taken your first steps managing Okta with Terraform. You've learned to create, change, import, and destroy resources, as well as refactoring and formatting your Terraform code.
0 commit comments