Skip to content

Commit

Permalink
refa: get rid of code structure lab
Browse files Browse the repository at this point in the history
why? because we dont need a dedicated lab for it
  • Loading branch information
HenrikFricke committed May 9, 2022
1 parent 5ce16c3 commit 0a5f622
Show file tree
Hide file tree
Showing 67 changed files with 36 additions and 226 deletions.
15 changes: 11 additions & 4 deletions 1-static-hosting/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Static Hosting

Welcome to the first lab! 🥳 Let's get started by bootstrapping Terraform and deploying some resources to AWS. Instead of just deploying some random resources, we want to create a S3 bucket and enable static website hosting. Ultimately, we serve a static HTML file.
Welcome to the first lab! 🥳 Let's get started by bootstrapping Terraform and deploying some resources to AWS. Instead of just deploying some random resources, we want to create an S3 bucket and enable static website hosting. Ultimately, we serve a static HTML file.

## Bootstrap Terraform

Expand Down Expand Up @@ -38,11 +38,11 @@ Welcome to the first lab! 🥳 Let's get started by bootstrapping Terraform and

We just created an empty S3 bucket. Go to the [S3 console](https://s3.console.aws.amazon.com/s3/buckets) and verify the existence.

What's going on here? We created a simple `.tf` file: Terraform comes with its own syntax called [HCL](https://www.terraform.io/language/syntax/configuration). In the `main.tf` file, we set the required Terraform version. After that, we configure a provider. Throughout the workshop, we focus on AWS and only deploy AWS resources. The [AWS provider](https://www.terraform.io/language/providers) gives us all the resources and data sources we need to interact with AWS.
What's going on here? We created a simple `.tf` file: Terraform comes with its syntax called [HCL](https://www.terraform.io/language/syntax/configuration). In the `main.tf` file, we set the required Terraform version. After that, we configure a provider. Throughout the workshop, we focus on AWS and only deploy AWS resources. The [AWS provider](https://www.terraform.io/language/providers) gives us all the resources and data sources we need to interact with AWS.

Bare in mind, that Terraform provides [dozens of providers](https://registry.terraform.io/browse/providers) (e.g. Azure, Google Cloud Plaform or even Auth0).

Now, after the provider we have a data source and a resource. The resource block describes a simple S3 bucket. The configuration arguments (between `{` and `}`) describe the resource furthermore. S3 buckets always need an unique bucket name. To achieve this, we get the current AWS account id and append it to the bucket name. We get the AWS account id by using the `aws_caller_identity` data source. With data sources, we can fetch data outside of the Terraform stack and use it for resources.
Now, after the provider, we have a data source and a resource. The resource block describes a simple S3 bucket. The configuration arguments (between `{` and `}`) describe the resource furthermore. S3 buckets always need a unique bucket name. To achieve this, we get the current AWS account id and append it to the bucket name. We get the AWS account id by using the `aws_caller_identity` data source. With data sources, we can fetch data outside of the Terraform stack and use it for resources.

Finally, we run `terraform apply` to deploy the resources.

Expand Down Expand Up @@ -98,7 +98,14 @@ Let's extend the stack and deploy more resources:
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
}
```
3. Run `terraform apply` again and confirm with `yes`.
3. Create another file `outputs.tf` next to the `main.tf`. Add the following lines:
```tf
output "website_url" {
description = "Static website URL"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
}
```
4. Run `terraform apply` again and confirm with `yes`.

The new resources enable static website hosting and upload the `index.html`. Feel free to go to the S3 console again. You should see the `index.html` file in the bucket.

Expand Down
5 changes: 0 additions & 5 deletions 1-static-hosting/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,3 @@ resource "aws_s3_bucket_website_configuration" "website" {
suffix = "index.html"
}
}

output "website_url" {
description = "Static website URL"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
}
4 changes: 4 additions & 0 deletions 1-static-hosting/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "website_url" {
description = "Static website URL"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
}
32 changes: 10 additions & 22 deletions 2-simple-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ In the first lab, we bootstrapped Terraform and created some resources. Finally,
}
}
variable "lambda_function_response" {
type = string
description = "Body response of the hello world lambda function"
default = "Hello from Lambda!"
}
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
Expand All @@ -59,23 +53,21 @@ In the first lab, we bootstrapped Terraform and created some resources. Finally,
runtime = "nodejs12.x"
source_path = "./functions"
environment_variables = {
"RESPONSE" = var.lambda_function_response
"RESPONSE" = "Hello from Lambda! 👋"
}
}
```
5. Run `terraform init`, then `terraform apply` and confirm the changes with `yes`.
5. Run `terraform init`, then `terraform apply`, and confirm the changes with `yes`.
6. Go to the [Lambda console](https://eu-west-1.console.aws.amazon.com/lambda/home?region=eu-west-1#/functions/hello-world?tab=testing). You should see the deployed Lambda function. Now, scroll down a little bit and click on the `Test` button. Click on `Details`. You should see the output of the Lambda function:
```json
{
"message": "Hello from Lambda!"
"message": "Hello from Lambda! 👋"
}
```

It's worth noting that we use environment variables to pass certain data to the Lambda function. The Terraform [input variable](https://www.terraform.io/language/values/variables) sets the response of the Lambda function. Feel free to play around with the default value and deploy the stack again. You should retrieve the new Lambda function response.
We used a [module](https://www.terraform.io/language/modules/develop) for the first time. A *module* is a container for multiple resources. We use a third-party library called [*terraform-aws-modules/lambda/aws*](https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws/latest) to describe a Lambda function. Essentially, the library does the heavy lifting for us. It bundles the function source code and uploads it to the public cloud. We could also use plain AWS resources and create the process by ourselves, but that's not good advice. Before reinventing the wheel, we should check for open-source modules already available. The [Terraform Registry](https://registry.terraform.io/) is a good starting point to find the right library.

In addition, we also use a [module](https://www.terraform.io/language/modules/develop) for the first time. A *module* is a container for multiple resources. We use a third-party library called [*terraform-aws-modules/lambda/aws*](https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws/latest) to describe a Lambda function. Essentially, the library does the heavy lifting for us. It bundles the function source code and uploads it to the public cloud. We could also use plain AWS resources and create the process by ourselves, but that's not good advice. Before reinventing the wheel, we should check for open-source modules already available. The [Terraform Registry](https://registry.terraform.io/) is a good starting point to find the right library.

So, the Lambda function is in place and we can go to the next component. The API Gateway sitting in front of the Lambda function and processing HTTP(S) requests.
So, the Lambda function is in place and we can go to the next component: The API Gateway sitting in front of the Lambda function and processing HTTP(S) requests.

## API Gateway

Expand Down Expand Up @@ -112,12 +104,6 @@ So, the Lambda function is in place and we can go to the next component. The API
}
}
variable "lambda_function_response" {
type = string
description = "Body response of the hello world lambda function"
default = "Hello from Lambda!"
}
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
Expand All @@ -126,7 +112,7 @@ So, the Lambda function is in place and we can go to the next component. The API
runtime = "nodejs12.x"
source_path = "./functions"
environment_variables = {
"RESPONSE" = var.lambda_function_response
"RESPONSE" = "Hello from Lambda! 👋"
}
publish = true
Expand All @@ -143,7 +129,9 @@ So, the Lambda function is in place and we can go to the next component. The API
protocol_type = "HTTP"
target = module.lambda_function.lambda_function_arn
}
```
2. Replace the `outputs.tf` file:
```tf
output "website_url" {
description = "Static website URL"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
Expand All @@ -161,4 +149,4 @@ Okay, nothing really special here. We extended the stack and introduced more res

## Next

The Terraform file is already growing and growing. Let's talk about some naming conventions and code structure best practices [in the next lab](../3-code-structure/).
The [next lab](../3-environments/) covers a very important topic: Instead of just deploying the stack once, we want to deploy the stack for multiple environments. Imagine, we want to have a staging and production environment, probably with slightly different configurations.
18 changes: 1 addition & 17 deletions 2-simple-api/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ resource "aws_s3_bucket_website_configuration" "website" {
}
}

variable "lambda_function_response" {
type = string
description = "Body response of the hello world lambda function"
default = "Hello from Lambda!"
}

module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"

Expand All @@ -43,7 +37,7 @@ module "lambda_function" {
runtime = "nodejs12.x"
source_path = "./functions"
environment_variables = {
"RESPONSE" = var.lambda_function_response
"RESPONSE" = "Hello from Lambda! 👋"
}

publish = true
Expand All @@ -60,13 +54,3 @@ resource "aws_apigatewayv2_api" "hello_world" {
protocol_type = "HTTP"
target = module.lambda_function.lambda_function_arn
}

output "website_url" {
description = "Static website URL"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
}

output "api_url" {
description = "Hello World API URL"
value = aws_apigatewayv2_api.hello_world.api_endpoint
}
2 changes: 1 addition & 1 deletion 3-code-structure/outputs.tf → 2-simple-api/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
output "website_url" {
description = "Static website URL"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
value = "http://${aws_s3_bucket_website_configuration.website.website_endpoint}"
}

output "api_url" {
Expand Down
19 changes: 0 additions & 19 deletions 3-code-structure/README.md

This file was deleted.

56 changes: 0 additions & 56 deletions 3-code-structure/main.tf

This file was deleted.

5 changes: 0 additions & 5 deletions 3-code-structure/variables.tf

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions 6-terragrunt/modules/api/functions/helloworld.js

This file was deleted.

6 changes: 0 additions & 6 deletions 6-terragrunt/modules/website/index.html

This file was deleted.

82 changes: 0 additions & 82 deletions 6-terragrunt/staging/api/.terraform.lock.hcl

This file was deleted.

15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 🎓 Terraform Workshop
> Learn infrastructure as code basics w/ Terraform and AWS

The workshop addresses infrastructure as code and Terraform basics. Learn how to create the first Terraform stack and dpeloy some resources to AWS. We explore best practices and improve the growing code base over time.
The workshop addresses infrastructure as code and Terraform basics. Learn how to create the first Terraform stack and deploy some resources to AWS. We explore best practices and improve the growing codebase over time.

## ✨ At a glance

Expand All @@ -27,11 +27,14 @@ We recommend you walk through the labs step by step and follow the instructions.

1. [Static Hosting](./1-static-hosting): Get started with Terraform and deploy the first AWS resources
2. [Simple API](./2-simple-api/): Extend the codebase and create a simple HTTP API using AWS API Gateway and AWS Lambda
3. [Code Structure](./3-code-structure/): Refactor the codebase and learn more about naming conventions and commong code structures.
4. [Environments](./4-environments/): Extend the codebase and prepare everything for multiple environments (e.g. staging and prod). Learn more about modules.
5. [Remote Backend](./5-remote-backend/): Manage Terraform state remotely using AWS S3.
6. [Terragrunt](./6-terragrunt/): Use Terragrunt to automate the creation of the remote state and clean up the environments.

3. [Environments](./3-environments/): Extend the codebase and prepare everything for multiple environments (e.g. staging and prod). Learn more about modules.
4. [Remote Backend](./4-remote-backend/): Manage Terraform state remotely using AWS S3.
5. [Terragrunt](./5-terragrunt/): Use Terragrunt to automate the creation of the remote state and clean up the environments.

## 📖 Further Reading

- Best practices for [code structure](https://www.terraform-best-practices.com/code-structure) and [naming conventions](https://www.terraform-best-practices.com/naming)

## 👩‍⚖️ License

See [LICENSE](./LICENSE.md).

0 comments on commit 0a5f622

Please sign in to comment.