Skip to content

Commit a615d1b

Browse files
committed
IntoTheDevOps: Add End-to-End Terraform content
Signed-off-by: NotHarshhaa <reddyharshhaa12@gmail.com>
1 parent 52d518f commit a615d1b

File tree

8 files changed

+2115
-0
lines changed

8 files changed

+2115
-0
lines changed

topics/terraform/README.md

Lines changed: 1884 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Launch EC2 instance
2+
3+
## Requirements
4+
5+
* AWS account
6+
7+
## Objectives
8+
9+
1. Write Terraform configuration for launching an EC2 instance
10+
2. Run the commands to apply the configuration and create the EC2 instance
11+
3. What happens if you run again `terraform apply`?
12+
4. Destroy the instance you've created with Terraform
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Launch EC2 instance
2+
3+
## Requirements
4+
5+
* AWS account
6+
7+
## Objectives
8+
9+
1. Write Terraform configuration for launching an EC2 instance
10+
2. Run the commands to apply the configuration and create the EC2 instance
11+
3. What happens if you run again `terraform apply`?
12+
4. Destroy the instance you've created with Terraform
13+
14+
## Solution
15+
16+
```
17+
mkdir exercise
18+
19+
cat << EOT >> main.tf
20+
terraform {
21+
required_providers {
22+
aws = {
23+
source = "hashicorp/aws"
24+
version = "~> 4.16"
25+
}
26+
}
27+
28+
required_version = ">= 1.2.0"
29+
}
30+
31+
provider "aws" {
32+
region = "us-west-2"
33+
}
34+
35+
resource "aws_instance" "app_server" {
36+
ami = "ami-830c94e3"
37+
instance_type = "t2.micro"
38+
39+
tags = {
40+
Name = "ExampleAppServerInstance"
41+
}
42+
}
43+
EOT
44+
45+
terraform init
46+
terraform validate
47+
terraform plan
48+
49+
# You should see this line at the end: Plan: 1 to add, 0 to change, 0 to destroy
50+
51+
terraform apply -auto-approve
52+
53+
# You should see the following output:
54+
# aws_instance.app_server: Creation complete after 49s [id=i-004651a9d4427d236
55+
56+
# Running 'terraform apply' again won't change anything as
57+
# Terraform will compare actual infrastructure to your
58+
# configuration and won't find any difference. You should see the following line:
59+
# Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
60+
61+
# Remove instance
62+
terraform destroy -auto-approve
63+
64+
# Destroy complete! Resources: 1 destroyed.
65+
```

topics/terraform/exercises/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)