Skip to content

Commit 5e02872

Browse files
committed
Merge branch 'aryanshah/add_argo_cd' of https://github.com/ARYANSHAH1567/layer5-academy into aryanshah/add_argo_cd
2 parents 27f5740 + da1f5b5 commit 5e02872

File tree

12 files changed

+838
-0
lines changed

12 files changed

+838
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
type: "course"
3+
title: "Getting Started with OpenTofu"
4+
description: "Learn how to use OpenTofu, an open-source infrastructure as code tool, to automate and manage your cloud infrastructure."
5+
weight: 9
6+
tags: ["IAC", "infrastructure as code", "OpenTofu"]
7+
categories: "IAC"
8+
level: "beginner"
9+
banner: "sre-k8s.svg"
10+
---
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
type: "module"
3+
title: "Introduction and Learning Objectives"
4+
description: ""
5+
weight: 1
6+
---
7+
8+
### Welcome
9+
10+
This course provides an introduction to Infrastructure as Code (IaC) and explores its advantages over traditional manual infrastructure provisioning methods. Focusing on the open source IaC tool OpenTofu, you'll gain insight into how it enables IT professionals to automate infrastructure provisioning, scaling, and management while maintaining vendor neutrality and embracing open collaboration. Through hands-on learning, you'll understand how OpenTofu works, the essential tools required, and its workflow. You’ll also learn to write a simple OpenTofu configuration file to provision and destroy infrastructure, gaining practical experience with this innovative tool.
11+
12+
Whether you're new to IaC or looking to enhance your skills with an open source alternative, this course will empower you to modernize infrastructure management practices and contribute to the growing OpenTofu ecosystem.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
type: "module"
3+
title: "Lab"
4+
description: ""
5+
weight: 3
6+
tags: ["terraform", "opentofu", "IAC"]
7+
categories: "IAC"
8+
---
9+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
type: "page"
3+
title: "Lab 1. Installing OpenTofu"
4+
description: ""
5+
weight: 1
6+
---
7+
8+
### Overview
9+
In this hands-on activity, we will learn how to install OpenTofu and verify the installation.
10+
11+
### Installing OpenTofu
12+
13+
#### Linux Environment Setup
14+
15+
While this section presents various setup options, we will install OpenTofu on an Ubuntu 22.04 host.
16+
Depending on the operating system and the installation method, there are several ways to install OpenTofu.
17+
The most convenient is to use the installation script provided by the official OpenTofu documentation.
18+
19+
1. Before we download the installation script, let's update the system and install some necessary tools.
20+
21+
**Note**: On most operating systems, these tools might already be installed.
22+
23+
```bash
24+
sudo apt-get update
25+
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
26+
```
27+
28+
2. Download the installer script using the following command:
29+
30+
```bash
31+
curl -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
32+
```
33+
34+
3. Grant the script execute permission and verify:
35+
36+
```bash
37+
chmod +x install-opentofu.sh && ls -l install-opentofu.sh
38+
```
39+
40+
4. If you're familiar with shell scripting, feel free to review the script; if not, there's no need to worry - simply proceed to step 5:
41+
42+
```bash
43+
less install-opentofu.sh
44+
```
45+
46+
5. Execute the installer script to install OpenTofu. The script requires a mandatory argument known as the
47+
`--install-method`. The value will differ depending on your operating system and package manager. We are
48+
installing it on the Ubuntu system and will use snap (snapcraft) as the installation method of choice.
49+
50+
```bash
51+
sudo ./install-opentofu.sh --install-method snap
52+
Installing OpenTofu using Snap...
53+
opentofu 1.8.4 from OpenTofu Core Team✓ installed
54+
OpenTofu v1.8.4
55+
on linux_amd64
56+
```
57+
58+
6. The command line interface to OpenTofu is the tofu command. Verify the installation by executing the help:
59+
60+
```bash
61+
tofu -h
62+
Usage: tofu [global options] <subcommand> [args]
63+
The available commands for execution are listed below.
64+
The primary workflow commands are given first, followed by
65+
less common or more advanced commands.
66+
Main commands:
67+
init Prepare your working directory for other commands
68+
validate Check whether the configuration is valid
69+
plan Show changes required by the current configuration
70+
apply Create or update infrastructure
71+
destroy Destroy previously-created infrastructure
72+
<<Output Truncated>>
73+
```
74+
75+
7. OpenTofu provides tab-completion support for all command names, as well as some command arguments. To set up auto-completion, execute the following command:
76+
77+
```bash
78+
tofu -install-autocomplete
79+
```
80+
81+
**Note**: After installation, you need to restart your shell or reload its profile script to activate completion.
82+
83+
Congratulations, your OpenTofu installation is complete and ready to use!
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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

Comments
 (0)