{"id":33059,"date":"2023-03-31T05:00:02","date_gmt":"2023-03-31T05:00:02","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=33059"},"modified":"2023-03-31T05:17:38","modified_gmt":"2023-03-31T05:17:38","slug":"terraform-tutorials-named-values-filesystem-and-workspace-info","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-tutorials-named-values-filesystem-and-workspace-info\/","title":{"rendered":"Terraform Tutorials: Named Values &#8211; Filesystem and workspace info"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Filesystem and Workspace Info<\/h2>\n\n\n\n<p>Terraform makes several kinds of named values available. Each of these names is an expression that references the associated value. You can use them as standalone expressions, or combine them with other expressions to compute new values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Named Values<\/h2>\n\n\n\n<p>The main kinds of named values available in Terraform are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Resources<\/li>\n\n\n\n<li>Input variables<\/li>\n\n\n\n<li>Local values<\/li>\n\n\n\n<li>Child module outputs<\/li>\n\n\n\n<li>Data sources<\/li>\n\n\n\n<li>Filesystem and workspace info<\/li>\n\n\n\n<li>Block-local values<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Filesystem and Workspace Info<\/h1>\n\n\n\n<p>The following values are available:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>path.module<\/strong> is the filesystem path of the module where the expression is placed. We do not recommend using path.module in write operations because it can produce different behavior depending on whether you use remote or local module sources. Multiple invocations of local modules use the same source directory, overwriting the data in path.module during each call. This can lead to race conditions and unexpected results.<\/li>\n\n\n\n<li><strong>path.root <\/strong>is the filesystem path of the root module of the configuration.<\/li>\n\n\n\n<li><strong>path.cwd<\/strong> is the filesystem path of the original working directory from where you ran Terraform before applying any -chdir argument. This path is an absolute path that includes details about the filesystem structure. It is also useful in some advanced cases where Terraform is run from a directory other than the root module directory. We recommend using path.root or path.module over path.cwd where possible.<\/li>\n\n\n\n<li><strong>terraform.workspace<\/strong> is the name of the currently selected workspace.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example of <code>path.module<\/code><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/8b6edad241343fc735005498549f0a11.js\"><\/script>\n\n\n\n<p>In this example, we are using the AWS provider to create an S3 bucket resource. The <code>tags<\/code> block specifies some tags for the bucket, and the <code>lifecycle<\/code> block prevents the bucket from being destroyed. The <code>versioning<\/code> block enables versioning for the bucket.<\/p>\n\n\n\n<p>We also use <code>path.module<\/code> to reference a directory called <code>files<\/code> within the same module. We use the <code>fileset<\/code> function to get a list of all files in the <code>files<\/code> directory, and then we create an S3 object for each file in the bucket using the <code>dynamic<\/code> block. The <code>source<\/code> parameter of the <code>content<\/code> block is set to <code>${path.module}\/files\/${object.value}<\/code>, which resolves to the path of each file in the <code>files<\/code> directory.<\/p>\n\n\n\n<p>Note that this is just an example, and in practice, you may need to modify the configuration to fit your specific use case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of <code>path.root<\/code><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/31aa7cb77bb6a3be05b78dc5fe55c0d1.js\"><\/script>\n\n\n\n<p>In this example, we&#8217;re using <code>path.root<\/code> to define the <code>base_dir<\/code> local variable, which will contain the absolute path to the directory containing your Terraform files. We then use this variable to dynamically generate the name of an S3 bucket.<\/p>\n\n\n\n<p>Note that we&#8217;re prefixing the bucket name with <code>${local.base_dir}<\/code>. This is just one example of how you can use the <code>path.root<\/code> function in your Terraform code &#8211; you can use it to construct file paths, define resource names, and more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of <code>path.cwd<\/code><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/8ab16f07ff56e7ea29cc30e62e124521.js\"><\/script>\n\n\n\n<p>In this example, we&#8217;re using <code>path.cwd<\/code> to define the <code>current_dir<\/code> local variable, which will contain the absolute path to the current working directory. We then use this variable to dynamically generate the name of an S3 bucket.<\/p>\n\n\n\n<p>Note that we&#8217;re prefixing the bucket name with <code>${local.current_dir}<\/code>. This is just one example of how you can use the <code>path.cwd<\/code> function in your Terraform code &#8211; you can use it to construct file paths, define resource names, and more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Filesystem and Workspace Info Terraform makes several kinds of named values available. Each of these names is an expression that references the associated value. You can use&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-33059","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/33059","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=33059"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/33059\/revisions"}],"predecessor-version":[{"id":33065,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/33059\/revisions\/33065"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=33059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=33059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=33059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}