You've already forked devops-exercises
Rename exercises dir
Name it instead "topics" so it won't be strange if some topics included "exercises" directory.
This commit is contained in:
12
topics/terraform/exercises/launch_ec2_instance/exercise.md
Normal file
12
topics/terraform/exercises/launch_ec2_instance/exercise.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Launch EC2 instance
|
||||
|
||||
## Reuqirements
|
||||
|
||||
* AWS account
|
||||
|
||||
## Objectives
|
||||
|
||||
1. Write Terraform configuration for launching an EC2 instance
|
||||
2. Run the commands to apply the configuration and create the EC2 instance
|
||||
3. What happens if you run again `terraform apply`?
|
||||
4. Destroy the instance you've created with Terraform
|
||||
65
topics/terraform/exercises/launch_ec2_instance/solution.md
Normal file
65
topics/terraform/exercises/launch_ec2_instance/solution.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# Launch EC2 instance
|
||||
|
||||
## Reuqirements
|
||||
|
||||
* AWS account
|
||||
|
||||
## Objectives
|
||||
|
||||
1. Write Terraform configuration for launching an EC2 instance
|
||||
2. Run the commands to apply the configuration and create the EC2 instance
|
||||
3. What happens if you run again `terraform apply`?
|
||||
4. Destroy the instance you've created with Terraform
|
||||
|
||||
## Solution
|
||||
|
||||
```
|
||||
mkdir exercise
|
||||
|
||||
cat << EOT >> main.tf
|
||||
terraform {
|
||||
required_providers {
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = "~> 4.16"
|
||||
}
|
||||
}
|
||||
|
||||
required_version = ">= 1.2.0"
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
region = "us-west-2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "app_server" {
|
||||
ami = "ami-830c94e3"
|
||||
instance_type = "t2.micro"
|
||||
|
||||
tags = {
|
||||
Name = "ExampleAppServerInstance"
|
||||
}
|
||||
}
|
||||
EOT
|
||||
|
||||
terraform init
|
||||
terraform validate
|
||||
terraform plan
|
||||
|
||||
# You should see this line at the end: Plan: 1 to add, 0 to change, 0 to destroy
|
||||
|
||||
terraform apply -auto-approve
|
||||
|
||||
# You should see the following output:
|
||||
# aws_instance.app_server: Creation complete after 49s [id=i-004651a9d4427d236
|
||||
|
||||
# Running 'terraform apply' again won't change anything as
|
||||
# Terraform will compare actual infrastructure to your
|
||||
# configuration and won't find any difference. You should see the following line:
|
||||
# Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
|
||||
|
||||
# Remove instance
|
||||
terraform destroy -auto-approve
|
||||
|
||||
# Destroy complete! Resources: 1 destroyed.
|
||||
```
|
||||
20
topics/terraform/exercises/s3_bucket_rename/exercise.md
Normal file
20
topics/terraform/exercises/s3_bucket_rename/exercise.md
Normal file
@@ -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
topics/terraform/exercises/s3_bucket_rename/solution.md
Normal file
49
topics/terraform/exercises/s3_bucket_rename/solution.md
Normal file
@@ -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
|
||||
```
|
||||
@@ -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)
|
||||
@@ -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
|
||||
```
|
||||
Reference in New Issue
Block a user