devops-exercises/exercises/terraform/terraform_local_provider/solution.md

63 lines
1.7 KiB
Markdown
Raw Normal View History

# Local Provider
## Objectives
Learn how to use and run Terraform basic commands
1. Create a directory called "my_first_run"
2. Inside the directory create a file called "main.tf" with the following content
```terraform
resource "local_file" "mario_local_file" {
content = "It's a me, Mario!"
filename = "/tmp/who_is_it.txt"
}
```
3. Run `terraform init`. What did it do?
4. Run `terraform plan`. What Terraform is going to perform?
5. Finally, run 'terraform apply' and verify the file was created
## Solution
```sh
# Create a directory
mkdir my_first_run && cd my_first_run
# Create the file 'main.tf'
cat << EOT >> main.tf
resource "local_file" "mario_local_file" {
content = "It's a me, Mario!"
filename = "/tmp/who_is_it.txt"
}
EOT
# Run 'terraform init'
terraform init
# Running 'ls -la' you'll it created '.terraform' and '.terraform.lock.hcl'
# In addition, it initialized (downloaded and installed) the relevant provider plugins. In this case, the "hashicorp/local"
# Run 'terraform plan'
terraform plan
# It shows what Terraform is going to perform once you'll run 'terraform apply'
<< terraform_plan_output
Terraform will perform the following actions:
# local_file.mario_local_file will be created
+ resource "local_file" "mario_local_file" {
+ content = "It's a me, Mario!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/tmp/who_is_it.txt"
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
terraform_plan_output
# Apply main.tf (it's better to run without -auto-approve if you are new to Terraform)
terraform apply -auto-approve
ls /tmp/who_is_it.txt
# /tmp/who_is_it.txt
```