|
| 1 | +--- |
| 2 | +type: "page" |
| 3 | +title: "Lab 2. Learning the Basics of OpenTofu" |
| 4 | +description: "" |
| 5 | +weight: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +### Overview |
| 9 | + |
| 10 | +In this lab, you will gain hands-on experience with the basics of the OpenTofu command line and be introduced to the OpenTofu configuration language, which specifies the resources OpenTofu would create and the providers it would use. |
| 11 | + |
| 12 | +### Prerequisites |
| 13 | + |
| 14 | +You will need OpenTofu installed as per Lab 1. |
| 15 | + |
| 16 | +### Initialize and Create a Local Resource |
| 17 | + |
| 18 | +In this exercise, we will write a basic OpenTofu code to create a resource locally and understand the basic |
| 19 | +workflow to create any infrastructure in OpenTofu. |
| 20 | + |
| 21 | +The basic workflow in OpenTofu is comprised of three stages: |
| 22 | +1. Write the code |
| 23 | +2. Review changes before applying it |
| 24 | +3. Apply to provision the infrastructure |
| 25 | + |
| 26 | +To begin with, instead of creating a piece of infrastructure from code using OpenTofu, let us create a simple file |
| 27 | +locally to understand the basics. In the next exercise, we will create an actual infrastructure. |
| 28 | + |
| 29 | +1. In the OpenTofu language, we declare resources representing infrastructure objects. In the following code example, we are creating a resource, i.e., a local file named demo.txt, with the contents mentioned in the block of the code. We define what resource we want to create and how the resource should look and feel using the OpenTofu language. Using the editor of your choice, create the file main.tf with the contents below: |
| 30 | + |
| 31 | +```hcl |
| 32 | +resource "local_file" "hello_world" { |
| 33 | +filename = "${path.module}/demo.txt" |
| 34 | +content = <<-EOF |
| 35 | +Hello World!!! |
| 36 | +Welcome to the fascinating world of OpenTofu! |
| 37 | +EOF |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +2. The OpenTofu CLI provides us with a few commands to make the OpenTofu code more convenient to work with. The tofu fmt command reformats OpenTofu configuration files into a canonical format and style, saving you the time and effort of making minor adjustments for readability and consistency. It works effectively as a pre-commit hook in your version control system. |
| 42 | + |
| 43 | +```bash |
| 44 | +tofu fmt |
| 45 | +main.tf |
| 46 | +``` |
| 47 | + |
| 48 | +3. Create a directory and move the OpenTofu code to the directory before we initialize a working directory: |
| 49 | + |
| 50 | +```bash |
| 51 | +mkdir demo && mv main.tf demo/ && cd demo |
| 52 | +``` |
| 53 | + |
| 54 | +4. After writing the code, the first step is to initialize a new or existing OpenTofu working directory by creating initial files, loading any remote state, downloading modules, and more. This is the first command you should run for any new or existing OpenTofu configuration on each machine. It sets up all the necessary local data to run OpenTofu, which is typically not committed to version control. You can safely run this command multiple times. While subsequent runs may produce errors, this command will never delete your configuration or state. |
| 55 | + |
| 56 | +```bash |
| 57 | +tofu init |
| 58 | +Initializing the backend... |
| 59 | +Initializing provider plugins... |
| 60 | +- Finding latest version of hashicorp/local... |
| 61 | +- Installing hashicorp/local v2.5.2... |
| 62 | +- Installed hashicorp/local v2.5.2 (signed, key ID 0C0AF313E5FD9F80) |
| 63 | +Providers are signed by their developers. |
| 64 | +If you'd like to know more about provider signing, you can read about it here: |
| 65 | +https://opentofu.org/docs/cli/plugins/signing/ |
| 66 | +OpenTofu has created a lock file .terraform.lock.hcl to record the provider |
| 67 | +selections it made above. Include this file in your version control repository |
| 68 | +so that OpenTofu can guarantee to make the same selections by default when |
| 69 | +you run "tofu init" in the future. |
| 70 | +OpenTofu has been successfully initialized! |
| 71 | +You may now begin working with OpenTofu. Try running "tofu plan" to see |
| 72 | +any changes that are required for your infrastructure. All OpenTofu commands |
| 73 | +should now work. |
| 74 | +If you ever set or change modules or backend configuration for OpenTofu, |
| 75 | +rerun this command to reinitialize your working directory. If you forget, other |
| 76 | +commands will detect it and remind you to do so if necessary. |
| 77 | +``` |
| 78 | +
|
| 79 | +5. We can use an optional command tofu validate to validate the syntax and arguments of the configuration files present in the directory: |
| 80 | +
|
| 81 | +```bash |
| 82 | +tofu validate |
| 83 | +Success! The configuration is valid. |
| 84 | +``` |
| 85 | +
|
| 86 | +6. Generate a speculative execution plan, showing what actions OpenTofu would take to apply the current configuration. This command will not actually perform the planned actions: |
| 87 | +
|
| 88 | +```bash |
| 89 | +tofu plan |
| 90 | +OpenTofu used the selected providers to generate the following execution plan. |
| 91 | +Resource actions are indicated with |
| 92 | +the following symbols: |
| 93 | ++ create |
| 94 | +OpenTofu will perform the following actions: |
| 95 | +# local_file.hello_world will be created |
| 96 | ++ resource "local_file" "hello_world" { |
| 97 | ++ content = <<-EOT |
| 98 | +Hello World!!! |
| 99 | +Welcome to the fascinating world of OpenTofu! |
| 100 | +EOT |
| 101 | ++ content_base64sha256 = (known after apply) |
| 102 | ++ content_base64sha512 = (known after apply) |
| 103 | ++ content_md5 = (known after apply) |
| 104 | ++ content_sha1 = (known after apply) |
| 105 | ++ content_sha256 = (known after apply) |
| 106 | ++ content_sha512 = (known after apply) |
| 107 | ++ directory_permission = "0777" |
| 108 | ++ file_permission = "0777" |
| 109 | ++ filename = "./demo.txt" |
| 110 | ++ id = (known after apply) |
| 111 | +} |
| 112 | +Plan: 1 to add, 0 to change, 0 to destroy. |
| 113 | +───────────────────────────────────────────────────────────────────────────────── |
| 114 | +──────────────────────────────────── |
| 115 | +Note: You didn't use the -out option to save this plan, so OpenTofu can't |
| 116 | +guarantee to take exactly these actions if |
| 117 | +you run "tofu apply" now. |
| 118 | +``` |
| 119 | +
|
| 120 | +7. We can create or update an existing infrastructure with the tofu apply command. Let us go ahead and create the resource defined in our configuration file, i.e., a local file: |
| 121 | +
|
| 122 | +```bash |
| 123 | +tofu apply |
| 124 | +OpenTofu will perform the following actions: |
| 125 | +# local_file.hello_world will be created |
| 126 | ++ resource "local_file" "hello_world" { |
| 127 | ++ content = <<-EOT |
| 128 | +Hello World!!! |
| 129 | +Welcome to the fascinating world of OpenTofu! |
| 130 | +EOT |
| 131 | ++ content_base64sha256 = (known after apply) |
| 132 | ++ content_base64sha512 = (known after apply) |
| 133 | ++ content_md5 = (known after apply) |
| 134 | ++ content_sha1 = (known after apply) |
| 135 | ++ content_sha256 = (known after apply) |
| 136 | ++ content_sha512 = (known after apply) |
| 137 | ++ directory_permission = "0777" |
| 138 | ++ file_permission = "0777" |
| 139 | ++ filename = "./demo.txt" |
| 140 | ++ id = (known after apply) |
| 141 | +} |
| 142 | +Plan: 1 to add, 0 to change, 0 to destroy. |
| 143 | +Do you want to perform these actions? |
| 144 | +OpenTofu will perform the actions described above. |
| 145 | +Only 'yes' will be accepted to approve. |
| 146 | +Enter a value: yes |
| 147 | +local_file.hello_world: Creating... |
| 148 | +local_file.hello_world: Creation complete after 0s |
| 149 | +[id=869afe946d413938a87bb227b77d39ab9630f1e0] |
| 150 | +Apply complete! Resources: 1 added, 0 changed, 0 destroyed. |
| 151 | +``` |
| 152 | +
|
| 153 | +8. Verify the file was created in the local directory: |
| 154 | +
|
| 155 | +```bash |
| 156 | +ls demo.txt && cat demo.txt |
| 157 | +Hello World!!! |
| 158 | +Welcome to the fascinating world of OpenTofu! |
| 159 | +``` |
| 160 | +
|
| 161 | +9. To create any infrastructure in OpenTofu, we need to define the resource in the OpenTofu language, initialize a working directory, install the required plugins, generate a plan, and execute the plan by applying it. We can also clean up and destroy the resource created with a simple destroy command. Let us clean up by executing: |
| 162 | +
|
| 163 | +```bash |
| 164 | +tofu destroy |
| 165 | +
|
| 166 | +OpenTofu used the selected providers to generate the following execution plan. |
| 167 | +Resource actions are indicated with |
| 168 | +the following symbols: |
| 169 | +- destroy |
| 170 | +OpenTofu will perform the following actions: |
| 171 | +# local_file.hello_world will be destroyed |
| 172 | +- resource "local_file" "hello_world" { |
| 173 | +- content = <<-EOT |
| 174 | +Hello World!!! |
| 175 | +Welcome to the fascinating world of OpenTofu! |
| 176 | +EOT -> null |
| 177 | +. |
| 178 | +. |
| 179 | +. << Output truncated>> |
| 180 | +. |
| 181 | +. |
| 182 | +} |
| 183 | +Plan: 0 to add, 0 to change, 1 to destroy. |
| 184 | +Do you really want to destroy all resources? |
| 185 | +OpenTofu will destroy all your managed infrastructure, as shown above. |
| 186 | +There is no undo. Only 'yes' will be accepted to confirm. |
| 187 | +Enter a value: yes |
| 188 | +local_file.hello_world: Destroying... |
| 189 | +[id=869afe946d413938a87bb227b77d39ab9630f1e0] |
| 190 | +local_file.hello_world: Destruction complete after 0s |
| 191 | +Destroy complete! Resources: 1 destroyed. |
| 192 | +``` |
| 193 | +
|
| 194 | +9. Verify if the file has been cleaned up : |
| 195 | +```bash |
| 196 | +ls demo.txt |
| 197 | +No such file or directory |
| 198 | +``` |
| 199 | +In this lab exercise, we explored the basics of OpenTofu and learned how to create a resource using the Tofu CLI. |
0 commit comments