what is ?
Terraform is an open-source tool that allows you to define and manage your infrastructure using code. With Terraform, you can describe the desired state of your infrastructure in configuration files, and Terraform handles the provisioning, updating, and deletion of resources to achieve that state. Key features include support for multiple cloud providers, declarative syntax, state management, and modularity. Overall, Terraform simplifies infrastructure management by automating repetitive tasks and providing a consistent way to manage resources across different environments.
Prerequisites
The Terraform CLI (1.2.0+) installed.
The AWS CLI installed.
AWS account and associated credentials that allow you to create resources.
To use your IAM credentials to authenticate the Terraform AWS provider, set the AWS_ACCESS_KEY_ID
environment variable.
$ export AWS_ACCESS_KEY_ID=
Now, set your secret key.
$ export AWS_SECRET_ACCESS_KEY=
We provision resources that qualify under the AWS free tier. If your account does not qualify for free tier resources, we are not responsible for any charges that you may incur.
Write configuration
The set of files used to describe infrastructure in Terraform is known as a 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 aws-terraform
Change into the directory.
$ cd aws-terraform
Create a file to define your infrastructure.
$ touch main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
access_key = "*****************"
secret_key = "************************************"
region = "us-east-1"
}
resource "aws_instance" "app_server" {
ami = "ami-0c101f26f147fa7fd"
instance_type = "t2.micro"
tags = {
Name = "ExampleAppServerInstance"
}
}
The terraform {}
block in Terraform configuration files contains settings for provisioning infrastructure. Within this block, providers are specified, indicating which services Terraform will use. Each provider is defined with a source
attribute, which includes a hostname, namespace, and provider type. By default, Terraform installs providers from the Terraform Registry. In the provided example, the aws
provider's source is set to hashicorp/aws
, a shorthand for registry.terraform.io/hashicorp/aws
Providers: The
provider
block configures specific providers, such as AWS, which Terraform uses to create and manage resources. Providers act as plugins facilitating interactions with various infrastructure services.Resources: Resource blocks define different components of your infrastructure, such as EC2 instances or Heroku applications. Each resource block includes a type and a name, where the type specifies the resource type (e.g.,
aws_instance
) and the name provides a unique identifier within the configuration (e.g.,app_server
). Multiple resource blocks can be used to manage various resources across different providers.
Initialize the directory
When starting a new configuration or accessing an existing one from version control, the first step is to initialize the directory using terraform init
. This command downloads and installs the necessary providers specified in the configuration, such as the AWS provider. Initializing the directory ensures that Terraform has access to the required plugins and dependencies before proceeding with any further operations.
$ terraform init
To ensure consistent formatting and validate the configuration files, you can use the terraform fmt
command. This command automatically updates configurations in the current directory for readability and consistency.
terraform fmt
To ensure your configuration is syntactically valid and internally consistent, you can use the terraform validate
command.
terraform validate
To apply the configuration and create the infrastructure described in your Terraform files, you can use the terraform apply
command
terraform apply
My Output, after running the above comands ,
When you create resources the above files are created
Log into your AWS console the EC2 instance has been created
Cheers ๐ป