IaC - Terraform: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
imported>Unknown user
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:


* '''Declarative Language''': You describe the desired state of the infrastructure, and Terraform figures out how to achieve that state.
* '''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.
* '''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.
* '''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.
* '''Modular and Reusable''': Terraform configurations can be modularized, promoting code reuse.
Line 11: Line 11:
* '''Immutable Infrastructure''': Instead of modifying existing resources, Terraform provisions new resources based on the changes in the configuration.
* '''Immutable Infrastructure''': Instead of modifying existing resources, Terraform provisions new resources based on the changes in the configuration.


<span id="creating-an-aws-ecs-container-and-an-rds-instance-using-terraform"></span>
= Creating an AWS ECS Container and an RDS instance using Terraform =
= Creating an AWS ECS Container and an RDS instance using Terraform =


<syntaxhighlight># Specify the AWS provider with a given region
<syntaxhighlight lang="TypeScript"># Specify the AWS provider with a given region
provider &quot;aws&quot; {
provider "aws" {
   region = &quot;us-west-1&quot;
   region = "us-west-1"
}
}


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


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


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


# Create an ECS service
# Create an ECS service
resource &quot;aws_ecs_service&quot; &quot;example&quot; {
resource "aws_ecs_service" "example" {
   name            = &quot;example-service&quot;
   name            = "example-service"
   cluster        = aws_ecs_cluster.example.id
   cluster        = aws_ecs_cluster.example.id
   task_definition = aws_ecs_task_definition.example.arn
   task_definition = aws_ecs_task_definition.example.arn
Line 58: Line 57:


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


output &quot;ecs_cluster_name&quot; {
output "ecs_cluster_name" {
   value = aws_ecs_cluster.example.name
   value = aws_ecs_cluster.example.name
}</syntaxhighlight>
}</syntaxhighlight>
Line 73: Line 72:
* We create an ECS service that refers to the previously defined task and cluster.
* We create an ECS service that refers to the previously defined task and cluster.


<span id="apply-this-configuration"></span>
= Apply this configuration =
= Apply this configuration =


Line 79: Line 77:
* 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 -->


[[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.