- Installing the
tofuCLI - Writing HCL
.tffiles - Examples for local and AWS resources
- A step-by-step OpenTofu workflow
Table of Contents
- Installing OpenTofu
- HCL Configuration Files
- AWS Resource Examples
- What Is a Resource?
- OpenTofu Workflow
- Viewing Resource Arguments
- Links and References
Installing OpenTofu
OpenTofu ships as a single binary. Download the appropriate archive for your OS, unpack it, and movetofu into your system PATH:
HCL Configuration Files
HCL.tf files consist of blocks (grouping of settings) and arguments (key-value pairs) that define the desired state of your infrastructure.
Example: Managing Local Files
Createlocal.tf to manage a simple text file:
resource: Block type"local_file": Providerlocal, resourcefile"pet": Logical name- Inside
{}: Arguments likefilenameandcontent
AWS Resource Examples
HCL syntax stays consistent across providers. Here are two common AWS examples:| Resource Type | HCL Snippet | Description |
|---|---|---|
| EC2 Instance | resource "aws_instance" "web" { ... } | Launches a virtual server |
| S3 Bucket | resource "aws_s3_bucket" "data" { ... } | Creates object storage |
EC2 Instance
S3 Bucket
What Is a Resource?
A resource is any object OpenTofu manages—either locally or in the cloud. Examples include:| Scope | Examples |
|---|---|
| Local | files, directories |
| AWS | EC2 instances, S3 buckets, IAM users, Lambda |
| GCP | Compute Engine instances, App Engine applications |
| Azure | SQL Databases, Azure Active Directory resources |
OpenTofu Workflow
Provisioning with OpenTofu follows four main steps:- Write your HCL configuration files.
- Initialize with
tofu init. - Review changes using
tofu plan. - Apply changes via
tofu apply.
1. Initialize the Directory
The generated lock file (
.terraform.lock.hcl) ensures repeatable installs of provider versions.2. Preview the Execution Plan
3. Apply the Changes
Using
-auto-approve skips the confirmation prompt. Always review your plan before auto-approving:4. Verify and Inspect
Viewing Resource Arguments
Each resource has required and optional arguments. Consult the provider’s official docs to discover available fields. For example, the local provider’s local_file resource listsfilename as required, with optional settings like directory_permission.

That’s it for this lesson! Next, we’ll dive into updating and destroying resources with OpenTofu.