Terraform is a tool for building, changing, and versioning safely and efficiently

Terraform is a tool for building, changing, and versioning safely and efficiently

2022-10-28 0 1,434
Resource Number 47048 Last Updated 2025-02-21
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

The Terraform recommended in this issue is a tool for building, changing, and versioning your infrastructure securely and efficiently.

Terraform is a tool for building, changing, and versioning safely and efficiently插图

HashiCorp Terraform is an infrastructure-as-a-code tool that lets you define cloud and on-property resources in a readable configuration file that you can version, reuse, and share. You can then configure and manage all your infrastructure throughout the lifecycle using a consistent workflow. Terraform can manage low-level components such as compute, storage, and network resources, as well as advanced components such as DNS entries and SaaS functionality.

Main features of Terraform

  • Infrastructure as code : Describes infrastructure using advanced configuration syntax. This allows you to version and process your data center blueprints just like you would any other code. In addition, infrastructure can be shared and reused.
  • Execution Plan : Terraform has a “plan” step that generates an execution plan . The execution plan shows what Terraform will do when you call apply. This allows you to avoid any surprises when operating Terraform’s infrastructure.
  • Resource Graph : Terraform builds a graph of all resources and creates and modities any non-dependent resources in parallel. As a result, Terraform builds the infrastructure as efficiently as possible, and operators can gain insight into the dependencies in their infrastructure.
  • Change automation : You can apply complex change sets to your infrastructure with minimal human interaction. With the execution plan and resource diagram mentioned earlier, you know exactly what Terraform will change and in what order, thus avoiding many possible human errors.

How does Terraform work?

Terraform creates and manages resources on cloud platforms and other services through its application programming interface (API). The provider enables Terraform to work with almost any platform or service through an accessible API.

Terraform is a tool for building, changing, and versioning safely and efficiently插图1

Core Terraform workflow consists of three stages:

  • Written by: You define resources that may span multiple cloud providers and services. For example, you can create a configuration to deploy applications on Virtual machines in a Virtual Private Cloud (VPC) network with security groups and load balancers.
  • Plan: Terraform creates an execution plan that describes the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.
  • Application: Upon approval, Terraform performs the recommended actions in the correct order and respects any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before extending the virtual machines.

Terraform is a tool for building, changing, and versioning safely and efficiently插图2

Start using

Install Terraform

To install Terraform, find the right package for your system and download it as a zip archive.

After downloading Terraform, unzip the package. Terraform as a package called Terraform. any other file in the package can be safely deleted and terraform can still run.

Finally, make sure the terraform binary is in your PATH. This process will vary depending on your operating system.

Print the PATH, a list of locations separated by colons.

echo $PATH

Moves the Terraform binary to one of the listed locations. This command assumes that the binaries are currently in your download folder and that your PATH contains /usr/local/bin, but you can customize it if your location is different.

mv ~/Downloads/terraform /usr/local/bin/

Verifying installation

Verify that the installation is valid by opening a new terminal session and listing the available subcommands for Terraform.

$ terraform -help
Usage:  terraform [-version] [-help] < command>  [args]

The available commands for execution are listed  below.
The most common, useful commands are shown first,  followed by
less common or more advanced commands. If you're  just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
# #... 

Add any subcommand to terraform -help to learn more about its functionality and available options.

terraform -help plan

Enable TAB complete

If you use Bash or Zsh, you can enable TAB completion for the Terraform command. To enable autocomplete, first make sure that the shell you choose exists a configuration file.

touch ~/.bashrc

Then install the autocomplete package.

terraform  -install-autocomplete

After you install autocomplete support, you will need to restart the shell.

Quick Start tutorial

After installing Terraform and Docker on your local computer, start Docker Desktop.

open -a Docker

Create a file named
learn-terraform-docker-container

mkdir learn-terraform-docker-container

Open

cd  learn-terraform-docker-container

Paste the following Terraform configuration into a file and name it main.tf.

terraform {
required_providers {
docker = {
source  = "kreuzwerker/docker"
version = "~>  2.13.0 "< / span >
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}

Initializes the project, which downloads a plug-in that allows Terraform to interact with Docker.

terraform init

Use apply. When Terraform asks you to confirm the type yes and press ENTER.

terraform apply

Verify the existence of the NGINX container by accessing localhost:8000docker ps in a Web browser or running to view the container.

Building infrastructure

< Prerequisites

To follow this tutorial, you will need:

  • Terraform CLI (1.2.0+) has been installed.
  • The AWS CLI has been installed.
  • allows you to create AWS accounts and associated credentials for resources.

To authenticate the Terraform AWS provider with your IAM credentials, set the AWS_ACCESS_KEY_ID environment variable.

export AWS_ACCESS_KEY_ID=

Now, set your key.

export AWS_SECRET_ACCESS_KEY=

Write configuration

The set of files used in Terraform to describe the infrastructure is called Terraform Configuration . You will write your first configuration to define a single AWS EC2 instance.

Each Terraform configuration must be in its own working directory. Create a directory for your configuration.

mkdir  learn-terraform-aws-instance

Switch to directory.

cd  learn-terraform-aws-instance

Create a file to define your infrastructure.

touch main.tf

Open main.tf in a text editor, paste the following configuration, and save the file.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-2"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

Query data using output

Initial configuration

After following the previous tutorials in this collection, you will have one
learn-terraform-aws-instance uses the following configuration to name the directory.

# main.tf

terraform {
required_providers {
aws = {
source  = "hashicorp/aws"
version = "~>  4.16 "< / span >
}
}

required_version = "> = 1.2.0"
}

provider "aws" {
region  = "us-west-2"
}

resource "aws_instance" "app_server" {
ami           = "ami-08d70e59c07c61a3a"
instance_type = "t2.micro"

tags = {
Name = var.instance_name
}
}

# variables.tf

variable "instance_name" {
description = "Value of the Name tag for the EC2  instance"
type        = string
default     = "ExampleAppServerInstance"
}

Ensure that your configuration matches this and that you are already in
Initialize the configuration in the learn-terraform-aws-instance directory.

terraform init

Apply the configuration before continuing with this tutorial. Use the response confirmation prompt yes.

terraform apply

Storage remote status

< Set Terraform cloud

Modify main.tf to add cloud blocks to your Terraform configuration and replace < ORG_NAME> For your organization name

terraform {
cloud {
organization = "< ORG_NAME>" 
workspaces {
name = "Example-Workspace"
}
}

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
}

< Log in to Terraform cloud

Next, log into your Terraform Cloud account using the Terraform CLI in the terminal.

$ terraform login
Terraform will request an API token for app.terraform.io using your browser.

If login is successful, Terraform will store the token in plain text in
the following file for use by subsequent commands:
/Users/< USER> /.terraform.d/credentials.tfrc.json

Do you want to proceed?
Only 'yes' will be accepted to confirm.

Enter a value:

Confirm yes with a and follow the workflow in the browser window that will open automatically. When prompted, you need to paste the generated API key into the terminal.

< Set workspace variables

The terraform init step, Example-Workspace, creates a workspace in your Terraform Cloud organization. You must configure your workspace with your AWS credentials to authenticate to the AWS provider.

Navigate to
Example-WorkspaceTerraform Workspace in the Cloud, then select the Variables TAB. Add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in the Environment Variables section, making sure to mark them as sensitive.

Terraform is a tool for building, changing, and versioning safely and efficiently插图3

—END—

Open source protocol: MPL-2.0 license

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code Terraform is a tool for building, changing, and versioning safely and efficiently https://ictcoder.com/terraform-is-a-tool-for-building-changing-and-versioning-safely-and-efficiently/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services