Files

This lesson will teach you about Terraform files and how to use them.

Terraform files#

We have seen in the locals chapter how you can place a multi-line string as a value for a property. This is useful for something like an IAM policy. It can be even cleaner to move the value out into a file and then reference that file from your project. Doing this may remove the clutter from your project and make it much more readable.

Project example#

Let’s see an example of using files:

main.tf
policy.iam
Terraform files project example

The IAM policy creates a policy that gives list bucket permission to any bucket. If you look at the Terraform code, you will see we are configuring the AWS provider, as we will be connecting to AWS. Then we are defining an AWS IAM policy. Instead of placing the policy inline as we did in a previous chapter, we referenced the policy from the file policy.iam. We are calling the file function and passing in the argument as to where the file is.

📝Note: policy.iam is a relative file path from our current location.

Running the project#

Clicking the terminal will run terraform init and then terraform apply. When prompted, enter yes to run the project. Terraform will create the IAM policy you can see in the file and name it list-buckets-policy.

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
Terminal 1
Terminal

Click to Connect...

📝Note: Once you are done with the project, do not forget to run terraform destroy and then confirm with yes to delete all of the resources in this project.

🎉Quiz
Templatefile Function
Mark as Completed
Report an Issue