Skip to content

Commit 641f41a

Browse files
author
abregman
committed
Add Terraform and AWS exercises
In addition to multiple new questions.
1 parent 842120d commit 641f41a

File tree

8 files changed

+550
-343
lines changed

8 files changed

+550
-343
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
:information_source:  This repo contains questions and exercises on various technical topics, sometimes related to DevOps and SRE
44

5-
:bar_chart:  There are currently **2371** exercises and questions
5+
:bar_chart:  There are currently **2376** exercises and questions
66

77
:books:  To learn more about DevOps and SRE, check the resources in [devops-resources](https://github.com/bregman-arie/devops-resources) repository
88

exercises/aws/README.md

Lines changed: 172 additions & 167 deletions
Large diffs are not rendered by default.

exercises/terraform/README.md

Lines changed: 223 additions & 175 deletions
Large diffs are not rendered by default.

exercises/terraform/launch_ec2_web_instance/exercise.md

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Rename S3 Bucket
2+
3+
## Requirements
4+
5+
* An existing S3 bucket tracked by Terraform.
6+
If you don't have it, you can use the following block and run `terraform apply`:
7+
8+
```terraform
9+
resource "aws_s3_bucket" "some_bucket" {
10+
bucket = "some-old-bucket"
11+
}
12+
```
13+
14+
## Objectives
15+
16+
1. Rename an existing S3 bucket and make sure it's still tracked by Terraform
17+
18+
## Solution
19+
20+
Click [here to view the solution](solution.md)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Rename S3 Bucket
2+
3+
## Requirements
4+
5+
* An existing S3 bucket tracked by Terraform.
6+
If you don't have it, you can use the following block and run `terraform apply`:
7+
8+
```terraform
9+
resource "aws_s3_bucket" "some_bucket" {
10+
bucket = "some-old-bucket"
11+
}
12+
```
13+
14+
## Objectives
15+
16+
1. Rename an existing S3 bucket and make sure it's still tracked by Terraform
17+
18+
## Solution
19+
20+
```sh
21+
# A bucket name is immutable in AWS so we'll have to create a new bucket
22+
aws s3 mb s3://some-new-bucket-123
23+
24+
# Sync old bucket to new bucket
25+
aws s3 sync s3://some-old-bucket s3://some-new-bucket-123
26+
27+
# Remove the old bucket from Terraform's state
28+
terraform state rm aws_s3_bucket.some_bucket
29+
30+
# Import new bucket to Terraform's state
31+
terraform import aws_s3_bucket.some_bucket some-new-bucket-123
32+
33+
: '
34+
aws_s3_bucket.some_bucket: Refreshing state... [id=some-new-bucket-123]
35+
36+
Import successful!
37+
The resources that were imported are shown above. These resources are now in
38+
your Terraform state and will henceforth be managed by Terraform.
39+
'
40+
41+
# Modify the Terraform definition to include the new name
42+
# resource "aws_s3_bucket" "some_bucket" {
43+
# bucket = "some-new-bucket-123"
44+
# }
45+
46+
# Remove old bucket
47+
aws s3 rm s3://some-old-bucket --recursive
48+
aws s3 rb s3://some-old-bucket
49+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Local Provider
2+
3+
## Objectives
4+
5+
Learn how to use and run Terraform basic commands
6+
7+
1. Create a directory called "my_first_run"
8+
2. Inside the directory create a file called "main.tf" with the following content
9+
10+
```terraform
11+
resource "local_file" "mario_local_file" {
12+
content = "It's a me, Mario!"
13+
filename = "/tmp/who_is_it.txt"
14+
}
15+
```
16+
3. Run `terraform init`. What did it do?
17+
4. Run `terraform plan`. What Terraform is going to perform?
18+
5. Finally, run 'terraform apply' and verify the file was created
19+
20+
## Solution
21+
22+
Click [here to view the solution](solution.md)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Local Provider
2+
3+
## Objectives
4+
5+
Learn how to use and run Terraform basic commands
6+
7+
1. Create a directory called "my_first_run"
8+
2. Inside the directory create a file called "main.tf" with the following content
9+
10+
```terraform
11+
resource "local_file" "mario_local_file" {
12+
content = "It's a me, Mario!"
13+
filename = "/tmp/who_is_it.txt"
14+
}
15+
```
16+
3. Run `terraform init`. What did it do?
17+
4. Run `terraform plan`. What Terraform is going to perform?
18+
5. Finally, run 'terraform apply' and verify the file was created
19+
20+
## Solution
21+
22+
```sh
23+
# Create a directory
24+
mkdir my_first_run && cd my_first_run
25+
26+
# Create the file 'main.tf'
27+
cat << EOT >> main.tf
28+
resource "local_file" "mario_local_file" {
29+
content = "It's a me, Mario!"
30+
filename = "/tmp/who_is_it.txt"
31+
}
32+
EOT
33+
34+
# Run 'terraform init'
35+
terraform init
36+
# Running 'ls -la' you'll it created '.terraform' and '.terraform.lock.hcl'
37+
# In addition, it initialized (downloaded and installed) the relevant provider plugins. In this case, the "hashicorp/local"
38+
39+
# Run 'terraform plan'
40+
terraform plan
41+
# It shows what Terraform is going to perform once you'll run 'terraform apply'
42+
43+
<< terraform_plan_output
44+
Terraform will perform the following actions:
45+
46+
# local_file.mario_local_file will be created
47+
+ resource "local_file" "mario_local_file" {
48+
+ content = "It's a me, Mario!"
49+
+ directory_permission = "0777"
50+
+ file_permission = "0777"
51+
+ filename = "/tmp/who_is_it.txt"
52+
+ id = (known after apply)
53+
}
54+
55+
Plan: 1 to add, 0 to change, 0 to destroy.
56+
terraform_plan_output
57+
58+
# Apply main.tf (it's better to run without -auto-approve if you are new to Terraform)
59+
terraform apply -auto-approve
60+
61+
ls /tmp/who_is_it.txt
62+
# /tmp/who_is_it.txt
63+
```

0 commit comments

Comments
 (0)