IaC - Terraform: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
imported>Unknown user
No edit summary
 
No edit summary
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Terraform is an open-source infrastructure as code (IaC) software tool created by HashiCorp. It allows users to define, provision, and manage infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.


<!-- From bodyContent 1543234.mraw -->
<span id="features-of-terraform"></span>
= Features of Terraform =


* '''Declarative Language''': You describe the desired state of the infrastructure, and Terraform figures out how to achieve that state.
* '''Provider Ecosystem''': Terraform is platform-agnostic and has an extensive collection of plugins called "providers" for various cloud platforms and services like AWS, Azure, GCP, and many others.
* '''State Management''': Terraform maintains a state file that represents the actual state of the resources in the cloud. This allows Terraform to determine the differences between the current state and the desired state.
* '''Modular and Reusable''': Terraform configurations can be modularized, promoting code reuse.
* '''Plan &amp; Apply''': Before making changes, Terraform provides a plan command that shows what actions it will take. The apply command is then used to apply those changes.
* '''Immutable Infrastructure''': Instead of modifying existing resources, Terraform provisions new resources based on the changes in the configuration.
= Creating an AWS ECS Container and an RDS instance using Terraform =
<syntaxhighlight lang="TypeScript"># Specify the AWS provider with a given region
provider "aws" {
  region = "us-west-1"
}
# Create an RDS instance
resource "aws_db_instance" "example" {
  allocated_storage    = 20
  storage_type        = "gp2"
  engine              = "mysql"
  engine_version      = "5.7"
  instance_class      = "db.t2.micro"
  name                = "mydb"
  username            = "example"
  password            = "yourpassword"
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
}
# Create an ECS cluster
resource "aws_ecs_cluster" "example" {
  name = "example-cluster"
}
# Create a task definition for the ECS service
resource "aws_ecs_task_definition" "example" {
  family                = "example"
  network_mode          = "bridge"
  container_definitions = jsonencode([{
    name  = "example-container"
    image = "nginx:latest"
    memory = 512
  }])
}
# Create an ECS service
resource "aws_ecs_service" "example" {
  name            = "example-service"
  cluster        = aws_ecs_cluster.example.id
  task_definition = aws_ecs_task_definition.example.arn
  desired_count  = 1
}
# Outputs
output "rds_address" {
  value = aws_db_instance.example.address
}
output "ecs_cluster_name" {
  value = aws_ecs_cluster.example.name
}</syntaxhighlight>
Explanation:
* We specify the AWS provider with a given region (us-west-1 in this case).
* We create an RDS instance with MySQL 5.7, 20GB of storage, and provide a username and password.
* We create an ECS cluster.
* We define a task for the ECS service which uses the nginx image.
* We create an ECS service that refers to the previously defined task and cluster.
= Apply this configuration =
* Initialize Terraform in your directory using terraform init.
* Plan the changes using terraform plan.
* Apply the changes using terraform apply.
[[Category:Infrastructure as Code]]

Latest revision as of 12:08, 22 October 2025

Terraform is an open-source infrastructure as code (IaC) software tool created by HashiCorp. It allows users to define, provision, and manage infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.

Features of Terraform

  • Declarative Language: You describe the desired state of the infrastructure, and Terraform figures out how to achieve that state.
  • Provider Ecosystem: Terraform is platform-agnostic and has an extensive collection of plugins called "providers" for various cloud platforms and services like AWS, Azure, GCP, and many others.
  • State Management: Terraform maintains a state file that represents the actual state of the resources in the cloud. This allows Terraform to determine the differences between the current state and the desired state.
  • Modular and Reusable: Terraform configurations can be modularized, promoting code reuse.
  • Plan & Apply: Before making changes, Terraform provides a plan command that shows what actions it will take. The apply command is then used to apply those changes.
  • Immutable Infrastructure: Instead of modifying existing resources, Terraform provisions new resources based on the changes in the configuration.

Creating an AWS ECS Container and an RDS instance using Terraform

# Specify the AWS provider with a given region
provider "aws" {
  region = "us-west-1"
}

# Create an RDS instance
resource "aws_db_instance" "example" {
  allocated_storage    = 20
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  name                 = "mydb"
  username             = "example"
  password             = "yourpassword"
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
}

# Create an ECS cluster
resource "aws_ecs_cluster" "example" {
  name = "example-cluster"
}

# Create a task definition for the ECS service
resource "aws_ecs_task_definition" "example" {
  family                = "example"
  network_mode          = "bridge"
  container_definitions = jsonencode([{
    name  = "example-container"
    image = "nginx:latest"
    memory = 512
  }])
}

# Create an ECS service
resource "aws_ecs_service" "example" {
  name            = "example-service"
  cluster         = aws_ecs_cluster.example.id
  task_definition = aws_ecs_task_definition.example.arn
  desired_count   = 1
}

# Outputs
output "rds_address" {
  value = aws_db_instance.example.address
}

output "ecs_cluster_name" {
  value = aws_ecs_cluster.example.name
}

Explanation:

  • We specify the AWS provider with a given region (us-west-1 in this case).
  • We create an RDS instance with MySQL 5.7, 20GB of storage, and provide a username and password.
  • We create an ECS cluster.
  • We define a task for the ECS service which uses the nginx image.
  • We create an ECS service that refers to the previously defined task and cluster.

Apply this configuration

  • Initialize Terraform in your directory using terraform init.
  • Plan the changes using terraform plan.
  • Apply the changes using terraform apply.