Skip to content

Commit

Permalink
added cloudwatch trigger script
Browse files Browse the repository at this point in the history
  • Loading branch information
ahegdeNR committed Dec 24, 2024
1 parent 33a1f75 commit de62b23
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 43 deletions.
30 changes: 30 additions & 0 deletions e2e-tests/common-stack-scripts.sh → e2e-tests/common-scripts.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -35,4 +60,9 @@ delete_stack() {
else
echo "Unexpected stack status: $stack_status."
fi
}

exit_with_error() {
echo "Error: $1"
exit 1
}
4 changes: 3 additions & 1 deletion e2e-tests/config-file.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

43 changes: 43 additions & 0 deletions e2e-tests/lambda-cloudwatch-trigger.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF > parameter.json
'[{"LogGroupName":"anusha-firehose"}]'
EOF
LOG_GROUP_NAMES=$(<parameter.json)

deploy_stack "$LAMBDA_TEMPLATE_BUILD_DIR/$LAMBDA_TEMPLATE" "$CLOUDWATCH_TRIGGER_CASE" "$NEW_RELIC_LICENSE_KEY" "$NEW_RELIC_REGION" "$NEW_RELIC_ACCOUNT_ID" "false" "''" "$LOG_GROUP_NAMES" "''"
validate_stack_deployment_status "$CLOUDWATCH_TRIGGER_CASE"
validate_stack_resources "$CLOUDWATCH_TRIGGER_CASE" "anusha-firehose" "hello"
delete_stack "$CLOUDWATCH_TRIGGER_CASE"
60 changes: 18 additions & 42 deletions e2e-tests/lambda-s3-trigger.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,26 @@
#!/bin/bash

source ./common-stack-scripts.sh

exit_with_error() {
echo "Error: $1"
exit 1
}

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
source common-scripts.sh
source config-file.cfg

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
}
# test case constant
S3_TRIGGER_CASE=s3-trigger-stack-1

validate_stack_resources() {
stack_name=$1
bucket_name=$2
bucket_prefix=$3

resources=$(aws cloudformation describe-stack-resources --stack-name "$stack_name")

lambda_function_arn=$(echo "$resources" | \
jq -r '.StackResources[] | select(.ResourceType == "AWS::Lambda::Function") | .PhysicalResourceId' | \
grep "$LAMBDA_NAME" | \
xargs -I {} aws lambda get-function --function-name {} --query 'Configuration.FunctionArn' --output text)
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
)

notification_configuration=$(aws s3api get-bucket-notification-configuration --bucket "$bucket_name")

Expand All @@ -59,14 +37,12 @@ validate_stack_resources() {
fi
}

source config-file.cfg

cat <<EOF > parameter.json
'[{"bucket":"$S3_BUCKET_TRIGGER","prefix":"$S3_BUCKET_PREFIX"}]'
EOF
S3_BUCKET_NAMES=$(<parameter.json)

deploy_stack "$LAMBDA_TEMPLATE_BUILD_DIR/$LAMBDA_TEMPLATE" "e2eTests" "$NEW_RELIC_LICENSE_KEY" "$NEW_RELIC_REGION" "$NEW_RELIC_ACCOUNT_ID" "false" "$S3_BUCKET_NAMES" "''" "''"
validate_stack_deployment_status "e2eTests"
validate_stack_resources "e2eTests" "$S3_BUCKET_TRIGGER" "$S3_BUCKET_PREFIX"
delete_stack "e2eTests"
deploy_stack "$LAMBDA_TEMPLATE_BUILD_DIR/$LAMBDA_TEMPLATE" "$S3_TRIGGER_CASE" "$NEW_RELIC_LICENSE_KEY" "$NEW_RELIC_REGION" "$NEW_RELIC_ACCOUNT_ID" "false" "$S3_BUCKET_NAMES" "''" "''"
validate_stack_deployment_status "$S3_TRIGGER_CASE"
validate_stack_resources "$S3_TRIGGER_CASE" "$S3_BUCKET_TRIGGER" "$S3_BUCKET_PREFIX"
delete_stack "$S3_TRIGGER_CASE"

0 comments on commit de62b23

Please sign in to comment.