Block
Are you looking to get certified in DevOps, SRE and DevSecOps?
DevOps
Get Certified!
SRE
Ahead from others!
DevSecOps
Security is Key
Kubernetes
Tomorow's Platform!

Data Sources in Terraform resources explained with example

  • Data sources provide dynamic information about entities that are not managed by the current Terraform and configuration.
  • Variables provide static information.
  • Referencing a resource defined in a data source won’t create the resource itself, and your plan will fail if you reference nonexistent data or infrastructure.
  • Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.
  • External data sources must return information in JSON format.

This may include:

  • Configuration data from Consul
  • Information about the state of manually-configured infrastructure components
  • Another Terraform configuration
  • Defined outside of Terraform
  • Defined by another separate Terraform configuration.

data "azurerm_virtual_machine" "example" {
  name                = "simple-vm"
  resource_group_name = "demo"
}

output "virtual_machine_id" {
  value = data.azurerm_virtual_machine.example.id
}

Here’s an example of using the AWS data source in Terraform to retrieve information about an existing Amazon S3 bucket

In this example, we’re using the aws_s3_bucket data source to retrieve information about an existing S3 bucket named example-bucket. We’re then outputting several attributes of the bucket using the output block.

The aws_s3_bucket data source retrieves the specified bucket’s attributes such as bucket, region, arn, policy, id, acceleration_status, versioning, website_domain, website_endpoint, and logging as per the below documentation. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket


data "aws_s3_bucket" "example" {
  bucket = "example-bucket"
}

output "bucket_name" {
  value = data.aws_s3_bucket.example.bucket
}

output "bucket_region" {
  value = data.aws_s3_bucket.example.region
}

output "bucket_arn" {
  value = data.aws_s3_bucket.example.arn
}

output "bucket_policy" {
  value = data.aws_s3_bucket.example.policy
}

Here’s an example of using the AWS data source in Terraform to retrieve information about an existing availability zone

In this example, we’re using the aws_availability_zone data source to retrieve information about the availability zone named us-west-2a in the US West (Oregon) region. We’re then outputting the zone_id and zone_name attributes of the availability zone using the output block.

The aws_availability_zone data source retrieves the specified availability zone’s attributes such as zone_id, zone_name, region_name, and opt_in_status as per the below documentation. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zone


data "aws_availability_zone" "example" {
  state = "available"
  name  = "us-west-2a"
}

output "zone_id" {
  value = data.aws_availability_zone.example.zone_id
}

output "zone_name" {
  value = data.aws_availability_zone.example.zone_name
}

Rajesh Kumar
Follow me