Table of contents
pre-requisites
Install terraform
Install aws-cli
Amazon web services account
Terraform
Terraform is an infrastructure as code (IAC) tool used for provisioning and managing cloud resources and infrastructure. Here's a short summary of Terraform:
Purpose: Infrastructure as Code (IAC) tool for automating the provisioning and management of cloud resources and infrastructure.
Key Features:
Declarative Syntax: Describe infrastructure in a human-readable configuration language.
Provider Support: Supports various cloud providers (e.g., AWS, Azure, GCP) and other services.
State Management: Maintains a state file to track resource status and changes.
Plan and Apply: Generates execution plans and applies changes in a controlled manner.
Resource Dependencies: Automatically manages resource dependencies and creates them in the right order.
Modules: Encapsulates and reuses infrastructure configurations using modules.
Version Control: Integrates well with version control systems like Git.
Ecosystem: Large community-contributed modules and providers available.
Example Configuration:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
output "instance_ip" {
value = aws_instance.example.public_ip
}
Workflow:
Write Terraform configurations (
.tf
files).Initialize the working directory with
terraform init
.Generate an execution plan with
terraform plan
.Apply the plan with
terraform apply
to create or update resources.View resource status and outputs with
terraform show
andterraform output
.Destroy resources with
terraform destroy
when no longer needed.
Terraform simplifies infrastructure management, ensures consistency, and enhances collaboration among teams.
Terraform module
A Terraform module is a reusable and encapsulated set of Terraform configurations that represent a specific piece of infrastructure or functionality. It allows you to abstract and modularize your infrastructure code for better organization and reuse. Here's a short description of a Terraform module:
Terraform Module:
Purpose: A modular and reusable collection of Terraform configurations that defines a specific component or functionality of your infrastructure.
Benefits: Encapsulates infrastructure code, promotes code reuse, simplifies maintenance, and enhances collaboration.
Structure: Typically includes one or more
.tf
files with resource definitions, variables, outputs, and optionally, other files or sub-modules.Input Variables: Modules can accept input variables, allowing customization when using the module in different contexts.
Outputs: Modules can define outputs to provide information or data to the calling configuration.
Example Use: Modules can be used in a Terraform configuration by specifying the module source, input variables, and using the outputs as needed.
main.tf
A main.tf
file in Terraform is the primary configuration file where you define the infrastructure resources and their settings using a declarative syntax. It typically contains the core resource definitions and provider configurations needed to create and manage your infrastructure. This file is a central part of your Terraform project and serves as the blueprint for your infrastructure.
Here's a short description of what you might find in a main.tf
file:
Resource Definitions: Declare the cloud resources (e.g., virtual machines, databases, networks) you want to create and configure.
Provider Configuration: Specify the cloud provider you're using (e.g., AWS, Azure) and any necessary authentication details.
Variable Assignments: Set values for variables that might be used in your resource definitions.
Data Sources: Retrieve information from existing resources or external data sources.
Output Definitions: Define what information you want to display after Terraform applies the configuration.
Dependencies: Ensure resource dependencies are properly defined so Terraform can manage them in the correct order.
The main.tf
file is where you structure and define your infrastructure, and it's usually combined with other .tf
files and organized within modules to create a complete Terraform project.
variable.tf
A variables.tf
file in Terraform is used to define input variables for your infrastructure configuration. These variables allow you to customize and parameterize your Terraform configurations, making them more flexible and reusable. Here's a short description of what you might find in a variables.tf
file:
Variable Declarations: In this file, you define the variables you want to use in your Terraform configuration. You specify the variable name, type, and an optional default value if needed.
Example:
variable "instance_count" { description = "Number of instances to create" type = number default = 1 } variable "instance_type" { description = "Type of the instance to launch" type = string }
Variable Types: Terraform supports various variable types, such as
string
,number
,bool
,list
, andmap
, among others. You specify the appropriate type for each variable to ensure type safety.Variable Descriptions: You can include descriptions for your variables to provide information about their purpose and usage. These descriptions are helpful for documentation and communication within your team.
Default Values: You can set default values for variables, which are used when a value is not explicitly provided when calling the Terraform configuration. Default values make it convenient to use the configuration without specifying every variable explicitly.
By defining variables in a variables.tf
file, you make your Terraform configurations more adaptable to different scenarios and environments. These variables can be set when running terraform plan
or terraform apply
, allowing you to customize your infrastructure as needed.
provider.tf
A provider.tf
file in Terraform is used to configure and specify the cloud provider or backend infrastructure that your Terraform configuration will interact with. This file is essential for defining the settings and credentials needed to authenticate and work with the chosen provider. Here's a short description of what you might find in a provider.tf
file:
Provider Configuration: In this file, you specify the details of the cloud provider or backend service you intend to use. This includes the provider's name (e.g., AWS, Azure, Google Cloud), region, and any other provider-specific settings.
Example for AWS:
provider "aws" { region = "us-west-2" }
Authentication and Credentials: Depending on the provider, you may need to provide authentication credentials, such as access keys or service account credentials. These credentials are typically stored securely and provided here.
Example for AWS:
provider "aws" { region = "us-west-2" access_key = "your-access-key" secret_key = "your-secret-key" }
Provider Aliases: You can use provider aliases to manage multiple instances of the same provider within a single configuration. Aliases allow you to differentiate between provider configurations when you have multiple regions or accounts.
Example with Aliases:
provider "aws" { alias = "east" region = "us-east-1" } provider "aws" { alias = "west" region = "us-west-2" }
Backend Configuration (Optional): If you're using a remote backend, such as AWS S3 or HashiCorp Consul, you may also configure it in the
provider.tf
file. This is where you specify backend-specific settings like storage buckets or endpoints.Example for AWS S3 Backend:
terraform { backend "s3" { bucket = "my-terraform-state" key = "terraform.tfstate" region = "us-east-1" } }
The provider.tf
file is a crucial part of your Terraform configuration because it establishes the connection to the target infrastructure or backend, ensuring that Terraform can create and manage resources in the specified environment.