| my Profile | | — |
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
curl -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
chmod +x install-opentofu.sh
sudo ./install-opentofu.sh --install-method snap
tofu -h
tofu install-autocomplete
main.tf:
resource "local_file" "hello_world" {
filename = "${path.module}/demo.txt"
content = <<-EOF
Hello World!!!
Welcome to the fascinating world of OpenTofu!
EOF
}
tofu fmt main.tf
mkdir demo && mv main.tf demo/ && cd demo
tofu init
tofu validate
tofu plan
tofu apply
cat demo.txt
tofu destroy
main.tf:
provider "aws" {
region = "us-east-1"
access_key = "your_access_key"
secret_key = "your_secret_key"
}
resource "aws_instance" "firstvm" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
subnet_id = "your_subnet_id"
}
tofu init
tofu plan
tofu apply
tofu destroy
tofu inittofu validatetofu plantofu applytofu destroyOpenTofu is an open-source infrastructure as code (IaC) tool used for provisioning and managing infrastructure resources in a declarative manner. It enables developers and DevOps engineers to define infrastructure configurations in human-readable files, which can then be applied consistently across different environments.
Declarative Configuration:
Write infrastructure configurations in a simple and structured format.
Provider Support:
Supports multiple cloud providers (AWS, Azure, GCP) and on-premise solutions.
State Management:
Tracks the state of your infrastructure to identify changes and apply only necessary updates.
Automation:
Automates the creation, updating, and destruction of infrastructure.
Version Control Friendly:
Configuration files can be stored in version control systems like Git for collaboration and history tracking.
Providers:
Plugins that interact with cloud services or APIs (e.g., AWS, Azure).
Example:
provider "aws" {
region = "us-east-1"
}
Resources:
Describes infrastructure components like servers, databases, or storage.
Example:
resource "aws_instance" "my_instance" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
}
State:
A file that records the current state of your managed resources, ensuring consistency.
Modules:
Reusable blocks of configuration, similar to functions in programming.
Write Configuration:
Define resources and their attributes in .tf files.
Initialize Directory:
Use tofu init to download necessary plugins and set up the working directory.
Plan Changes:
Use tofu plan to preview the changes that will be made.
Apply Configuration:
Use tofu apply to implement the changes in your infrastructure.
Destroy Resources:
Use tofu destroy to clean up resources when no longer needed.
Lab 1: Local File Resource
local_file resource in main.tf.Lab 2: Virtual Machine Creation
main.tf.tofu plan before applying.tofu init: Initializes the directory and downloads provider plugins.tofu validate: Validates the configuration for syntax errors.tofu plan: Previews the changes to be applied.tofu apply: Implements the changes.tofu destroy: Deletes the resources defined in the configuration.By combining theoretical knowledge with hands-on practice and the Exam, OpenTofu empowers you to efficiently manage and automate infrastructure, making it an essential tool for modern cloud and DevOps workflows.
</br> </br>