From de62b2362fd2fbfa718d2c6e53bc38067ae455c1 Mon Sep 17 00:00:00 2001 From: Anusha Hegde Date: Tue, 24 Dec 2024 13:31:17 +0530 Subject: [PATCH] added cloudwatch trigger script --- ...mon-stack-scripts.sh => common-scripts.sh} | 30 ++++++++++ e2e-tests/config-file.cfg | 4 +- e2e-tests/lambda-cloudwatch-trigger.sh | 43 +++++++++++++ e2e-tests/lambda-s3-trigger.sh | 60 ++++++------------- 4 files changed, 94 insertions(+), 43 deletions(-) rename e2e-tests/{common-stack-scripts.sh => common-scripts.sh} (68%) create mode 100755 e2e-tests/lambda-cloudwatch-trigger.sh diff --git a/e2e-tests/common-stack-scripts.sh b/e2e-tests/common-scripts.sh similarity index 68% rename from e2e-tests/common-stack-scripts.sh rename to e2e-tests/common-scripts.sh index 9911bd0..a5a0b65 100644 --- a/e2e-tests/common-stack-scripts.sh +++ b/e2e-tests/common-scripts.sh @@ -1,5 +1,30 @@ #!/bin/bash +deploy_stack() { + template_file=$1 + stack_name=$2 + license_key=$3 + new_relic_region=$4 + new_relic_account_id=$5 + secret_license_key=$6 + s3_bucket_names=$7 + log_group_config=$8 + common_attributes=$9 + + sam deploy \ + --template-file "$template_file" \ + --stack-name "$stack_name" \ + --parameter-overrides \ + LicenseKey="$license_key" \ + NewRelicRegion="$new_relic_region" \ + NewRelicAccountId="$new_relic_account_id" \ + StoreNRLicenseKeyInSecretManager="$secret_license_key" \ + S3BucketNames="$s3_bucket_names" \ + LogGroupConfig="$log_group_config" \ + CommonAttributes="$common_attributes" \ + --capabilities CAPABILITY_IAM +} + validate_stack_deployment_status() { stack_name=$1 @@ -35,4 +60,9 @@ delete_stack() { else echo "Unexpected stack status: $stack_status." fi +} + +exit_with_error() { + echo "Error: $1" + exit 1 } \ No newline at end of file diff --git a/e2e-tests/config-file.cfg b/e2e-tests/config-file.cfg index baef994..a83e773 100644 --- a/e2e-tests/config-file.cfg +++ b/e2e-tests/config-file.cfg @@ -11,10 +11,12 @@ BUILD_DIR_BASE=.aws-sam/build LAMBDA_TEMPLATE=lambda-template.yaml LAMBDA_TEMPLATE_BUILD_DIR=.aws-sam/build/lambda-template -LAMBDA_NAME="NewRelicLogsServerlessLogForwarder" +LAMBDA_LOGICAL_RESOURCE_ID=NewRelicLogsServerlessLogForwarder # deployment constants S3_BUCKET_TRIGGER=tanusha-test-test S3_BUCKET_PREFIX=one +S3_BUCKET_TRIGGER=tanusha-test-test +S3_BUCKET_PREFIX=one diff --git a/e2e-tests/lambda-cloudwatch-trigger.sh b/e2e-tests/lambda-cloudwatch-trigger.sh new file mode 100755 index 0000000..87dedf1 --- /dev/null +++ b/e2e-tests/lambda-cloudwatch-trigger.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +source common-scripts.sh +source config-file.cfg + +# test case constants +CLOUDWATCH_TRIGGER_CASE=cloudwatch-trigger-stack-1 + +validate_stack_resources() { + stack_name=$1 + log_group_name=$2 + log_group_filter=$3 + + lambda_physical_id=$(aws cloudformation describe-stack-resources \ + --stack-name "$stack_name" \ + --logical-resource-id "$LAMBDA_LOGICAL_RESOURCE_ID" \ + --query "StackResources[0].PhysicalResourceId" \ + --output text + ) + lambda_function_arn=$(aws lambda get-function --function-name "$lambda_physical_id" \ + --query "Configuration.FunctionArn" \ + --output text + ) + + subscriptions=$(aws logs describe-subscription-filters --log-group-name "$log_group_name" --query 'subscriptionFilters[*].[destinationArn, filterPattern]' --output text) + + if echo "$subscriptions" | grep -q "$lambda_function_arn" && echo "$subscriptions" | grep -q "$log_group_filter"; then + echo "Lambda function $lambda_function_arn is subscribed to log group: $log_group_name with filter: $log_group_filter" + else + exit_with_error "Lambda function $lambda_function_arn is not subscribed to log group: $log_group_name" + fi + +} + +cat < parameter.json +'[{"LogGroupName":"anusha-firehose"}]' +EOF +LOG_GROUP_NAMES=$( parameter.json '[{"bucket":"$S3_BUCKET_TRIGGER","prefix":"$S3_BUCKET_PREFIX"}]' EOF S3_BUCKET_NAMES=$(