diff --git a/2-simple-api/README.md b/2-simple-api/README.md index 728e761..6808daf 100644 --- a/2-simple-api/README.md +++ b/2-simple-api/README.md @@ -9,7 +9,9 @@ In the first lab, we bootstrapped Terraform and created some resources. Finally, 3. Add the following lines to the JS file: ```js exports.handler = async () => { - return { "message": process.env.RESPONSE }; + return { + message: "Hello from Lambda! 👋" + }; }; ``` 4. Now, go back to the `main.tf` file and replace it: @@ -50,11 +52,8 @@ In the first lab, we bootstrapped Terraform and created some resources. Finally, function_name = "hello-world" handler = "helloworld.handler" - runtime = "nodejs12.x" + runtime = "nodejs14.x" source_path = "./functions" - environment_variables = { - "RESPONSE" = "Hello from Lambda! 👋" - } } ``` 5. Run `terraform init`, then `terraform apply`, and confirm the changes with `yes`. @@ -109,11 +108,8 @@ So, the Lambda function is in place and we can go to the next component: The API function_name = "hello-world" handler = "helloworld.handler" - runtime = "nodejs12.x" + runtime = "nodejs14.x" source_path = "./functions" - environment_variables = { - "RESPONSE" = "Hello from Lambda! 👋" - } publish = true allowed_triggers = { diff --git a/2-simple-api/functions/helloworld.js b/2-simple-api/functions/helloworld.js index 19ac00e..155c485 100644 --- a/2-simple-api/functions/helloworld.js +++ b/2-simple-api/functions/helloworld.js @@ -1,3 +1,5 @@ exports.handler = async () => { - return { "message": process.env.RESPONSE }; + return { + message: "Hello from Lambda! 👋" + }; }; diff --git a/2-simple-api/main.tf b/2-simple-api/main.tf index 2b0c9a9..3dcd0cb 100644 --- a/2-simple-api/main.tf +++ b/2-simple-api/main.tf @@ -34,11 +34,8 @@ module "lambda_function" { function_name = "hello-world" handler = "helloworld.handler" - runtime = "nodejs12.x" + runtime = "nodejs14.x" source_path = "./functions" - environment_variables = { - "RESPONSE" = "Hello from Lambda! 👋" - } publish = true allowed_triggers = { diff --git a/3-environments/modules/api/functions/helloworld.js b/3-environments/modules/api/functions/helloworld.js index 19ac00e..155c485 100644 --- a/3-environments/modules/api/functions/helloworld.js +++ b/3-environments/modules/api/functions/helloworld.js @@ -1,3 +1,5 @@ exports.handler = async () => { - return { "message": process.env.RESPONSE }; + return { + message: "Hello from Lambda! 👋" + }; }; diff --git a/3-environments/modules/api/main.tf b/3-environments/modules/api/main.tf index 981c8b5..f867964 100644 --- a/3-environments/modules/api/main.tf +++ b/3-environments/modules/api/main.tf @@ -7,11 +7,8 @@ module "lambda_function" { function_name = "${local.project_name}-${var.environment}" handler = "helloworld.handler" - runtime = "nodejs12.x" + runtime = "nodejs14.x" source_path = "${path.module}/functions" - environment_variables = { - "RESPONSE" = var.lambda_function_response - } publish = true allowed_triggers = { diff --git a/3-environments/modules/api/variables.tf b/3-environments/modules/api/variables.tf index 8b71976..f569dc7 100644 --- a/3-environments/modules/api/variables.tf +++ b/3-environments/modules/api/variables.tf @@ -1,9 +1,3 @@ -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-remote-backend/modules/api/.terraform.lock.hcl b/4-feature-flags/modules/api/.terraform.lock.hcl similarity index 100% rename from 4-remote-backend/modules/api/.terraform.lock.hcl rename to 4-feature-flags/modules/api/.terraform.lock.hcl diff --git a/4-feature-flags/modules/api/functions/helloworld.js b/4-feature-flags/modules/api/functions/helloworld.js new file mode 100644 index 0000000..4c62882 --- /dev/null +++ b/4-feature-flags/modules/api/functions/helloworld.js @@ -0,0 +1,12 @@ +const greetingEnabled = process.env.GREETING_ENABLED === "true"; + +exports.handler = async (event) => { + let message = "Hello from Lambda! 👋"; + const name = event.queryStringParameters?.name; + + if (greetingEnabled && name) { + message = `Hello ${name}! 👋`; + } + + return { message }; +}; diff --git a/4-remote-backend/modules/api/main.tf b/4-feature-flags/modules/api/main.tf similarity index 89% rename from 4-remote-backend/modules/api/main.tf rename to 4-feature-flags/modules/api/main.tf index 981c8b5..ff043c5 100644 --- a/4-remote-backend/modules/api/main.tf +++ b/4-feature-flags/modules/api/main.tf @@ -7,10 +7,10 @@ module "lambda_function" { function_name = "${local.project_name}-${var.environment}" handler = "helloworld.handler" - runtime = "nodejs12.x" + runtime = "nodejs14.x" source_path = "${path.module}/functions" environment_variables = { - "RESPONSE" = var.lambda_function_response + GREETING_ENABLED = "${var.enable_greeting}" } publish = true diff --git a/4-remote-backend/modules/api/outputs.tf b/4-feature-flags/modules/api/outputs.tf similarity index 100% rename from 4-remote-backend/modules/api/outputs.tf rename to 4-feature-flags/modules/api/outputs.tf diff --git a/4-feature-flags/modules/api/variables.tf b/4-feature-flags/modules/api/variables.tf new file mode 100644 index 0000000..a888291 --- /dev/null +++ b/4-feature-flags/modules/api/variables.tf @@ -0,0 +1,10 @@ +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} + +variable "enable_greeting" { + type = bool + description = "Enable greeting feature" + default = false +} diff --git a/4-remote-backend/modules/website/.terraform.lock.hcl b/4-feature-flags/modules/website/.terraform.lock.hcl similarity index 100% rename from 4-remote-backend/modules/website/.terraform.lock.hcl rename to 4-feature-flags/modules/website/.terraform.lock.hcl diff --git a/4-remote-backend/modules/website/index.html b/4-feature-flags/modules/website/index.html similarity index 100% rename from 4-remote-backend/modules/website/index.html rename to 4-feature-flags/modules/website/index.html diff --git a/4-feature-flags/modules/website/main.tf b/4-feature-flags/modules/website/main.tf new file mode 100644 index 0000000..54e20e2 --- /dev/null +++ b/4-feature-flags/modules/website/main.tf @@ -0,0 +1,22 @@ +data "aws_caller_identity" "current" {} + +resource "aws_s3_bucket" "website" { + bucket = "hello-world-website-${data.aws_caller_identity.current.account_id}-${var.environment}" + force_destroy = true +} + +resource "aws_s3_object" "startpage" { + bucket = aws_s3_bucket.website.id + key = "index.html" + source = "${path.module}/index.html" + acl = "public-read" + content_type = "text/html" +} + +resource "aws_s3_bucket_website_configuration" "website" { + bucket = aws_s3_bucket.website.bucket + + index_document { + suffix = "index.html" + } +} diff --git a/4-remote-backend/modules/website/outputs.tf b/4-feature-flags/modules/website/outputs.tf similarity index 100% rename from 4-remote-backend/modules/website/outputs.tf rename to 4-feature-flags/modules/website/outputs.tf diff --git a/4-remote-backend/modules/website/variables.tf b/4-feature-flags/modules/website/variables.tf similarity index 100% rename from 4-remote-backend/modules/website/variables.tf rename to 4-feature-flags/modules/website/variables.tf diff --git a/4-remote-backend/prod/.terraform.lock.hcl b/4-feature-flags/prod/.terraform.lock.hcl similarity index 100% rename from 4-remote-backend/prod/.terraform.lock.hcl rename to 4-feature-flags/prod/.terraform.lock.hcl diff --git a/4-feature-flags/prod/main.tf b/4-feature-flags/prod/main.tf new file mode 100644 index 0000000..8896638 --- /dev/null +++ b/4-feature-flags/prod/main.tf @@ -0,0 +1,19 @@ +terraform { + required_version = "~> 1.1.7" +} + +provider "aws" { + region = "eu-west-1" +} + +module "website" { + source = "../modules/website" + + environment = "prod" +} + +module "api" { + source = "../modules/api" + + environment = "prod" +} diff --git a/4-remote-backend/prod/outputs.tf b/4-feature-flags/prod/outputs.tf similarity index 100% rename from 4-remote-backend/prod/outputs.tf rename to 4-feature-flags/prod/outputs.tf diff --git a/4-remote-backend/staging/.terraform.lock.hcl b/4-feature-flags/staging/.terraform.lock.hcl similarity index 100% rename from 4-remote-backend/staging/.terraform.lock.hcl rename to 4-feature-flags/staging/.terraform.lock.hcl diff --git a/4-feature-flags/staging/main.tf b/4-feature-flags/staging/main.tf new file mode 100644 index 0000000..ec4a8d3 --- /dev/null +++ b/4-feature-flags/staging/main.tf @@ -0,0 +1,20 @@ +terraform { + required_version = "~> 1.1.7" +} + +provider "aws" { + region = "eu-west-1" +} + +module "website" { + source = "../modules/website" + + environment = "staging" +} + +module "api" { + source = "../modules/api" + + environment = "staging" + enable_greeting = true +} diff --git a/4-remote-backend/staging/outputs.tf b/4-feature-flags/staging/outputs.tf similarity index 100% rename from 4-remote-backend/staging/outputs.tf rename to 4-feature-flags/staging/outputs.tf diff --git a/4-remote-backend/modules/api/functions/helloworld.js b/4-remote-backend/modules/api/functions/helloworld.js deleted file mode 100644 index 19ac00e..0000000 --- a/4-remote-backend/modules/api/functions/helloworld.js +++ /dev/null @@ -1,3 +0,0 @@ -exports.handler = async () => { - return { "message": process.env.RESPONSE }; -}; diff --git a/4-remote-backend/modules/api/variables.tf b/4-remote-backend/modules/api/variables.tf deleted file mode 100644 index 8b71976..0000000 --- a/4-remote-backend/modules/api/variables.tf +++ /dev/null @@ -1,10 +0,0 @@ -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-terragrunt/modules/api/.terraform.lock.hcl b/5-remote-backend/modules/api/.terraform.lock.hcl similarity index 100% rename from 5-terragrunt/modules/api/.terraform.lock.hcl rename to 5-remote-backend/modules/api/.terraform.lock.hcl 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..4c62882 --- /dev/null +++ b/5-remote-backend/modules/api/functions/helloworld.js @@ -0,0 +1,12 @@ +const greetingEnabled = process.env.GREETING_ENABLED === "true"; + +exports.handler = async (event) => { + let message = "Hello from Lambda! 👋"; + const name = event.queryStringParameters?.name; + + if (greetingEnabled && name) { + message = `Hello ${name}! 👋`; + } + + return { message }; +}; diff --git a/5-terragrunt/modules/api/main.tf b/5-remote-backend/modules/api/main.tf similarity index 89% rename from 5-terragrunt/modules/api/main.tf rename to 5-remote-backend/modules/api/main.tf index 981c8b5..ff043c5 100644 --- a/5-terragrunt/modules/api/main.tf +++ b/5-remote-backend/modules/api/main.tf @@ -7,10 +7,10 @@ module "lambda_function" { function_name = "${local.project_name}-${var.environment}" handler = "helloworld.handler" - runtime = "nodejs12.x" + runtime = "nodejs14.x" source_path = "${path.module}/functions" environment_variables = { - "RESPONSE" = var.lambda_function_response + GREETING_ENABLED = "${var.enable_greeting}" } publish = true diff --git a/5-terragrunt/modules/api/outputs.tf b/5-remote-backend/modules/api/outputs.tf similarity index 100% rename from 5-terragrunt/modules/api/outputs.tf rename to 5-remote-backend/modules/api/outputs.tf diff --git a/5-remote-backend/modules/api/variables.tf b/5-remote-backend/modules/api/variables.tf new file mode 100644 index 0000000..be7b226 --- /dev/null +++ b/5-remote-backend/modules/api/variables.tf @@ -0,0 +1,10 @@ +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} + +variable "enable_greeting" { + type = bool + description = "Enable greeting feature" + default = false +} diff --git a/5-terragrunt/modules/website/.terraform.lock.hcl b/5-remote-backend/modules/website/.terraform.lock.hcl similarity index 100% rename from 5-terragrunt/modules/website/.terraform.lock.hcl rename to 5-remote-backend/modules/website/.terraform.lock.hcl diff --git a/5-terragrunt/modules/website/index.html b/5-remote-backend/modules/website/index.html similarity index 100% rename from 5-terragrunt/modules/website/index.html rename to 5-remote-backend/modules/website/index.html diff --git a/4-remote-backend/modules/website/main.tf b/5-remote-backend/modules/website/main.tf similarity index 100% rename from 4-remote-backend/modules/website/main.tf rename to 5-remote-backend/modules/website/main.tf diff --git a/5-terragrunt/modules/website/outputs.tf b/5-remote-backend/modules/website/outputs.tf similarity index 100% rename from 5-terragrunt/modules/website/outputs.tf rename to 5-remote-backend/modules/website/outputs.tf diff --git a/5-terragrunt/modules/website/variables.tf b/5-remote-backend/modules/website/variables.tf similarity index 100% rename from 5-terragrunt/modules/website/variables.tf rename to 5-remote-backend/modules/website/variables.tf diff --git a/5-terragrunt/prod/api/.terraform.lock.hcl b/5-remote-backend/prod/.terraform.lock.hcl similarity index 100% rename from 5-terragrunt/prod/api/.terraform.lock.hcl rename to 5-remote-backend/prod/.terraform.lock.hcl diff --git a/4-remote-backend/prod/main.tf b/5-remote-backend/prod/main.tf similarity index 79% rename from 4-remote-backend/prod/main.tf rename to 5-remote-backend/prod/main.tf index 7b921ed..a0e5122 100644 --- a/4-remote-backend/prod/main.tf +++ b/5-remote-backend/prod/main.tf @@ -21,6 +21,5 @@ module "website" { module "api" { source = "../modules/api" - lambda_function_response = "Hello from Prod 👋" - environment = "prod" + environment = "prod" } diff --git a/5-remote-backend/prod/outputs.tf b/5-remote-backend/prod/outputs.tf new file mode 100644 index 0000000..17f2ca0 --- /dev/null +++ b/5-remote-backend/prod/outputs.tf @@ -0,0 +1,9 @@ +output "api_url" { + description = "Hello World API URL" + value = module.api.url +} + +output "website_url" { + description = "Static Website URL" + value = module.website.url +} diff --git a/5-terragrunt/staging/api/.terraform.lock.hcl b/5-remote-backend/staging/.terraform.lock.hcl similarity index 100% rename from 5-terragrunt/staging/api/.terraform.lock.hcl rename to 5-remote-backend/staging/.terraform.lock.hcl diff --git a/4-remote-backend/staging/main.tf b/5-remote-backend/staging/main.tf similarity index 85% rename from 4-remote-backend/staging/main.tf rename to 5-remote-backend/staging/main.tf index 48eadef..a8e48ad 100644 --- a/4-remote-backend/staging/main.tf +++ b/5-remote-backend/staging/main.tf @@ -14,13 +14,13 @@ provider "aws" { module "website" { source = "../modules/website" - + environment = "staging" } module "api" { source = "../modules/api" - - lambda_function_response = "Hello from Staging 👋" + environment = "staging" + enable_greeting = true } diff --git a/5-remote-backend/staging/outputs.tf b/5-remote-backend/staging/outputs.tf new file mode 100644 index 0000000..17f2ca0 --- /dev/null +++ b/5-remote-backend/staging/outputs.tf @@ -0,0 +1,9 @@ +output "api_url" { + description = "Hello World API URL" + value = module.api.url +} + +output "website_url" { + description = "Static Website URL" + value = module.website.url +} diff --git a/5-terragrunt/modules/api/functions/helloworld.js b/5-terragrunt/modules/api/functions/helloworld.js deleted file mode 100644 index 19ac00e..0000000 --- a/5-terragrunt/modules/api/functions/helloworld.js +++ /dev/null @@ -1,3 +0,0 @@ -exports.handler = async () => { - return { "message": process.env.RESPONSE }; -}; diff --git a/5-terragrunt/modules/api/variables.tf b/5-terragrunt/modules/api/variables.tf deleted file mode 100644 index 8b71976..0000000 --- a/5-terragrunt/modules/api/variables.tf +++ /dev/null @@ -1,10 +0,0 @@ -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/api/.terraform.lock.hcl b/6-terragrunt/modules/api/.terraform.lock.hcl new file mode 100644 index 0000000..e83f112 --- /dev/null +++ b/6-terragrunt/modules/api/.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/modules/api/functions/helloworld.js b/6-terragrunt/modules/api/functions/helloworld.js new file mode 100644 index 0000000..4c62882 --- /dev/null +++ b/6-terragrunt/modules/api/functions/helloworld.js @@ -0,0 +1,12 @@ +const greetingEnabled = process.env.GREETING_ENABLED === "true"; + +exports.handler = async (event) => { + let message = "Hello from Lambda! 👋"; + const name = event.queryStringParameters?.name; + + if (greetingEnabled && name) { + message = `Hello ${name}! 👋`; + } + + return { message }; +}; diff --git a/6-terragrunt/modules/api/main.tf b/6-terragrunt/modules/api/main.tf new file mode 100644 index 0000000..ff043c5 --- /dev/null +++ b/6-terragrunt/modules/api/main.tf @@ -0,0 +1,29 @@ +locals { + project_name = "hello-world" +} + +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + + function_name = "${local.project_name}-${var.environment}" + handler = "helloworld.handler" + runtime = "nodejs14.x" + source_path = "${path.module}/functions" + environment_variables = { + GREETING_ENABLED = "${var.enable_greeting}" + } + + publish = true + allowed_triggers = { + AllowExecutionFromAPIGateway = { + service = "apigateway" + source_arn = "${aws_apigatewayv2_api.hello_world.execution_arn}/*/*" + } + } +} + +resource "aws_apigatewayv2_api" "hello_world" { + name = "${local.project_name}-${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..92162b0 --- /dev/null +++ b/6-terragrunt/modules/api/outputs.tf @@ -0,0 +1,4 @@ +output "url" { + description = "Hello World API URL" + value = aws_apigatewayv2_api.hello_world.api_endpoint +} diff --git a/5-terragrunt/modules/api/terragrunt.hcl b/6-terragrunt/modules/api/terragrunt.hcl similarity index 100% rename from 5-terragrunt/modules/api/terragrunt.hcl rename to 6-terragrunt/modules/api/terragrunt.hcl diff --git a/6-terragrunt/modules/api/variables.tf b/6-terragrunt/modules/api/variables.tf new file mode 100644 index 0000000..66c94a7 --- /dev/null +++ b/6-terragrunt/modules/api/variables.tf @@ -0,0 +1,10 @@ +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} + +variable "enable_greeting" { + type = bool + description = "Enable greeting feature" + default = false +} diff --git a/5-terragrunt/prod/website/.terraform.lock.hcl b/6-terragrunt/modules/website/.terraform.lock.hcl similarity index 100% rename from 5-terragrunt/prod/website/.terraform.lock.hcl rename to 6-terragrunt/modules/website/.terraform.lock.hcl diff --git a/6-terragrunt/modules/website/index.html b/6-terragrunt/modules/website/index.html new file mode 100644 index 0000000..cde5102 --- /dev/null +++ b/6-terragrunt/modules/website/index.html @@ -0,0 +1,6 @@ + + +
+