{"id":7586,"date":"2023-08-01T19:29:23","date_gmt":"2023-08-01T19:29:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7586"},"modified":"2023-08-01T19:29:25","modified_gmt":"2023-08-01T19:29:25","slug":"how-to-use-one-modules-variable-in-another-module-in-terraform","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-use-one-modules-variable-in-another-module-in-terraform\/","title":{"rendered":"How to use one module&#8217;s variable in another module in terraform?"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>To get the output from one module to another module in Terraform, you can use the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Define the output in the first module.<\/li>\n\n\n\n<li>Import the second module.<\/li>\n\n\n\n<li>Reference the output of the first module in the second module.<\/li>\n<\/ol>\n\n\n\n<p>Here is an example of how to do this:<\/p>\n\n\n\n<p><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># First module<\/span>\r\nmodule <span class=\"hljs-string\">\"web_server\"<\/span> {\r\n  source = <span class=\"hljs-string\">\".\/web_server\"<\/span>\r\n\r\n  output <span class=\"hljs-string\">\"instance_ip_addr\"<\/span> {\r\n    value = aws_instance.web_server.public_ip\r\n  }\r\n}\r\n\r\n<span class=\"hljs-comment\"># Second module<\/span>\r\nmodule <span class=\"hljs-string\">\"database\"<\/span> {\r\n  source = <span class=\"hljs-string\">\".\/database\"<\/span>\r\n\r\n  instance_ip_addr = module.web_server.instance_ip_addr\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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<p>In this example, the <code>web_server<\/code> module defines an output called <code>instance_ip_addr<\/code>. The <code>database<\/code> module imports the <code>web_server<\/code> module and then references the <code>instance_ip_addr<\/code> output in its own configuration.<\/p>\n\n\n\n<p>When you run <code>terraform apply<\/code>, Terraform will create the resources defined in both modules. The <code>database<\/code> module will use the <code>instance_ip_addr<\/code> output from the <code>web_server<\/code> module to connect to the web server.<\/p>\n\n\n\n<p>Here is an explanation of the code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>source<\/code>&nbsp;attribute in the&nbsp;<code>module<\/code>&nbsp;block specifies the location of the module definition. In this case, the module definition is located in the&nbsp;<code>web_server<\/code>&nbsp;directory.<\/li>\n\n\n\n<li>The&nbsp;<code>output<\/code>&nbsp;block defines an output called&nbsp;<code>instance_ip_addr<\/code>. The value of the output is the public IP address of the web server instance.<\/li>\n\n\n\n<li>The&nbsp;<code>source<\/code>&nbsp;attribute in the&nbsp;<code>module<\/code>&nbsp;block specifies the location of the module definition. In this case, the module definition is located in the&nbsp;<code>database<\/code>&nbsp;directory.<\/li>\n\n\n\n<li>The&nbsp;<code>instance_ip_addr<\/code>&nbsp;attribute references the&nbsp;<code>instance_ip_addr<\/code>&nbsp;output from the&nbsp;<code>web_server<\/code>&nbsp;module.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>You can use outputs to accomplish this. In Terraform, a module can also return values. Again, this is done using a mechanism you already know: output variables.  Such as if there are 2 modules, kafka and cloudwatch.<\/p>\n\n\n\n<p>In your kafka module, you could define an output that looks something like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">\noutput <span class=\"hljs-string\">\"instance_ids\"<\/span> {\n  value = &#91;<span class=\"hljs-string\">\"${aws_instance.kafka.*.id}\"<\/span>]\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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<p><strong>Case 1 &#8211; If you are calling kafka module from cloudwatch module by instantiated the module with something like:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\nmodule <span class=\"hljs-string\">\"kafka\"<\/span> {\n  source = <span class=\"hljs-string\">\".\/modules\/kafka\"<\/span>\n}\n\n<span class=\"hljs-comment\"># You can then access that output as follows:<\/span>\ninstances = &#91;<span class=\"hljs-string\">\"${module.kafka.instance_ids}\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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<p><strong>Case 2 &#8211; If your modules are isolated from each other (i.e. your cloudwatch module doesn&#8217;t instantiate your kafka module), you can pass the outputs as variables between modules:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\nmodule <span class=\"hljs-string\">\"kafka\"<\/span> {\n  source = <span class=\"hljs-string\">\".\/modules\/kafka\"<\/span>\n}\n\nmodule <span class=\"hljs-string\">\"cloudwatch\"<\/span> {\n  source = <span class=\"hljs-string\">\".\/modules\/cloudwatch\"<\/span>\n  instances = &#91;<span class=\"hljs-string\">\"${module.kafka.instance_ids}\"<\/span>]\n}\n\n<span class=\"hljs-comment\"># Of course, your \"cloudwatch\" module would have to declare the instances variable.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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<p><strong>Case 3 &#8211; You can add the ASG name as an output variable in \/modules\/services\/webserver-cluster\/outputs.tf as follows.<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">output <span class=\"hljs-string\">\"asg_name\"<\/span> {\n  value       = aws_autoscaling_group.example.name\n  description = <span class=\"hljs-string\">\"The name of the Auto Scaling Group\"<\/span>\n}\n\nYou can access <span class=\"hljs-built_in\">module<\/span> output variables the same way <span class=\"hljs-keyword\">as<\/span> resource output attributes. The syntax is:\n\n<span class=\"hljs-built_in\">module<\/span>.&lt;MODULE_NAME&gt;.&lt;OUTPUT_NAME&gt;\n<span class=\"hljs-built_in\">module<\/span>.frontend.asg_name<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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<script src=\"https:\/\/gist.github.com\/devops-school\/618a1908d82f29769b85cc1e1258bcaf.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_42690\"><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_53997\"  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_42690\"  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>To get the output from one module to another module in Terraform, you can use the following steps: Here is an example of how to do this: In this example, the web_server module defines an output called instance_ip_addr. The database module imports the web_server module and then references the instance_ip_addr output in its own configuration&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":7783,"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-7586","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7586","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=7586"}],"version-history":[{"count":8,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7586\/revisions"}],"predecessor-version":[{"id":37665,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7586\/revisions\/37665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7783"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}