IaC - Terraform: Difference between revisions
Jump to navigation
Jump to search
m Maintenance script moved page UserDocumentation:Knowledge Central/KC IT/Devops definitions/Infrastructure as Code/IaC - Docker/IaC - Terraform to IaC - Terraform: Cleaned title and categorized |
No edit summary |
||
| (2 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 | * '''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. | ||
= 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 | provider "aws" { | ||
region = | region = "us-west-1" | ||
} | } | ||
# Create an RDS instance | # Create an RDS instance | ||
resource | resource "aws_db_instance" "example" { | ||
allocated_storage = 20 | allocated_storage = 20 | ||
storage_type = | storage_type = "gp2" | ||
engine = | engine = "mysql" | ||
engine_version = | engine_version = "5.7" | ||
instance_class = | instance_class = "db.t2.micro" | ||
name = | name = "mydb" | ||
username = | username = "example" | ||
password = | password = "yourpassword" | ||
parameter_group_name = | parameter_group_name = "default.mysql5.7" | ||
skip_final_snapshot = true | skip_final_snapshot = true | ||
} | } | ||
# Create an ECS cluster | # Create an ECS cluster | ||
resource | resource "aws_ecs_cluster" "example" { | ||
name = | name = "example-cluster" | ||
} | } | ||
# Create a task definition for the ECS service | # Create a task definition for the ECS service | ||
resource | resource "aws_ecs_task_definition" "example" { | ||
family = | family = "example" | ||
network_mode = | network_mode = "bridge" | ||
container_definitions = jsonencode([{ | container_definitions = jsonencode([{ | ||
name = | name = "example-container" | ||
image = | image = "nginx:latest" | ||
memory = 512 | memory = 512 | ||
}]) | }]) | ||
| Line 50: | Line 49: | ||
# Create an ECS service | # Create an ECS service | ||
resource | resource "aws_ecs_service" "example" { | ||
name = | 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 | output "rds_address" { | ||
value = aws_db_instance.example.address | value = aws_db_instance.example.address | ||
} | } | ||
output | 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. | ||
= 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. | ||
[[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.