forked from bregman-arie/devops-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In addition to multiple new questions.
- Loading branch information
abregman
committed
Jul 21, 2022
1 parent
842120d
commit 641f41a
Showing
8 changed files
with
550 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Rename S3 Bucket | ||
|
||
## Requirements | ||
|
||
* An existing S3 bucket tracked by Terraform. | ||
If you don't have it, you can use the following block and run `terraform apply`: | ||
|
||
```terraform | ||
resource "aws_s3_bucket" "some_bucket" { | ||
bucket = "some-old-bucket" | ||
} | ||
``` | ||
|
||
## Objectives | ||
|
||
1. Rename an existing S3 bucket and make sure it's still tracked by Terraform | ||
|
||
## Solution | ||
|
||
Click [here to view the solution](solution.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Rename S3 Bucket | ||
|
||
## Requirements | ||
|
||
* An existing S3 bucket tracked by Terraform. | ||
If you don't have it, you can use the following block and run `terraform apply`: | ||
|
||
```terraform | ||
resource "aws_s3_bucket" "some_bucket" { | ||
bucket = "some-old-bucket" | ||
} | ||
``` | ||
|
||
## Objectives | ||
|
||
1. Rename an existing S3 bucket and make sure it's still tracked by Terraform | ||
|
||
## Solution | ||
|
||
```sh | ||
# A bucket name is immutable in AWS so we'll have to create a new bucket | ||
aws s3 mb s3://some-new-bucket-123 | ||
|
||
# Sync old bucket to new bucket | ||
aws s3 sync s3://some-old-bucket s3://some-new-bucket-123 | ||
|
||
# Remove the old bucket from Terraform's state | ||
terraform state rm aws_s3_bucket.some_bucket | ||
|
||
# Import new bucket to Terraform's state | ||
terraform import aws_s3_bucket.some_bucket some-new-bucket-123 | ||
|
||
: ' | ||
aws_s3_bucket.some_bucket: Refreshing state... [id=some-new-bucket-123] | ||
Import successful! | ||
The resources that were imported are shown above. These resources are now in | ||
your Terraform state and will henceforth be managed by Terraform. | ||
' | ||
|
||
# Modify the Terraform definition to include the new name | ||
# resource "aws_s3_bucket" "some_bucket" { | ||
# bucket = "some-new-bucket-123" | ||
# } | ||
|
||
# Remove old bucket | ||
aws s3 rm s3://some-old-bucket --recursive | ||
aws s3 rb s3://some-old-bucket | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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 | ||
|
||
Click [here to view the solution](solution.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 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 | ||
``` |