IaC - Terraform: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
Added category IaC - Docker
No edit summary
Line 79: Line 79:
* Plan the changes using terraform plan.
* Plan the changes using terraform plan.
* Apply the changes using terraform apply.
* Apply the changes using terraform apply.
  <!-- From bodyContent 1543234.mraw -->
  <!-- From bodyContent 1543234.mraw -->[[Category:Infrastructure as Code]]
 
[[Category:IaC - Docker]]

Revision as of 19:11, 19 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 &quot;aws&quot; {
  region = &quot;us-west-1&quot;
}

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

# Create an ECS cluster
resource &quot;aws_ecs_cluster&quot; &quot;example&quot; {
  name = &quot;example-cluster&quot;
}

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

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

# Outputs
output &quot;rds_address&quot; {
  value = aws_db_instance.example.address
}

output &quot;ecs_cluster_name&quot; {
  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.