From 6a8ad323194ec47d16695b092becba0d2993da7e Mon Sep 17 00:00:00 2001 From: Henrik Fricke Date: Tue, 3 May 2022 18:06:42 +0200 Subject: [PATCH] feat: add more labs --- .gitignore | 10 ++- .terraform-version | 1 + .../.terraform.lock.hcl | 0 {0-bootstrap => 1-bootstrap}/README.md | 5 -- {0-bootstrap => 1-bootstrap}/main.tf | 0 .../.terraform.lock.hcl | 0 .../functions/helloworld.js | 0 2-hello-world/main.tf | 57 +++++++++++++ 3-code-structure/.terraform.lock.hcl | 82 +++++++++++++++++++ 3-code-structure/functions/helloworld.js | 3 + {1-hello-world => 3-code-structure}/main.tf | 9 -- 3-code-structure/outputs.tf | 4 + 3-code-structure/variables.tf | 5 ++ .../environments/mgmt/.terraform.lock.hcl | 21 +++++ 4-environments/environments/mgmt/main.tf | 11 +++ .../environments/prod/.terraform.lock.hcl | 82 +++++++++++++++++++ 4-environments/environments/prod/main.tf | 14 ++++ 4-environments/environments/prod/outputs.tf | 4 + .../environments/staging/.terraform.lock.hcl | 82 +++++++++++++++++++ 4-environments/environments/staging/main.tf | 14 ++++ .../environments/staging/outputs.tf | 4 + .../modules/api/functions/helloworld.js | 3 + 4-environments/modules/api/main.tf | 25 ++++++ 4-environments/modules/api/outputs.tf | 4 + 4-environments/modules/api/variables.tf | 10 +++ 4-environments/modules/mgmt/main.tf | 12 +++ .../environments/mgmt/.terraform.lock.hcl | 21 +++++ 5-remote-backend/environments/mgmt/main.tf | 17 ++++ .../environments/prod/.terraform.lock.hcl | 82 +++++++++++++++++++ 5-remote-backend/environments/prod/main.tf | 20 +++++ 5-remote-backend/environments/prod/outputs.tf | 4 + .../environments/staging/.terraform.lock.hcl | 82 +++++++++++++++++++ 5-remote-backend/environments/staging/main.tf | 20 +++++ .../environments/staging/outputs.tf | 4 + .../modules/api/functions/helloworld.js | 3 + 5-remote-backend/modules/api/main.tf | 25 ++++++ 5-remote-backend/modules/api/outputs.tf | 4 + 5-remote-backend/modules/api/variables.tf | 10 +++ 5-remote-backend/modules/mgmt/main.tf | 12 +++ .../environments/mgmt/.terraform.lock.hcl | 21 +++++ 6-terragrunt/environments/mgmt/terragrunt.hcl | 7 ++ .../environments/prod/.terraform.lock.hcl | 82 +++++++++++++++++++ 6-terragrunt/environments/prod/terragrunt.hcl | 12 +++ .../environments/staging/.terraform.lock.hcl | 82 +++++++++++++++++++ .../environments/staging/terragrunt.hcl | 12 +++ 6-terragrunt/environments/terragrunt.hcl | 26 ++++++ .../modules/api/functions/helloworld.js | 3 + 6-terragrunt/modules/api/main.tf | 25 ++++++ 6-terragrunt/modules/api/outputs.tf | 4 + 6-terragrunt/modules/api/variables.tf | 10 +++ 6-terragrunt/modules/mgmt/main.tf | 12 +++ LICENSE.md | 21 +++++ README.md | 24 ++++++ 53 files changed, 1092 insertions(+), 15 deletions(-) create mode 100644 .terraform-version rename {0-bootstrap => 1-bootstrap}/.terraform.lock.hcl (100%) rename {0-bootstrap => 1-bootstrap}/README.md (90%) rename {0-bootstrap => 1-bootstrap}/main.tf (100%) rename {1-hello-world => 2-hello-world}/.terraform.lock.hcl (100%) rename {1-hello-world => 2-hello-world}/functions/helloworld.js (100%) create mode 100644 2-hello-world/main.tf create mode 100644 3-code-structure/.terraform.lock.hcl create mode 100644 3-code-structure/functions/helloworld.js rename {1-hello-world => 3-code-structure}/main.tf (85%) create mode 100644 3-code-structure/outputs.tf create mode 100644 3-code-structure/variables.tf create mode 100644 4-environments/environments/mgmt/.terraform.lock.hcl create mode 100644 4-environments/environments/mgmt/main.tf create mode 100644 4-environments/environments/prod/.terraform.lock.hcl create mode 100644 4-environments/environments/prod/main.tf create mode 100644 4-environments/environments/prod/outputs.tf create mode 100644 4-environments/environments/staging/.terraform.lock.hcl create mode 100644 4-environments/environments/staging/main.tf create mode 100644 4-environments/environments/staging/outputs.tf create mode 100644 4-environments/modules/api/functions/helloworld.js create mode 100644 4-environments/modules/api/main.tf create mode 100644 4-environments/modules/api/outputs.tf create mode 100644 4-environments/modules/api/variables.tf create mode 100644 4-environments/modules/mgmt/main.tf create mode 100644 5-remote-backend/environments/mgmt/.terraform.lock.hcl create mode 100644 5-remote-backend/environments/mgmt/main.tf create mode 100644 5-remote-backend/environments/prod/.terraform.lock.hcl create mode 100644 5-remote-backend/environments/prod/main.tf create mode 100644 5-remote-backend/environments/prod/outputs.tf create mode 100644 5-remote-backend/environments/staging/.terraform.lock.hcl create mode 100644 5-remote-backend/environments/staging/main.tf create mode 100644 5-remote-backend/environments/staging/outputs.tf create mode 100644 5-remote-backend/modules/api/functions/helloworld.js create mode 100644 5-remote-backend/modules/api/main.tf create mode 100644 5-remote-backend/modules/api/outputs.tf create mode 100644 5-remote-backend/modules/api/variables.tf create mode 100644 5-remote-backend/modules/mgmt/main.tf create mode 100644 6-terragrunt/environments/mgmt/.terraform.lock.hcl create mode 100644 6-terragrunt/environments/mgmt/terragrunt.hcl create mode 100644 6-terragrunt/environments/prod/.terraform.lock.hcl create mode 100644 6-terragrunt/environments/prod/terragrunt.hcl create mode 100644 6-terragrunt/environments/staging/.terraform.lock.hcl create mode 100644 6-terragrunt/environments/staging/terragrunt.hcl create mode 100644 6-terragrunt/environments/terragrunt.hcl create mode 100644 6-terragrunt/modules/api/functions/helloworld.js create mode 100644 6-terragrunt/modules/api/main.tf create mode 100644 6-terragrunt/modules/api/outputs.tf create mode 100644 6-terragrunt/modules/api/variables.tf create mode 100644 6-terragrunt/modules/mgmt/main.tf create mode 100644 LICENSE.md create mode 100644 README.md diff --git a/.gitignore b/.gitignore index ead3688..6f00c00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,13 @@ +# Common +.DS_Store + +# Terraform .terraform *.tfstate* terraform.tfvars + +# Lambda Function Builds builds -.DS_Store + +# Terragrunt (auto-generated) +.terragrunt-cache diff --git a/.terraform-version b/.terraform-version new file mode 100644 index 0000000..512a1fa --- /dev/null +++ b/.terraform-version @@ -0,0 +1 @@ +1.1.9 diff --git a/0-bootstrap/.terraform.lock.hcl b/1-bootstrap/.terraform.lock.hcl similarity index 100% rename from 0-bootstrap/.terraform.lock.hcl rename to 1-bootstrap/.terraform.lock.hcl diff --git a/0-bootstrap/README.md b/1-bootstrap/README.md similarity index 90% rename from 0-bootstrap/README.md rename to 1-bootstrap/README.md index ca35904..d074ce4 100644 --- a/0-bootstrap/README.md +++ b/1-bootstrap/README.md @@ -1,10 +1,5 @@ # Bootstrap -## Prerequisites - -- [Terraform CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli) -- [AWS credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) - ## Goals - Set up Terraform - Create first resource and deploy it diff --git a/0-bootstrap/main.tf b/1-bootstrap/main.tf similarity index 100% rename from 0-bootstrap/main.tf rename to 1-bootstrap/main.tf diff --git a/1-hello-world/.terraform.lock.hcl b/2-hello-world/.terraform.lock.hcl similarity index 100% rename from 1-hello-world/.terraform.lock.hcl rename to 2-hello-world/.terraform.lock.hcl diff --git a/1-hello-world/functions/helloworld.js b/2-hello-world/functions/helloworld.js similarity index 100% rename from 1-hello-world/functions/helloworld.js rename to 2-hello-world/functions/helloworld.js diff --git a/2-hello-world/main.tf b/2-hello-world/main.tf new file mode 100644 index 0000000..8e21144 --- /dev/null +++ b/2-hello-world/main.tf @@ -0,0 +1,57 @@ +terraform { + required_version = "~> 1.1.7" +} + +provider "aws" { + region = "eu-west-1" +} + +data "aws_iam_policy" "admin" { + name = "AdministratorAccess" +} + +resource "aws_iam_user" "ci" { + name = "CI" +} + +resource "aws_iam_user_policy_attachment" "ci" { + user = aws_iam_user.ci.name + policy_arn = data.aws_iam_policy.admin.arn +} + +variable "lambda_function_response" { + type = string + description = "Body response of the hello world lambda function" + default = "Hello World :)" +} + +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + + function_name = "hello-world" + handler = "helloworld.handler" + runtime = "nodejs12.x" + source_path = "./functions" + environment_variables = { + "RESPONSE" = var.lambda_function_response + } + + publish = true + allowed_triggers = { + AllowExecutionFromAPIGateway = { + service = "apigateway" + source_arn = "${aws_apigatewayv2_api.hello_world.execution_arn}/*/*" + } + } +} + +resource "aws_apigatewayv2_api" "hello_world" { + name = "hello-world" + protocol_type = "HTTP" + target = module.lambda_function.lambda_function_arn +} + +output "endpoint" { + description = "Hello World API URL" + value = aws_apigatewayv2_api.hello_world.api_endpoint +} diff --git a/3-code-structure/.terraform.lock.hcl b/3-code-structure/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/3-code-structure/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/3-code-structure/functions/helloworld.js b/3-code-structure/functions/helloworld.js new file mode 100644 index 0000000..3c6878a --- /dev/null +++ b/3-code-structure/functions/helloworld.js @@ -0,0 +1,3 @@ +exports.handler = async () => { + return process.env.RESPONSE; +}; diff --git a/1-hello-world/main.tf b/3-code-structure/main.tf similarity index 85% rename from 1-hello-world/main.tf rename to 3-code-structure/main.tf index 1cc8dad..e94c3af 100644 --- a/1-hello-world/main.tf +++ b/3-code-structure/main.tf @@ -19,11 +19,6 @@ resource "aws_iam_user_policy_attachment" "ci" { policy_arn = data.aws_iam_policy.admin.arn } -variable "lambda_function_response" { - type = string - default = "Hello World :)" -} - module "lambda_function" { source = "terraform-aws-modules/lambda/aws" @@ -49,7 +44,3 @@ resource "aws_apigatewayv2_api" "hello_world" { protocol_type = "HTTP" target = module.lambda_function.lambda_function_arn } - -output "endpoint" { - value = aws_apigatewayv2_api.hello_world.api_endpoint -} diff --git a/3-code-structure/outputs.tf b/3-code-structure/outputs.tf new file mode 100644 index 0000000..fab854f --- /dev/null +++ b/3-code-structure/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = aws_apigatewayv2_api.hello_world.api_endpoint +} diff --git a/3-code-structure/variables.tf b/3-code-structure/variables.tf new file mode 100644 index 0000000..635ea47 --- /dev/null +++ b/3-code-structure/variables.tf @@ -0,0 +1,5 @@ +variable "lambda_function_response" { + type = string + description = "Body response of the hello world lambda function" + default = "Hello World :)" +} diff --git a/4-environments/environments/mgmt/.terraform.lock.hcl b/4-environments/environments/mgmt/.terraform.lock.hcl new file mode 100644 index 0000000..1489342 --- /dev/null +++ b/4-environments/environments/mgmt/.terraform.lock.hcl @@ -0,0 +1,21 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} diff --git a/4-environments/environments/mgmt/main.tf b/4-environments/environments/mgmt/main.tf new file mode 100644 index 0000000..52c356a --- /dev/null +++ b/4-environments/environments/mgmt/main.tf @@ -0,0 +1,11 @@ +terraform { + required_version = "~> 1.1.7" +} + +provider "aws" { + region = "eu-west-1" +} + +module "mgmt" { + source = "../../modules/mgmt" +} diff --git a/4-environments/environments/prod/.terraform.lock.hcl b/4-environments/environments/prod/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/4-environments/environments/prod/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/4-environments/environments/prod/main.tf b/4-environments/environments/prod/main.tf new file mode 100644 index 0000000..d7622f9 --- /dev/null +++ b/4-environments/environments/prod/main.tf @@ -0,0 +1,14 @@ +terraform { + required_version = "~> 1.1.7" +} + +provider "aws" { + region = "eu-west-1" +} + +module "api" { + source = "../../modules/api" + + lambda_function_response = "Hello from Prod 👋" + environment = "prod" +} diff --git a/4-environments/environments/prod/outputs.tf b/4-environments/environments/prod/outputs.tf new file mode 100644 index 0000000..d3db721 --- /dev/null +++ b/4-environments/environments/prod/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = module.api.endpoint +} diff --git a/4-environments/environments/staging/.terraform.lock.hcl b/4-environments/environments/staging/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/4-environments/environments/staging/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/4-environments/environments/staging/main.tf b/4-environments/environments/staging/main.tf new file mode 100644 index 0000000..e851908 --- /dev/null +++ b/4-environments/environments/staging/main.tf @@ -0,0 +1,14 @@ +terraform { + required_version = "~> 1.1.7" +} + +provider "aws" { + region = "eu-west-1" +} + +module "api" { + source = "../../modules/api" + + lambda_function_response = "Hello from Staging 👋" + environment = "staging" +} diff --git a/4-environments/environments/staging/outputs.tf b/4-environments/environments/staging/outputs.tf new file mode 100644 index 0000000..d3db721 --- /dev/null +++ b/4-environments/environments/staging/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = module.api.endpoint +} diff --git a/4-environments/modules/api/functions/helloworld.js b/4-environments/modules/api/functions/helloworld.js new file mode 100644 index 0000000..3c6878a --- /dev/null +++ b/4-environments/modules/api/functions/helloworld.js @@ -0,0 +1,3 @@ +exports.handler = async () => { + return process.env.RESPONSE; +}; diff --git a/4-environments/modules/api/main.tf b/4-environments/modules/api/main.tf new file mode 100644 index 0000000..a5fef67 --- /dev/null +++ b/4-environments/modules/api/main.tf @@ -0,0 +1,25 @@ +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + + function_name = "hello-world-${var.environment}" + handler = "helloworld.handler" + runtime = "nodejs12.x" + source_path = "${path.module}/functions" + environment_variables = { + "RESPONSE" = var.lambda_function_response + } + + publish = true + allowed_triggers = { + AllowExecutionFromAPIGateway = { + service = "apigateway" + source_arn = "${aws_apigatewayv2_api.hello_world.execution_arn}/*/*" + } + } +} + +resource "aws_apigatewayv2_api" "hello_world" { + name = "hello-world-${var.environment}" + protocol_type = "HTTP" + target = module.lambda_function.lambda_function_arn +} diff --git a/4-environments/modules/api/outputs.tf b/4-environments/modules/api/outputs.tf new file mode 100644 index 0000000..fab854f --- /dev/null +++ b/4-environments/modules/api/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = aws_apigatewayv2_api.hello_world.api_endpoint +} diff --git a/4-environments/modules/api/variables.tf b/4-environments/modules/api/variables.tf new file mode 100644 index 0000000..8b71976 --- /dev/null +++ b/4-environments/modules/api/variables.tf @@ -0,0 +1,10 @@ +variable "lambda_function_response" { + type = string + description = "Body response of the hello world lambda function" + default = "Hello World :)" +} + +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} diff --git a/4-environments/modules/mgmt/main.tf b/4-environments/modules/mgmt/main.tf new file mode 100644 index 0000000..2577275 --- /dev/null +++ b/4-environments/modules/mgmt/main.tf @@ -0,0 +1,12 @@ +data "aws_iam_policy" "admin" { + name = "AdministratorAccess" +} + +resource "aws_iam_user" "ci" { + name = "CI" +} + +resource "aws_iam_user_policy_attachment" "ci" { + user = aws_iam_user.ci.name + policy_arn = data.aws_iam_policy.admin.arn +} diff --git a/5-remote-backend/environments/mgmt/.terraform.lock.hcl b/5-remote-backend/environments/mgmt/.terraform.lock.hcl new file mode 100644 index 0000000..1489342 --- /dev/null +++ b/5-remote-backend/environments/mgmt/.terraform.lock.hcl @@ -0,0 +1,21 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} diff --git a/5-remote-backend/environments/mgmt/main.tf b/5-remote-backend/environments/mgmt/main.tf new file mode 100644 index 0000000..c898cc9 --- /dev/null +++ b/5-remote-backend/environments/mgmt/main.tf @@ -0,0 +1,17 @@ +terraform { + required_version = "~> 1.1.7" + + backend "s3" { + bucket = "terraform-state-1651593315119" + key = "mgmt/terraform.tfstate" + region = "eu-west-1" + } +} + +provider "aws" { + region = "eu-west-1" +} + +module "mgmt" { + source = "../../modules/mgmt" +} diff --git a/5-remote-backend/environments/prod/.terraform.lock.hcl b/5-remote-backend/environments/prod/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/5-remote-backend/environments/prod/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/5-remote-backend/environments/prod/main.tf b/5-remote-backend/environments/prod/main.tf new file mode 100644 index 0000000..d2df7a1 --- /dev/null +++ b/5-remote-backend/environments/prod/main.tf @@ -0,0 +1,20 @@ +terraform { + required_version = "~> 1.1.7" + + backend "s3" { + bucket = "terraform-state-1651593315119" + key = "prod/terraform.tfstate" + region = "eu-west-1" + } +} + +provider "aws" { + region = "eu-west-1" +} + +module "api" { + source = "../../modules/api" + + lambda_function_response = "Hello from Prod 👋" + environment = "prod" +} diff --git a/5-remote-backend/environments/prod/outputs.tf b/5-remote-backend/environments/prod/outputs.tf new file mode 100644 index 0000000..d3db721 --- /dev/null +++ b/5-remote-backend/environments/prod/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = module.api.endpoint +} diff --git a/5-remote-backend/environments/staging/.terraform.lock.hcl b/5-remote-backend/environments/staging/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/5-remote-backend/environments/staging/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/5-remote-backend/environments/staging/main.tf b/5-remote-backend/environments/staging/main.tf new file mode 100644 index 0000000..ddd9993 --- /dev/null +++ b/5-remote-backend/environments/staging/main.tf @@ -0,0 +1,20 @@ +terraform { + required_version = "~> 1.1.7" + + backend "s3" { + bucket = "terraform-state-1651593315119" + key = "staging/terraform.tfstate" + region = "eu-west-1" + } +} + +provider "aws" { + region = "eu-west-1" +} + +module "api" { + source = "../../modules/api" + + lambda_function_response = "Hello from Staging 👋" + environment = "staging" +} diff --git a/5-remote-backend/environments/staging/outputs.tf b/5-remote-backend/environments/staging/outputs.tf new file mode 100644 index 0000000..d3db721 --- /dev/null +++ b/5-remote-backend/environments/staging/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = module.api.endpoint +} diff --git a/5-remote-backend/modules/api/functions/helloworld.js b/5-remote-backend/modules/api/functions/helloworld.js new file mode 100644 index 0000000..3c6878a --- /dev/null +++ b/5-remote-backend/modules/api/functions/helloworld.js @@ -0,0 +1,3 @@ +exports.handler = async () => { + return process.env.RESPONSE; +}; diff --git a/5-remote-backend/modules/api/main.tf b/5-remote-backend/modules/api/main.tf new file mode 100644 index 0000000..a5fef67 --- /dev/null +++ b/5-remote-backend/modules/api/main.tf @@ -0,0 +1,25 @@ +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + + function_name = "hello-world-${var.environment}" + handler = "helloworld.handler" + runtime = "nodejs12.x" + source_path = "${path.module}/functions" + environment_variables = { + "RESPONSE" = var.lambda_function_response + } + + publish = true + allowed_triggers = { + AllowExecutionFromAPIGateway = { + service = "apigateway" + source_arn = "${aws_apigatewayv2_api.hello_world.execution_arn}/*/*" + } + } +} + +resource "aws_apigatewayv2_api" "hello_world" { + name = "hello-world-${var.environment}" + protocol_type = "HTTP" + target = module.lambda_function.lambda_function_arn +} diff --git a/5-remote-backend/modules/api/outputs.tf b/5-remote-backend/modules/api/outputs.tf new file mode 100644 index 0000000..fab854f --- /dev/null +++ b/5-remote-backend/modules/api/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = aws_apigatewayv2_api.hello_world.api_endpoint +} diff --git a/5-remote-backend/modules/api/variables.tf b/5-remote-backend/modules/api/variables.tf new file mode 100644 index 0000000..8b71976 --- /dev/null +++ b/5-remote-backend/modules/api/variables.tf @@ -0,0 +1,10 @@ +variable "lambda_function_response" { + type = string + description = "Body response of the hello world lambda function" + default = "Hello World :)" +} + +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} diff --git a/5-remote-backend/modules/mgmt/main.tf b/5-remote-backend/modules/mgmt/main.tf new file mode 100644 index 0000000..2577275 --- /dev/null +++ b/5-remote-backend/modules/mgmt/main.tf @@ -0,0 +1,12 @@ +data "aws_iam_policy" "admin" { + name = "AdministratorAccess" +} + +resource "aws_iam_user" "ci" { + name = "CI" +} + +resource "aws_iam_user_policy_attachment" "ci" { + user = aws_iam_user.ci.name + policy_arn = data.aws_iam_policy.admin.arn +} diff --git a/6-terragrunt/environments/mgmt/.terraform.lock.hcl b/6-terragrunt/environments/mgmt/.terraform.lock.hcl new file mode 100644 index 0000000..1489342 --- /dev/null +++ b/6-terragrunt/environments/mgmt/.terraform.lock.hcl @@ -0,0 +1,21 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} diff --git a/6-terragrunt/environments/mgmt/terragrunt.hcl b/6-terragrunt/environments/mgmt/terragrunt.hcl new file mode 100644 index 0000000..847a01f --- /dev/null +++ b/6-terragrunt/environments/mgmt/terragrunt.hcl @@ -0,0 +1,7 @@ +include "root" { + path = find_in_parent_folders() +} + +terraform { + source = "../..//modules/mgmt" +} diff --git a/6-terragrunt/environments/prod/.terraform.lock.hcl b/6-terragrunt/environments/prod/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/6-terragrunt/environments/prod/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/6-terragrunt/environments/prod/terragrunt.hcl b/6-terragrunt/environments/prod/terragrunt.hcl new file mode 100644 index 0000000..5c5936c --- /dev/null +++ b/6-terragrunt/environments/prod/terragrunt.hcl @@ -0,0 +1,12 @@ +include "root" { + path = find_in_parent_folders() +} + +terraform { + source = "../..//modules/api" +} + +inputs = { + lambda_function_response = "Hello from Prod 👋" + environment = "prod" +} diff --git a/6-terragrunt/environments/staging/.terraform.lock.hcl b/6-terragrunt/environments/staging/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/6-terragrunt/environments/staging/.terraform.lock.hcl @@ -0,0 +1,82 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "4.12.1" + constraints = ">= 4.9.0" + hashes = [ + "h1:o9VATFhsl7QFfQQ6M0zL5VIZlq+8xHooKGpv/11DK9w=", + "zh:2b432dc3bf7e0987bf9dcad5d397c384890d12fcd95827bc4581ca2955fc623a", + "zh:2f79a448a4e5ad24a706ab634078d0ef159be3278eb24988b7d2185173f5dd8f", + "zh:5d70074c10cefb30d4104af54f912e58ffa1b6871277b0a5324c8f13000f5009", + "zh:63623743fb15d54787a96c9761b97a935ff396672e625730cb7a5c1971acf4b6", + "zh:8263f376e6db684667c10e28df8d8d188e02fd09ad58e1ad7075e363c389e24c", + "zh:8b5aa9fd1ddf1de0ab7d462891123405e5af04d7e4d1e4b03381634b3cae4884", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d00b2d0b374ab92e934eb597668c5f3e415c4cf8335e6a52ab99949b8fcf57dd", + "zh:d0e037725aced6cacc2e0a1903b31083c64f8765fb1263e4f8f891745266b7fb", + "zh:e6e244123bc1df109db90bef0af2a875a0b3afb268f21c3e5bc34753657102ad", + "zh:ec6901ab8b99ae3df50340e9aa86ed3bac1369f5e1403c0362edd9944640fa22", + "zh:f6a4d0ce3bd3d4b81163c4ae75b66e50c10b935c60a63d7fb96df285c0eeca40", + ] +} + +provider "registry.terraform.io/hashicorp/external" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:VUkgcWvCliS0HO4kt7oEQhFD2gcx/59XpwMqxfCU1kE=", + "zh:0b84ab0af2e28606e9c0c1289343949339221c3ab126616b831ddb5aaef5f5ca", + "zh:10cf5c9b9524ca2e4302bf02368dc6aac29fb50aeaa6f7758cce9aa36ae87a28", + "zh:56a016ee871c8501acb3f2ee3b51592ad7c3871a1757b098838349b17762ba6b", + "zh:719d6ef39c50e4cffc67aa67d74d195adaf42afcf62beab132dafdb500347d39", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7fbfc4d37435ac2f717b0316f872f558f608596b389b895fcb549f118462d327", + "zh:8ac71408204db606ce63fe8f9aeaf1ddc7751d57d586ec421e62d440c402e955", + "zh:a4cacdb06f114454b6ed0033add28006afa3f65a0ea7a43befe45fc82e6809fb", + "zh:bb5ce3132b52ae32b6cc005bc9f7627b95259b9ffe556de4dad60d47d47f21f0", + "zh:bb60d2976f125ffd232a7ccb4b3f81e7109578b23c9c6179f13a11d125dca82a", + "zh:f9540ecd2e056d6e71b9ea5f5a5cf8f63dd5c25394b9db831083a9d4ea99b372", + "zh:ffd998b55b8a64d4335a090b6956b4bf8855b290f7554dd38db3302de9c41809", + ] +} + +provider "registry.terraform.io/hashicorp/local" { + version = "2.2.2" + constraints = ">= 1.0.0" + hashes = [ + "h1:SjDyZXIUHEQzZe10VjhlhZq2a9kgQB6tmqJcpq2BeWg=", + "zh:027e4873c69da214e2fed131666d5de92089732a11d096b68257da54d30b6f9d", + "zh:0ba2216e16cfb72538d76a4c4945b4567a76f7edbfef926b1c5a08d7bba2a043", + "zh:1fee8f6aae1833c27caa96e156cf99a681b6f085e476d7e1b77d285e21d182c1", + "zh:2e8a3e72e877003df1c390a231e0d8e827eba9f788606e643f8e061218750360", + "zh:719008f9e262aa1523a6f9132adbe9eee93c648c2981f8359ce41a40e6425433", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9a70fdbe6ef955c4919a4519caca116f34c19c7ddedd77990fbe4f80fe66dc84", + "zh:abc412423d670cbb6264827fa80e1ffdc4a74aff3f19ba6a239dd87b85b15bec", + "zh:ae953a62c94d2a2a0822e5717fafc54e454af57bd6ed02cd301b9786765c1dd3", + "zh:be0910bdf46698560f9e86f51a4ff795c62c02f8dc82b2b1dab77a0b3a93f61e", + "zh:e58f9083b7971919b95f553227adaa7abe864fce976f0166cf4d65fc17257ff2", + "zh:ff4f77cbdbb22cc98182821c7ef84dce16298ab0e997d5c7fae97247f7a4bcb0", + ] +} + +provider "registry.terraform.io/hashicorp/null" { + version = "3.1.1" + constraints = ">= 2.0.0" + hashes = [ + "h1:Pctug/s/2Hg5FJqjYcTM0kPyx3AoYK1MpRWO0T9V2ns=", + "zh:063466f41f1d9fd0dd93722840c1314f046d8760b1812fa67c34de0afcba5597", + "zh:08c058e367de6debdad35fc24d97131c7cf75103baec8279aba3506a08b53faf", + "zh:73ce6dff935150d6ddc6ac4a10071e02647d10175c173cfe5dca81f3d13d8afe", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:8fdd792a626413502e68c195f2097352bdc6a0df694f7df350ed784741eb587e", + "zh:976bbaf268cb497400fd5b3c774d218f3933271864345f18deebe4dcbfcd6afa", + "zh:b21b78ca581f98f4cdb7a366b03ae9db23a73dfa7df12c533d7c19b68e9e72e5", + "zh:b7fc0c1615dbdb1d6fd4abb9c7dc7da286631f7ca2299fb9cd4664258ccfbff4", + "zh:d1efc942b2c44345e0c29bc976594cb7278c38cfb8897b344669eafbc3cddf46", + "zh:e356c245b3cd9d4789bab010893566acace682d7db877e52d40fc4ca34a50924", + "zh:ea98802ba92fcfa8cf12cbce2e9e7ebe999afbf8ed47fa45fc847a098d89468b", + "zh:eff8872458806499889f6927b5d954560f3d74bf20b6043409edf94d26cd906f", + ] +} diff --git a/6-terragrunt/environments/staging/terragrunt.hcl b/6-terragrunt/environments/staging/terragrunt.hcl new file mode 100644 index 0000000..bbccdb2 --- /dev/null +++ b/6-terragrunt/environments/staging/terragrunt.hcl @@ -0,0 +1,12 @@ +include "root" { + path = find_in_parent_folders() +} + +terraform { + source = "../..//modules/api" +} + +inputs = { + lambda_function_response = "Hello from Staging 👋" + environment = "staging" +} diff --git a/6-terragrunt/environments/terragrunt.hcl b/6-terragrunt/environments/terragrunt.hcl new file mode 100644 index 0000000..99c62b6 --- /dev/null +++ b/6-terragrunt/environments/terragrunt.hcl @@ -0,0 +1,26 @@ +remote_state { + backend = "s3" + generate = { + path = "backend.tf" + if_exists = "overwrite_terragrunt" + } + config = { + bucket = "superluminar-terraform-workshop-${get_aws_account_id()}-${path_relative_to_include()}-state" + + key = "terraform.tfstate" + region = "eu-west-1" + encrypt = true + dynamodb_table = "superluminar-terraform-workshop-${get_aws_account_id()}-${path_relative_to_include()}-lock-table" + } +} + +generate "provider" { + path = "provider.tf" + if_exists = "overwrite_terragrunt" + contents = < { + return process.env.RESPONSE; +}; diff --git a/6-terragrunt/modules/api/main.tf b/6-terragrunt/modules/api/main.tf new file mode 100644 index 0000000..a5fef67 --- /dev/null +++ b/6-terragrunt/modules/api/main.tf @@ -0,0 +1,25 @@ +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + + function_name = "hello-world-${var.environment}" + handler = "helloworld.handler" + runtime = "nodejs12.x" + source_path = "${path.module}/functions" + environment_variables = { + "RESPONSE" = var.lambda_function_response + } + + publish = true + allowed_triggers = { + AllowExecutionFromAPIGateway = { + service = "apigateway" + source_arn = "${aws_apigatewayv2_api.hello_world.execution_arn}/*/*" + } + } +} + +resource "aws_apigatewayv2_api" "hello_world" { + name = "hello-world-${var.environment}" + protocol_type = "HTTP" + target = module.lambda_function.lambda_function_arn +} diff --git a/6-terragrunt/modules/api/outputs.tf b/6-terragrunt/modules/api/outputs.tf new file mode 100644 index 0000000..fab854f --- /dev/null +++ b/6-terragrunt/modules/api/outputs.tf @@ -0,0 +1,4 @@ +output "endpoint" { + description = "Hello World API URL" + value = aws_apigatewayv2_api.hello_world.api_endpoint +} diff --git a/6-terragrunt/modules/api/variables.tf b/6-terragrunt/modules/api/variables.tf new file mode 100644 index 0000000..8b71976 --- /dev/null +++ b/6-terragrunt/modules/api/variables.tf @@ -0,0 +1,10 @@ +variable "lambda_function_response" { + type = string + description = "Body response of the hello world lambda function" + default = "Hello World :)" +} + +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} diff --git a/6-terragrunt/modules/mgmt/main.tf b/6-terragrunt/modules/mgmt/main.tf new file mode 100644 index 0000000..2577275 --- /dev/null +++ b/6-terragrunt/modules/mgmt/main.tf @@ -0,0 +1,12 @@ +data "aws_iam_policy" "admin" { + name = "AdministratorAccess" +} + +resource "aws_iam_user" "ci" { + name = "CI" +} + +resource "aws_iam_user_policy_attachment" "ci" { + user = aws_iam_user.ci.name + policy_arn = data.aws_iam_policy.admin.arn +} diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..a3eb808 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 superluminar GmbH + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9d53529 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Terraform Workshop +> Learn infrastructure as code basics w/ Terraform + +## Prerequisites + +Make sure you have the following tools installed on your computer: + +- [Terraform CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli) +- [Terragrunt](https://terragrunt.gruntwork.io/docs/getting-started/install/) +- [AWS credentials in the terminal](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) +- IDE with Terraform support (e.g. [VS Code](https://code.visualstudio.com/) / [Terraform extension](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform)) + +## Labs + +1. [Bootstrap](./1-bootstrap): Get started with Terraform and deploy your first AWS resource. Learn more about data sources along the way. +2. [Hello World](./2-hello-world/): Extend the codebase and create a simple HTTP API w/ 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-backend/): Manage Terraform state remotely using AWS S3. +6. [Terragrunt](./5-backend/): Use Terragrunt to automate the creation of the remote state and clean up the environments. + +## License + +See [LICENSE](./LICENSE.md).