MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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
}Code language: JavaScript (javascript)


data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20230325"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

data "aws_availability_zones" "available" {
  state = "available"
}

output "virtual_machine_id" {
  value = data.aws_availability_zones.available.names[0]
}

output "virtual_machine_id1" {
  value = data.aws_availability_zones.available.names[1]
}


output "ips_with_list_interpolation" {
  value = [ for name in data.aws_availability_zones.available.names : name ]
}

resource "aws_instance" "first-ec2" {
  ami           = data.aws_ami.ubuntu.id # us-west-2
  instance_type = "t2.micro"
  tags = {
    Name = "RajeshKumar"
  }
  }
  
Code language: PHP (php)

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
}
Code language: JavaScript (javascript)

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
}
Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Terraform Architecture and Components through diagram

Terraform Architecture & End-to-End Workflow This reference architecture covers the standard Terraform CLI workflow, including execution from a developer machine or CI/CD runner. It shows the relationship…

Read More

Terraform Tutorials: What is namespace / Alias

In Terraform, a namespace is a logical grouping or isolation mechanism for resources within a cloud provider. It allows you to separate resources based on different environments,…

Read More

Terraform Tutorials: List of Terraform Blocks with Explanation

Example: terraform { required_version = “>= 0.14” backend “s3” { bucket = “my-terraform-state” key = “terraform.tfstate” region = “us-west-2” } } Example: provider “aws” { region =…

Read More

Terraform dynamaic block details explaination with example

A Terraform dynamic block generates repeated nested blocks inside a resource, data source, provider or provisioner, such as ingress rules in a security group or rule blocks…

Read More

Terraform Error Handing with Try Catch

Terraform has no try { } catch { } block. Error handling in HCL uses the try() function, which returns the first of its arguments that evaluates…

Read More

Terraform: Example Code for Create Multiple Azure Linux Vms

This page shows how to create several Azure Linux virtual machines from one Terraform configuration by using the count meta-argument. The VMs sit behind a load balancer…

Read More