DEV Community

Cover image for Terraform Core Concepts: Providers, Resources, and Data Sources: Day 2
Avesh
Avesh

Posted on

Terraform Core Concepts: Providers, Resources, and Data Sources: Day 2

Terraform Core Concepts: Providers, Resources, and Data Sources

Terraform is an open-source Infrastructure as Code (IaC) tool that enables you to define, provision, and manage your infrastructure through configuration files. At the heart of Terraform's functionality are its core concepts: Providers, Resources, and Data Sources. This article delves into these concepts with hands-on examples, ensuring you gain a practical understanding of how Terraform operates.

Provider Configuration

Providers are plugins in Terraform that interact with APIs to manage and provision resources. Each provider allows Terraform to work with a specific cloud service, on-premise infrastructure, or SaaS platform, such as AWS, Azure, Google Cloud, or Kubernetes.

Example:

To use a provider, you need to configure it within your Terraform script:

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The provider block specifies that Terraform will interact with AWS.
  • The region argument determines the AWS region where resources will be created.

Hands-on Practice:

  1. Install Terraform.
  2. Create a new directory and initialize Terraform using terraform init.
  3. Add the above provider block to a file named main.tf.
  4. Run terraform init to download the AWS provider plugin.

Resource Blocks

Resources define the infrastructure objects you want to create or manage, such as virtual machines, databases, or networking components. Each resource is declared in a resource block.

Example:

Creating an AWS S3 bucket:

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket-name"
  acl    = "private"
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • aws_s3_bucket is the type of resource.
  • my_bucket is the logical name used to reference this resource within the Terraform configuration.
  • The bucket argument specifies the bucket name, and acl sets its access control.

Hands-on Practice:

  1. Add the above resource block to your main.tf file.
  2. Run terraform plan to preview the changes Terraform will apply.
  3. Execute terraform apply to create the S3 bucket.
  4. Verify the bucket in the AWS Management Console.

Data Sources

Data sources allow you to retrieve information about existing resources, which can be used in your configuration. They are particularly useful when you need to reference resources not created by Terraform.

Example:

Fetching details of an existing AWS VPC:

data "aws_vpc" "selected" {
  default = true
}

resource "aws_subnet" "my_subnet" {
  vpc_id     = data.aws_vpc.selected.id
  cidr_block = "10.0.1.0/24"
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The data block retrieves information about the default VPC.
  • The aws_subnet resource uses the VPC ID from the data source to create a subnet within that VPC.

Hands-on Practice:

  1. Add the above blocks to your main.tf file.
  2. Run terraform plan to see how the data source and resource interact.
  3. Apply the changes using terraform apply.

Understanding Dependencies

Terraform automatically manages resource dependencies using an internal dependency graph. When one resource depends on another, Terraform ensures the dependent resource is created or updated in the correct order.

Example:

Creating an EC2 instance after creating a security group:

resource "aws_security_group" "my_sg" {
  name_prefix = "example-"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "my_instance" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  security_groups = [aws_security_group.my_sg.name]
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The aws_instance resource depends on the aws_security_group because it references the security group’s name.
  • Terraform's dependency graph ensures the security group is created before the EC2 instance.

Hands-on Practice:

  1. Add the above resources to your main.tf file.
  2. Run terraform plan to see the dependency graph.
  3. Execute terraform apply to provision the resources.

Conclusion

Terraform’s core concepts—Providers, Resources, and Data Sources—form the foundation of its ability to manage infrastructure efficiently. By configuring providers, defining resources, utilizing data sources, and understanding dependencies, you can create robust and reusable infrastructure as code. Practice these examples to solidify your understanding and become proficient in Terraform.

Top comments (2)

Collapse
 
michelsylvestre profile image
Michel Sylvestre

Again, great post! Speaking about dependencies, sometimes Terraform doesn't know which resource must be created first if there's no direct reference (like when a resource depends on a role assigment in Azure for example).

In these cases, you can specify an explicit dependency on all needed resources (it's an array).

resource "provider" "first" {
  argument = "value"
}

resource "provider" "second" {
  depends_on = [
    provider.first 
  ]
  argument     = "value"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
i_am_vesh profile image
Avesh

Thanks ill add this in future posts