🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Tutorials: Named Values – Filesystem and workspace info

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 them as standalone expressions, or combine them with other expressions to compute new values.

Types of Named Values

The main kinds of named values available in Terraform are:

  • Resources
  • Input variables
  • Local values
  • Child module outputs
  • Data sources
  • Filesystem and workspace info
  • Block-local values

Filesystem and Workspace Info

The following values are available:

  • path.module 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.
  • path.root is the filesystem path of the root module of the configuration.
  • path.cwd 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.
  • terraform.workspace is the name of the currently selected workspace.

Example of path.module

provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "example_bucket" {
bucket = "example-bucket"
acl = "private"
tags = {
Name = "Example Bucket"
Environment = "Production"
}
lifecycle {
prevent_destroy = true
}
versioning {
enabled = true
}
# create a file in the bucket with the contents of the local file
dynamic "object" {
for_each = fileset("${path.module}/files", "*")
content {
bucket = aws_s3_bucket.example_bucket.id
key = object.value
source = "${path.module}/files/${object.value}"
}
}
}

In this example, we are using the AWS provider to create an S3 bucket resource. The tags block specifies some tags for the bucket, and the lifecycle block prevents the bucket from being destroyed. The versioning block enables versioning for the bucket.

We also use path.module to reference a directory called files within the same module. We use the fileset function to get a list of all files in the files directory, and then we create an S3 object for each file in the bucket using the dynamic block. The source parameter of the content block is set to ${path.module}/files/${object.value}, which resolves to the path of each file in the files directory.

Note that this is just an example, and in practice, you may need to modify the configuration to fit your specific use case.

Example of path.root

provider "aws" {
region = "us-east-1"
}
locals {
# Define the path to the directory containing your Terraform files
base_dir = path.root
}
resource "aws_s3_bucket" "example_bucket" {
bucket = "example-bucket-${local.base_dir}"
acl = "private"
}

In this example, we’re using path.root to define the base_dir 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.

Note that we’re prefixing the bucket name with ${local.base_dir}. This is just one example of how you can use the path.root function in your Terraform code – you can use it to construct file paths, define resource names, and more.

Example of path.cwd

locals {
# Define the path to the current working directory
current_dir = path.cwd
}
resource "aws_s3_bucket" "example_bucket" {
bucket = "example-bucket-${local.current_dir}"
acl = "private"
}

In this example, we’re using path.cwd to define the current_dir 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.

Note that we’re prefixing the bucket name with ${local.current_dir}. This is just one example of how you can use the path.cwd function in your Terraform code – you can use it to construct file paths, define resource names, and more.

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x