Overview of Terraform
Terraform will be an open source tool for automating cloud infrastructure and other IT resources Terraform allows you to create, modify, and version resources in a programmable way.
Terraform can manage resources such as virtual machines, databases, networks, load balancers, etc. for various cloud providers (AWS, Azure, Google Cloud, etc.) and on-premise environments, and can define the entire infrastructure in a single configuration file. The entire infrastructure can be defined in a single configuration file.
The main features of Terraform are as follows.
- Infrastructure state management: Terraform uses a state file to track the current state of the infrastructure. This state file is automatically updated by Terraform and the state is synchronized at the next run.
- Create, update, and delete resources: Terraform can programmatically create, update, and delete infrastructure resources.
- Plan confirmation: Terraform can generate a plan that displays a list of resources to be created, updated, or deleted. By reviewing this plan, the impact of changes can be checked in advance.
- Use of modules: Terraform can use modules to divide the configuration into reusable components.
Terraform is offered as a command line tool and also has a service called Terraform Cloud. It is also designed to facilitate collaboration with team members by providing shared status and functionality.
Environmental setup and main workflow
The following environment settings are required to use Terraform.
- Installing Terraform Download and install Terraform. Download the version appropriate for your OS from the official website.
- Configure providers Terraform supports cloud providers such as AWS and Google Cloud Platform. For the provider you use, you need to add information such as access keys to the configuration file.
- Creating a working directory for Terraform Create a working directory to store Terraform configuration files; Terraform treats this working directory as the current directory.
- Initialize Terraform Initialize Terraform and download plug-ins and modules for the providers to be used.
- Create Terraform configuration file Create Terraform configuration file. In this configuration file, define the resources you want to execute, such as starting an AWS EC2 instance or creating a Google Cloud Storage bucket.
- Executing Terraform Execute Terraform to create, update, and delete the resources described in the configuration file.
By following the above steps, you are ready to use Terraform. For more information, see Setting up Terraform, an infrastructure management tool. For more detailed instructions, please refer to the official documentation.
Hello World
First, we describe a simple implementation of Hello World in Terraform. The following example uses Terraform to create an AWS EC2 instance.
- Install Terraform.
- Install AWS CLI and obtain an access key and secret access key.
- Create a working directory and create a file named main.tf in it, and add the following code to this file.
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
This configuration describes the settings for creating a t2.micro sized EC2 instance in the us-west-2 region of AWS.
Execute the terraform init command in the working directory to initialize Terraform. Next, execute the terraform apply command in the working directory to create the resources described in the configuration file.
Finally, check the EC2 instance on the AWS management console to confirm that the resource has been successfully created.
Notes on using Terraform
Terraform is a tool for managing cloud infrastructure in code, and will be a tool that supports many public cloud providers (AWS, Google Cloud, Azure, etc.) Using Terraform has some fee considerations.
- Timing of resource creation and deletion
When using Terraform, the timing of resource creation or deletion is important. When creating resources, providers may be charged, so unnecessary resource creation should be avoided. Also, when deleting resources, care should be taken to ensure that no unnecessary resources remain.
- Scaling Infrastructure
When using Terraform, there are also considerations related to scaling infrastructure. For example, AWS EC2 instances are priced differently depending on the instance type. Therefore, when increasing the number of instances, it is necessary to select the appropriate instance type for the resources required.
- Using Reserved Instances and Spot Instances
In the case of AWS, the cost of instances can be reduced by using reserved or spot instances; when using Terraform, these options can be configured.
- Provider fee structure
When using Terraform, it is necessary to understand the provider’s fee structure; in the case of AWS, the fees charged vary depending on the type of resource. Therefore, it is necessary to understand the provider’s fee structure and select the appropriate resources.
- Module Reuse
Terraform allows code reuse through the use of modules, simplifying the code used to build the infrastructure. When using modules, the exact resources needed must be specified.
Terraform Reference Books
A reference book for Terraform is “Practical Terraform: System Design and Best Practices in AWS”.
The book begins with a brief setup and then describes specific applications. The table of contents is given below.
Chapter 1 Setup
Chapter 2 Basic Operation
Chapter 3 Basic Syntax
Chapter 4 Overall Design
Chapter 5 Authorization Management
Chapter 6 Storage
Chapter 7 Networking
Chapter 8 Load Balancers and DNS
Chapter 9 Container Orchestration
Chapter 10 Batch
Chapter 11 Key Management
Chapter 12 Configuration Management
Chapter 13 Datastores
Chapter 14 Deployment Pipeline
Chapter 15 SSH-less Operation
Chapter 16 Logging
Chapter 17 Terraform Best Practices
Chapter 18 AWS Best Practices
Chapter 19 Advanced Syntax
Chapter 20 Managing tfstate Files
Chapter 21 Structuring
Chapter 22 Modular Design
Chapter 23 Resource Reference Patterns
Chapter 24 Refactoring
Chapter 25 Importing Existing Resources
Chapter 26 Team Development
Chapter 27 Continuous Apply
Chapter 28 Falling on the Ground
Appendix A Riding on the Shoulders of Giants
コメント