Skip to content

Commit

Permalink
Add Terraform and AWS exercises
Browse files Browse the repository at this point in the history
In addition to multiple new questions.
  • Loading branch information
abregman committed Jul 21, 2022
1 parent 842120d commit 641f41a
Show file tree
Hide file tree
Showing 8 changed files with 550 additions and 343 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

:bar_chart:  There are currently **2371** exercises and questions
:bar_chart:  There are currently **2376** exercises and questions

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

Expand Down
339 changes: 172 additions & 167 deletions exercises/aws/README.md

Large diffs are not rendered by default.

398 changes: 223 additions & 175 deletions exercises/terraform/README.md

Large diffs are not rendered by default.

Empty file.
20 changes: 20 additions & 0 deletions exercises/terraform/s3_bucket_rename/exercise.md
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)
49 changes: 49 additions & 0 deletions exercises/terraform/s3_bucket_rename/solution.md
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
```
22 changes: 22 additions & 0 deletions exercises/terraform/terraform_local_provider/exercise.md
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)
63 changes: 63 additions & 0 deletions exercises/terraform/terraform_local_provider/solution.md
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
```

0 comments on commit 641f41a

Please sign in to comment.