Terraform is very popular due to its Infrastructure as Code functionality for cloud providers. But it also has many providers that allow us to manipulate the local system.
In the Querying external data with Terraform recipe, we discussed local script executions that are performed by Terraform to get data for external data sources.
In this recipe, we will study another type of local operation that involves creating and archiving local files with Terraform.
Getting ready
For this recipe, we don't need any prerequisites or base code – we will write the code from scratch.
The source code for this recipe is available at https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP02/files.
How to do it…
Perform the following steps:
- In a new folder called files, create a main.tf file. Write the following code inside it:
resource "local_file" "myfile" {
content = "This is my text"
filename = "../mytextfile.txt"
}
- In a command-line terminal, navigate to the files directory and execute Terraform's workflow commands, which are as follows:
terraform init
terraform plan -out="app.tfplan"
terraform apply "app.tfplan"
- In a new archive folder, create a main.tf file and write the following Terraform configuration inside it:
data "archive_file" "backup" {
type = "zip"
source_file = "../mytextfile.txt"
output_path = "${path.module}/archives/backup.zip"
}
- Then, using the command-line terminal, navigate to the archive directory and execute the following Terraform commands:
terraform init
terraform plan
How it works…
In step 1, we wrote a piece of Terraform configuration that uses the local provider and the local_file resource. This resource creates a file called mytextfile.txt and adds This is my text to it.
Then, in step 2, we executed Terraform on this code. By doing this, we obtained the mytextfile.txt file on our local disk.
The result of executing the terraform plan command on this code can be seen in the following screenshot:
After we executed terraform apply, the mytextfile.txt file became available on our local filesystem.
In the second part of this recipe, in step 3, we wrote a piece of Terraform configuration that uses the archive provider and the archive_file resource to create a ZIP file that contains the file we created in steps 1 and 2.
After we executed terraform apply, the ZIP archive backup.zip file became available on our local filesystem, in the archives folder.
There's more…
As we can see, the archive_file resource we used in the second part of this recipe is of the data block type (which we learned about in the Obtaining external data with data sources recipe of this chapter) and is therefore based on an element that already exists before we execute the terraform plan command.
In our case, the file to be included in the archive must already be present on the local disk.
See also
- Documentation on the local_file resource is available at https://www.terraform.io/docs/providers/local/r/file.html.
- Documentation on the archive_file resource is available at https://www.terraform.io/docs/providers/archive/d/archive_file.html.