From 393218f54738faf236d4d0b99be97431d34e26f0 Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Mon, 21 Nov 2022 12:23:11 +0800 Subject: [PATCH 1/4] Refactor build logic to not leak into lambda deploy tf module --- testing/benchmarking/main.tf | 8 ++++++-- testing/tf-modules/lambda_deployment/main.tf | 14 ++++---------- testing/tf-modules/lambda_deployment/variables.tf | 13 +++++++++---- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/testing/benchmarking/main.tf b/testing/benchmarking/main.tf index eb9949f8..659e88f4 100644 --- a/testing/benchmarking/main.tf +++ b/testing/benchmarking/main.tf @@ -17,7 +17,10 @@ terraform { } locals { - load_req_path = "/test" + load_req_path = "/test" + name_from_runtime = replace(var.lambda_runtime, ".", "_") + lambda_function_zip = "../build/${local.name_from_runtime}.zip" + lambda_function_name = "${var.resource_prefix}_${local.name_from_runtime}_apm_aws_lambda" runtimeToHandler = { "python3.8" = "main.handler" "go1.x" = "main" @@ -53,10 +56,11 @@ module "lambda_deployment" { resource_prefix = var.resource_prefix - build_dir = "../build" apm_aws_extension_path = "../../bin/extension.zip" lambda_runtime = var.lambda_runtime + lambda_function_zip = local.lambda_function_zip + lambda_function_name = local.lambda_function_name lambda_handler = local.runtimeToHandler[var.lambda_runtime] lambda_invoke_path = local.load_req_path lambda_memory_size = var.lambda_memory_size diff --git a/testing/tf-modules/lambda_deployment/main.tf b/testing/tf-modules/lambda_deployment/main.tf index 415ac7f2..2fb875d4 100644 --- a/testing/tf-modules/lambda_deployment/main.tf +++ b/testing/tf-modules/lambda_deployment/main.tf @@ -1,9 +1,3 @@ -locals { - name_from_runtime = replace(var.lambda_runtime, ".", "_") - lambda_function_path = "${var.build_dir}/${local.name_from_runtime}.zip" - lambda_function_name = "${var.resource_prefix}_${local.name_from_runtime}_apm_aws_lambda" -} - resource "aws_iam_role" "iam_for_lambda" { name = "${var.resource_prefix}_apm_aws_lambda_iam" @@ -32,12 +26,12 @@ resource "aws_lambda_layer_version" "extn_layer" { # TODO: @lahsivjar Add in cloudwatch integration for visualizing logs resource "aws_lambda_function" "test_fn" { - filename = local.lambda_function_path - function_name = local.lambda_function_name + filename = var.lambda_function_zip + function_name = var.lambda_function_name role = aws_iam_role.iam_for_lambda.arn handler = var.lambda_handler runtime = var.lambda_runtime - source_code_hash = filebase64sha256(local.lambda_function_path) + source_code_hash = filebase64sha256(var.lambda_function_zip) layers = [var.custom_lambda_extension_arn == "" ? aws_lambda_layer_version.extn_layer[0].arn : var.custom_lambda_extension_arn] timeout = var.lambda_timeout memory_size = var.lambda_memory_size @@ -52,7 +46,7 @@ resource "aws_lambda_function" "test_fn" { } resource "aws_apigatewayv2_api" "trigger" { - name = local.lambda_function_name + name = var.lambda_function_name protocol_type = "HTTP" description = "API Gateway to trigger lambda for testing apm-aws-lambda" } diff --git a/testing/tf-modules/lambda_deployment/variables.tf b/testing/tf-modules/lambda_deployment/variables.tf index c7248dd6..33d68a2c 100644 --- a/testing/tf-modules/lambda_deployment/variables.tf +++ b/testing/tf-modules/lambda_deployment/variables.tf @@ -3,14 +3,19 @@ variable "resource_prefix" { description = "Prefix to add to all created resource" } -variable "build_dir" { +variable "apm_aws_extension_path" { type = string - description = "Prefix to add to all created resource" + description = "Path to the zip file containing extension code" } -variable "apm_aws_extension_path" { +variable "lambda_function_zip" { type = string - description = "Path to the zip file containing extension code" + description = "Path to the zip package containing the lambda function to deploy" +} + +variable "lambda_function_name" { + type = string + description = "The name of the lambda function" } variable "lambda_runtime" { From 4e25151aa33e606d306a69cfc5b2cb1dfc4306b6 Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Mon, 21 Nov 2022 14:08:31 +0800 Subject: [PATCH 2/4] Add cloudwatch logging to lambda deployment tf --- testing/tf-modules/lambda_deployment/main.tf | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/testing/tf-modules/lambda_deployment/main.tf b/testing/tf-modules/lambda_deployment/main.tf index 2fb875d4..2e4780dd 100644 --- a/testing/tf-modules/lambda_deployment/main.tf +++ b/testing/tf-modules/lambda_deployment/main.tf @@ -24,7 +24,16 @@ resource "aws_lambda_layer_version" "extn_layer" { layer_name = "${var.resource_prefix}_apm_aws_lambda_extn" } -# TODO: @lahsivjar Add in cloudwatch integration for visualizing logs +resource "aws_iam_role_policy_attachment" "cw" { + role = aws_iam_role.iam_for_lambda.name + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" +} + +resource "aws_cloudwatch_log_group" "cw_log_group" { + name = "/aws/lambda/${var.lambda_function_name}" + retention_in_days = 1 +} + resource "aws_lambda_function" "test_fn" { filename = var.lambda_function_zip function_name = var.lambda_function_name @@ -36,6 +45,10 @@ resource "aws_lambda_function" "test_fn" { timeout = var.lambda_timeout memory_size = var.lambda_memory_size + depends_on = [ + aws_cloudwatch_log_group.cw_log_group, + ] + environment { variables = { ELASTIC_APM_LAMBDA_APM_SERVER = var.apm_server_url From 0605098cc28badb3fd1aa8f7279fc49987b72730 Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Tue, 22 Nov 2022 09:46:00 +0800 Subject: [PATCH 3/4] Update extn layer on code changes --- testing/tf-modules/lambda_deployment/main.tf | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/testing/tf-modules/lambda_deployment/main.tf b/testing/tf-modules/lambda_deployment/main.tf index 2e4780dd..d852da6f 100644 --- a/testing/tf-modules/lambda_deployment/main.tf +++ b/testing/tf-modules/lambda_deployment/main.tf @@ -19,9 +19,10 @@ EOF } resource "aws_lambda_layer_version" "extn_layer" { - count = var.custom_lambda_extension_arn == "" ? 1 : 0 - filename = var.apm_aws_extension_path - layer_name = "${var.resource_prefix}_apm_aws_lambda_extn" + count = var.custom_lambda_extension_arn == "" ? 1 : 0 + filename = var.apm_aws_extension_path + layer_name = "${var.resource_prefix}_apm_aws_lambda_extn" + source_code_hash = filebase64sha256(var.apm_aws_extension_path) } resource "aws_iam_role_policy_attachment" "cw" { From 8a696f484436363a938be128c05aa47b88a7dedc Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Tue, 22 Nov 2022 09:46:21 +0800 Subject: [PATCH 4/4] Fix bool in headers for python test fn --- testing/functions/python3.8/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/functions/python3.8/main.py b/testing/functions/python3.8/main.py index 1e6ba08f..50ea2661 100644 --- a/testing/functions/python3.8/main.py +++ b/testing/functions/python3.8/main.py @@ -1,7 +1,7 @@ import json from elasticapm import capture_serverless -coldstart = True +coldstart = "true" @capture_serverless() def handler(event, context): global coldstart @@ -13,6 +13,6 @@ def handler(event, context): "coldstart": coldstart, } } - coldstart = False + coldstart = "false" return resp