From bf5a52b6f9187bb0599553df590447715459c112 Mon Sep 17 00:00:00 2001 From: Reb-B Date: Fri, 20 May 2022 16:17:10 +0200 Subject: [PATCH 1/2] chore: remind participants to delete resources --- 5-parameterization/README.md | 163 +++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 76 deletions(-) diff --git a/5-parameterization/README.md b/5-parameterization/README.md index ace37db..cb71d15 100644 --- a/5-parameterization/README.md +++ b/5-parameterization/README.md @@ -5,116 +5,127 @@ The API becomes more powerful in this lab, but we want to be careful and only ro ## Implement new feature 1. Go to the file `modules/api/variables.tf` and replace it: - ``` - variable "environment" { - type = string - description = "Identifier for the environment (e.g. staging, development or prod)" - } - variable "enable_greeting_feature" { - type = bool - description = "Enable greeting feature" - default = false - } - ``` -2. Go to the file `modules/api/main.tf` and replace it: - ```tf - locals { - project_name = "hello-world" - } +``` +variable "environment" { + type = string + description = "Identifier for the environment (e.g. staging, development or prod)" +} - module "lambda_function" { - source = "terraform-aws-modules/lambda/aws" - version = "3.2.0" +variable "enable_greeting_feature" { + type = bool + description = "Enable greeting feature" + default = false +} +``` - 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_feature}" - } +2. Go to the file `modules/api/main.tf` and replace it: - publish = true - allowed_triggers = { - AllowExecutionFromAPIGateway = { - service = "apigateway" - source_arn = "${aws_apigatewayv2_api.hello_world.execution_arn}/*/*" - } - } +```tf +locals { + project_name = "hello-world" +} + +module "lambda_function" { + source = "terraform-aws-modules/lambda/aws" + version = "3.2.0" + + 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_feature}" } - resource "aws_apigatewayv2_api" "hello_world" { - name = "${local.project_name}-${var.environment}" - protocol_type = "HTTP" - target = module.lambda_function.lambda_function_arn + 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 +} +``` + 3. Go to the file `modules/api/functions/helloworld.js` and replace it: - ```js - const greetingEnabled = process.env.GREETING_ENABLED === "true"; - exports.handler = async (event) => { - let message = "Hello from Lambda! πŸ‘‹"; - const name = event.queryStringParameters?.name; +```js +const greetingEnabled = process.env.GREETING_ENABLED === "true"; - if (greetingEnabled && name) { - message = `Hello ${name}! πŸ‘‹`; - } +exports.handler = async (event) => { + let message = "Hello from Lambda! πŸ‘‹"; + const name = event.queryStringParameters?.name; - return { message }; - }; - ``` + if (greetingEnabled && name) { + message = `Hello ${name}! πŸ‘‹`; + } -We extended the *API* module by introducing a new input variable `enable_greeting_feature`. The default is set to `false`, so we can’t accidentally distribute the new feature. In the `main.tf` file, we simply pass the input variable down to the AWS Lambda function as an environment variable. Finally, in the Lambda function, we use the environment variable to flip on the new feature. + return { message }; +}; +``` + +We extended the _API_ module by introducing a new input variable `enable_greeting_feature`. The default is set to `false`, so we can’t accidentally distribute the new feature. In the `main.tf` file, we simply pass the input variable down to the AWS Lambda function as an environment variable. Finally, in the Lambda function, we use the environment variable to flip on the new feature. The new feature wouldn’t appear after deployment (feel free to try it and deploy your staging and production environment). We need to configure the new input variable explicitly. Let’s do it. ## Rollout 1. Go to the file `staging/main.tf` and replace it: - ```tf - terraform { - required_version = "~> 1.1.7" - backend "s3" { - key = "staging/terraform.tfstate" - region = "eu-west-1" - } - } +```tf +terraform { + required_version = "~> 1.1.7" - provider "aws" { + backend "s3" { + key = "staging/terraform.tfstate" region = "eu-west-1" } +} - module "website" { - source = "../modules/website" +provider "aws" { + region = "eu-west-1" +} - environment = "staging" - } +module "website" { + source = "../modules/website" - module "api" { - source = "../modules/api" + environment = "staging" +} + +module "api" { + source = "../modules/api" + + environment = "staging" + enable_greeting_feature = true +} +``` - environment = "staging" - enable_greeting_feature = true - } - ``` 2. Cd into the `staging` folder and run `terraform apply`. Confirm with `yes`. 3. Open the API URL in the browser. You should still see this message: - ```json - {"message":"Hello from Lambda! πŸ‘‹"} - ``` + +```json +{ "message": "Hello from Lambda! πŸ‘‹" } +``` + 4. Now, add the `name` query param to the URL, e.g.: - ``` - https://XXXXXXXXXX.execute-api.eu-west-1.amazonaws.com/?name=alice - ``` + +``` +https://XXXXXXXXXX.execute-api.eu-west-1.amazonaws.com/?name=alice +``` + 5. Here we go! The new feature works on staging. With input variables, we can make modules configurable for different scenarios. In this case, we only want to deploy a new feature to the staging environment, but not to production. In practice, it’s a common requirement to configure environments differently. For example, we want to configure provisioned capacities (like CPU or memory allocation), a global CDN or custom domains with SSL certificates. ## Final words -Well, that was the last lab for the Terraform workshop. We hope you enjoyed the workshop. If you want to learn more about Terraform and dive deeper, take a look at the [reading list](../README.md#πŸ“–-further-reading). +Well, that was the last lab for the Terraform workshop. We hope you enjoyed the workshop. Before you go, don't forget to remove the stack deployed in your aws account with the `terraform destroy` command. If you want to learn more about Terraform and dive deeper, take a look at the [reading list](../README.md#πŸ“–-further-reading). Cheers ✌️ From beb7d7b85fe10e05068eed537bf6497ef80fc338 Mon Sep 17 00:00:00 2001 From: Reb-B Date: Fri, 20 May 2022 20:59:59 +0200 Subject: [PATCH 2/2] chore: include more detailed clean up instructions --- 5-parameterization/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/5-parameterization/README.md b/5-parameterization/README.md index cb71d15..e2bf791 100644 --- a/5-parameterization/README.md +++ b/5-parameterization/README.md @@ -124,8 +124,16 @@ https://XXXXXXXXXX.execute-api.eu-west-1.amazonaws.com/?name=alice With input variables, we can make modules configurable for different scenarios. In this case, we only want to deploy a new feature to the staging environment, but not to production. In practice, it’s a common requirement to configure environments differently. For example, we want to configure provisioned capacities (like CPU or memory allocation), a global CDN or custom domains with SSL certificates. +## Clean up + +If you are finished with the workshop and don't plan to use the resources you deployed anymore, you need to remove them from your aws account so that you don't incur any unnecessary costs. + +1. To do this, if you are not already there, navigate to the staging folder and run the `terraform destroy` command. You then need to perform the same steps for your production environment. + +2. You also made an S3 bucket in the AWS management console to store your terraform state file. To remove this bucket, navigate to your [S3 Buckets](https://s3.console.aws.amazon.com/s3/), select the state bucket your created and delete the contents by clicking `empty`. Once the contents of the bucket are deleted, you can then delete the the bucket itself. + ## Final words -Well, that was the last lab for the Terraform workshop. We hope you enjoyed the workshop. Before you go, don't forget to remove the stack deployed in your aws account with the `terraform destroy` command. If you want to learn more about Terraform and dive deeper, take a look at the [reading list](../README.md#πŸ“–-further-reading). +Well, that was the last lab for the Terraform workshop. We hope you enjoyed the workshop. If you want to learn more about Terraform and dive deeper, take a look at the [reading list](../README.md#πŸ“–-further-reading). Cheers ✌️