{"id":12954,"date":"2023-02-15T05:46:14","date_gmt":"2023-02-15T05:46:14","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=12954"},"modified":"2023-04-05T03:02:42","modified_gmt":"2023-04-05T03:02:42","slug":"data-sources-in-terraform-resources-explained-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/data-sources-in-terraform-resources-explained-with-example\/","title":{"rendered":"Data Sources in Terraform resources explained with example"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>Data sources provide dynamic information about entities that are not managed by the current Terraform and configuration.<\/li>\n\n\n\n<li>Variables provide static information.<\/li>\n\n\n\n<li>Referencing a resource defined in a data source won&#8217;t create the resource itself, and your plan will fail if you reference nonexistent data or infrastructure.<\/li>\n\n\n\n<li>Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.<\/li>\n\n\n\n<li>External data sources must return information in JSON format.<\/li>\n<\/ul>\n\n\n\n<p><strong>This may include:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Configuration data from Consul<\/li>\n\n\n\n<li>Information about the state of manually-configured infrastructure components<\/li>\n\n\n\n<li>Another Terraform configuration<\/li>\n\n\n\n<li>Defined outside of Terraform<\/li>\n\n\n\n<li>Defined by another separate Terraform configuration.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">data <span class=\"hljs-string\">\"azurerm_virtual_machine\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  name                = <span class=\"hljs-string\">\"simple-vm\"<\/span>\n  resource_group_name = <span class=\"hljs-string\">\"demo\"<\/span>\n}\n\noutput <span class=\"hljs-string\">\"virtual_machine_id\"<\/span> {\n  value = data.azurerm_virtual_machine.example.id\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\r\ndata <span class=\"hljs-string\">\"aws_ami\"<\/span> <span class=\"hljs-string\">\"ubuntu\"<\/span> {\r\n  most_recent = <span class=\"hljs-keyword\">true<\/span>\r\n\r\n  filter {\r\n    name   = <span class=\"hljs-string\">\"name\"<\/span>\r\n    values = &#91;<span class=\"hljs-string\">\"ubuntu\/images\/hvm-ssd\/ubuntu-jammy-22.04-amd64-server-20230325\"<\/span>]\r\n  }\r\n\r\n  filter {\r\n    name   = <span class=\"hljs-string\">\"virtualization-type\"<\/span>\r\n    values = &#91;<span class=\"hljs-string\">\"hvm\"<\/span>]\r\n  }\r\n\r\n  owners = &#91;<span class=\"hljs-string\">\"099720109477\"<\/span>] <span class=\"hljs-comment\"># Canonical<\/span>\r\n}\r\n\r\ndata <span class=\"hljs-string\">\"aws_availability_zones\"<\/span> <span class=\"hljs-string\">\"available\"<\/span> {\r\n  state = <span class=\"hljs-string\">\"available\"<\/span>\r\n}\r\n\r\noutput <span class=\"hljs-string\">\"virtual_machine_id\"<\/span> {\r\n  value = data.aws_availability_zones.available.names&#91;<span class=\"hljs-number\">0<\/span>]\r\n}\r\n\r\noutput <span class=\"hljs-string\">\"virtual_machine_id1\"<\/span> {\r\n  value = data.aws_availability_zones.available.names&#91;<span class=\"hljs-number\">1<\/span>]\r\n}\r\n\r\n\r\noutput <span class=\"hljs-string\">\"ips_with_list_interpolation\"<\/span> {\r\n  value = &#91; <span class=\"hljs-keyword\">for<\/span> name in data.aws_availability_zones.available.names : name ]\r\n}\r\n\r\nresource <span class=\"hljs-string\">\"aws_instance\"<\/span> <span class=\"hljs-string\">\"first-ec2\"<\/span> {\r\n  ami           = data.aws_ami.ubuntu.id <span class=\"hljs-comment\"># us-west-2<\/span>\r\n  instance_type = <span class=\"hljs-string\">\"t2.micro\"<\/span>\r\n  tags = {\r\n    Name = <span class=\"hljs-string\">\"RajeshKumar\"<\/span>\r\n  }\r\n  }\r\n  \r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/64c9d794533d4891eeaa942f3ca22e0b.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Here&#8217;s an example of using the AWS data source in Terraform to retrieve information about an existing Amazon S3 bucket<\/h2>\n\n\n\n<p>In this example, we&#8217;re using the <code>aws_s3_bucket<\/code> data source to retrieve information about an existing S3 bucket named <code>example-bucket<\/code>. We&#8217;re then outputting several attributes of the bucket using the <code>output<\/code> block.<\/p>\n\n\n\n<p>The <code>aws_s3_bucket<\/code> data source retrieves the specified bucket&#8217;s attributes such as <code>bucket<\/code>, <code>region<\/code>, <code>arn<\/code>, <code>policy<\/code>, <code>id<\/code>, <code>acceleration_status<\/code>, <code>versioning<\/code>, <code>website_domain<\/code>, <code>website_endpoint<\/code>, and <code>logging<\/code> as per the below documentation. <a href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/data-sources\/s3_bucket\" target=\"_blank\" rel=\"noopener\">https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/data-sources\/s3_bucket<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">data <span class=\"hljs-string\">\"aws_s3_bucket\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  bucket = <span class=\"hljs-string\">\"example-bucket\"<\/span>\n}\n\noutput <span class=\"hljs-string\">\"bucket_name\"<\/span> {\n  value = data.aws_s3_bucket.example.bucket\n}\n\noutput <span class=\"hljs-string\">\"bucket_region\"<\/span> {\n  value = data.aws_s3_bucket.example.region\n}\n\noutput <span class=\"hljs-string\">\"bucket_arn\"<\/span> {\n  value = data.aws_s3_bucket.example.arn\n}\n\noutput <span class=\"hljs-string\">\"bucket_policy\"<\/span> {\n  value = data.aws_s3_bucket.example.policy\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Here&#8217;s an example of using the AWS data source in Terraform to retrieve information about an existing availability zone<\/h2>\n\n\n\n<p>In this example, we&#8217;re using the <code>aws_availability_zone<\/code> data source to retrieve information about the availability zone named <code>us-west-2a<\/code> in the US West (Oregon) region. We&#8217;re then outputting the <code>zone_id<\/code> and <code>zone_name<\/code> attributes of the availability zone using the <code>output<\/code> block.<\/p>\n\n\n\n<p>The <code>aws_availability_zone<\/code> data source retrieves the specified availability zone&#8217;s attributes such as <code>zone_id<\/code>, <code>zone_name<\/code>, <code>region_name<\/code>, and <code>opt_in_status<\/code> as per the below documentation. <a href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/data-sources\/availability_zone\" target=\"_blank\" rel=\"noopener\">https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/data-sources\/availability_zone<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">data <span class=\"hljs-string\">\"aws_availability_zone\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  state = <span class=\"hljs-string\">\"available\"<\/span>\n  name  = <span class=\"hljs-string\">\"us-west-2a\"<\/span>\n}\n\noutput <span class=\"hljs-string\">\"zone_id\"<\/span> {\n  value = data.aws_availability_zone.example.zone_id\n}\n\noutput <span class=\"hljs-string\">\"zone_name\"<\/span> {\n  value = data.aws_availability_zone.example.zone_name\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_76317\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_32090\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_76317\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>This may include: Here&#8217;s an example of using the AWS data source in Terraform to retrieve information about an existing Amazon S3 bucket In this example, we&#8217;re using the aws_s3_bucket data source to retrieve information about an existing S3 bucket named example-bucket. We&#8217;re then outputting several attributes of the bucket using the output block. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5129],"tags":[],"class_list":["post-12954","post","type-post","status-publish","format-standard","hentry","category-terraform"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=12954"}],"version-history":[{"count":7,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12954\/revisions"}],"predecessor-version":[{"id":33253,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/12954\/revisions\/33253"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=12954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=12954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=12954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}