From 7f1f27cf5befe0d43ec8b7265ed281a9dc6a8f0e Mon Sep 17 00:00:00 2001 From: sahilk225579 Date: Thu, 12 Dec 2024 02:10:57 +0530 Subject: [PATCH 1/5] added files for listing inactive function --- .../README.md | 80 ++++++++++ .../pattern.json | 73 +++++++++ .../src/lambda_function.zip | Bin 0 -> 552 bytes .../statemachine/definition.asl.json | 149 ++++++++++++++++++ .../template.yaml | 120 ++++++++++++++ 5 files changed, 422 insertions(+) create mode 100644 eventbridge-stepfunction-lambda-getfunctionstatus/README.md create mode 100644 eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json create mode 100644 eventbridge-stepfunction-lambda-getfunctionstatus/src/lambda_function.zip create mode 100644 eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json create mode 100644 eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md new file mode 100644 index 000000000..09994296f --- /dev/null +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md @@ -0,0 +1,80 @@ +# EventBridge Scheduler to invoke Step Functions to get the List of Inactive functions + +This pattern will create a one time schedule in EventBridge Scheduler with State machine as Target & Invoke the Lambda ListFunctions & GetFunction API using the SDK integration in Step Functions and publish the List of Inactive functions to SNS Topic. + +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. + +## Requirements + +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed + +## Deployment Instructions + +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: + ``` + git clone https://github.com/aws-samples/serverless-patterns + ``` +2. Change directory to the pattern directory: + ``` + cd eventbridge-stepfunction-lambda-getfunctionstatus + ``` +3. Build the dependencies: + ``` + sam build + ``` +4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: + ``` + sam deploy --guided + ``` +5. During the prompts: + * Enter a stack name + * Enter the desired AWS Region + * Parameter EmailAddress + * Allow SAM CLI to create IAM roles with the required permissions. + + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. + +6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. + +## How it works + +1. When the stack is deployed it creates a Step Functions state machine, an EventBridge Scheduler,a SNS Topic with Email subscription, a Lambda Function and required IAM roles. + +2. Event Bridge scheduler invokes the State machine on defined schedule, Default schedule to invoke the state machine daily at 00::00 UTC with payload. Modify the schedule as per your requirement. + + ```json + { + "Function_ARN_List_existing": { + "lambda_arn_list_combined": [] + }, + "NextMarker": null + } + ``` + +3) In Step function, the 1st step is to get the List of Lambda functions using the ListFunctions API call, ListFunctions API is called multiple times as it return max 50 Functions in a single API Call. NextMarker in the response is used to get the list of next 50 Functions. + +4) Lambda Invoke - Combine data Step : This a custom lambda function python script to create a combined json array of Lambda function ARN's. + +5) Map - Loop Functions State : In this state, GetFunction API is called for each Lambda ARN taken as input from previous step & status of each function is checked and appended to the output. + +6) Filter Inactive Functions State : This filters out the Active functions from the Json array and passes only the Inactive Functions list to the next step. + +7) SNS Publish : List of Inactive functions is published to the SNS Topic. + + +## Cleanup + +1. Delete the stack + ```bash + sam delete + ``` + +2. Also, you have to manually delete any schedules you created if required. + +---- +Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: MIT-0 \ No newline at end of file diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json b/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json new file mode 100644 index 000000000..159cf15f8 --- /dev/null +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json @@ -0,0 +1,73 @@ +{ + "title": "Find Inactive lambda functions and Publish it to SNS topic using Step Functions.", + "description": "Simple pattern that creates a Step Functions & uses SDK integration to find Inactive functions and Publish it to SNS topic.", + "language": "Python", + "level": "200", + "framework": "SAM", + "introBox": { + "headline": "How it works", + "text": [ + "Creates a one time schedule in EventBridge Scheduler with Step Functions as target and uses SDK integration to find the Lambda function Status and return list of Inactive Functions and then publishing to the SNS Topic." + ] + }, + "gitHub": { + "template": { + "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-stepfunction-lambda-getfunctionstatus", + "templateURL": "serverless-patterns/eventbridge-stepfunction-lambda-getfunctionstatus", + "projectFolder": "eventbridge-stepfunction-lambda-getfunctionstatus", + "templateFile": "eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml" + } + }, + "resources": { + "bullets": [ + { + "text": "Introducing Amazon EventBridge Scheduler", + "link": "https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/" + }, + { + "text": "Amazon EventBridge Scheduler Docs", + "link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html" + }, + { + "text": "Amazon EventBridge Scheduler Docs", + "link": "https://docs.aws.amazon.com/lambda/latest/api/API_ListFunctions.html" + }, + { + "text": "Lambda ListFunctions API", + "link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html" + }, + { + "text": "Lambda GetFunction API", + "link": "https://docs.aws.amazon.com/lambda/latest/api/API_GetFunction.html" + }, + { + "text": "SNS Publish API", + "link": "https://docs.aws.amazon.com/sns/latest/api/API_Publish.html" + } + ] + }, + "deploy": { + "text": [ + "sam deploy" + ] + }, + "testing": { + "text": [ + "See the GitHub repo for detailed testing instructions." + ] + }, + "cleanup": { + "text": [ + "Delete the stack: sam delete." + ] + }, + "authors": [ + { + "name": "Sahil Kapoor", + "image": "https://media.licdn.com/dms/image/v2/D5603AQHTVptga3RxcA/profile-displayphoto-shrink_800_800/B56ZO3ZfseHoAc-/0/1733948735068?e=1739404800&v=beta&t=FX6MFZ2JFH17KQc89u4gY6tQXGoMJLiLkB2qT3MtV2g", + "bio": "AWS Cloud Support Engineer", + "linkedin": "sahil-kapoor-503391a7", + "twitter": "" + } + ] +} diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/src/lambda_function.zip b/eventbridge-stepfunction-lambda-getfunctionstatus/src/lambda_function.zip new file mode 100644 index 0000000000000000000000000000000000000000..6ca232e134ee7b3111c24ffa8d178b752a2fa286 GIT binary patch literal 552 zcmWIWW@Zs#-~d9EZC#NJP@uugz#zn+z>t%eo0O6mpH`ZeT#}ier&mxJ8p6xK&RY?Y zt^~viKwMhE&A`a=osoe7XbuBIYtX^0!v;KipKH6Yf96V^BFk($<=}!1Q#WRF9`v1Z zb*(acv}Eh0`uUHy6pQcDV;6qCSKanxP-(Kx;cJp!lZ3P{Y>Jz1bl*eE^cMRcr$?M3 zZ4%#Y{DeQmTll%Gl{w7Al&=~Wr4=E0>bd;sKUbYC-~Cbhe3s^*`+nYdP( zMwImOD`X@~?O?xY;;~fxaMMOMvxDjL7aNI%h=-Qw{nO6Qi^_`x;TVuu)H zDt4#rC_XLyMz%RJf8&D%-=qXxAD!8IGcrd_OSCeB&D*(xl~?RGa}BA~W{ M6M*z4puG$X0Q8^36#xJL literal 0 HcmV?d00001 diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json b/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json new file mode 100644 index 000000000..542941e5c --- /dev/null +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json @@ -0,0 +1,149 @@ +{ + "Comment": "A description of my state machine", + "StartAt": "ListFunctions", + "States": { + "ListFunctions": { + "Type": "Task", + "Arguments": { + "Marker": "{% $states.input.NextMarker%}" + }, + "Resource": "arn:aws:states:::aws-sdk:lambda:listFunctions", + "Next": "Lambda Invoke - Combine data", + "Output": { + "ListFunctions_output": "{% $states.result%}", + "Function_ARN_List_existing": { + "lambda_arn_list_combined": "{% $states.input.Function_ARN_List_existing.lambda_arn_list_combined%}" + } + } + }, + "Lambda Invoke - Combine data": { + "Type": "Task", + "Resource": "arn:aws:states:::lambda:invoke", + "Output": "{% $states.result.Payload %}", + "Arguments": { + "FunctionName": "${LambdaFunctionArn}", + "Payload": "{% $states.input %}" + }, + "Retry": [ + { + "ErrorEquals": [ + "Lambda.ServiceException", + "Lambda.AWSLambdaException", + "Lambda.SdkClientException", + "Lambda.TooManyRequestsException" + ], + "IntervalSeconds": 1, + "MaxAttempts": 3, + "BackoffRate": 2, + "JitterStrategy": "FULL" + } + ], + "Next": "Check NextMarker" + }, + "Check NextMarker": { + "Type": "Choice", + "Choices": [ + { + "Next": "ListFunctions", + "Condition": "{% $states.input.NextMarkercheck = 'true' %}", + "Output": "{% $states.input%}" + } + ], + "Default": "Map - Loop Functions" + }, + "Map - Loop Functions": { + "Type": "Map", + "ItemProcessor": { + "ProcessorConfig": { + "Mode": "INLINE" + }, + "StartAt": "GetFunction", + "States": { + "GetFunction": { + "Type": "Task", + "Arguments": { + "FunctionName": "{% $states.input.ARN %}" + }, + "Resource": "arn:aws:states:::aws-sdk:lambda:getFunction", + "Next": "Check Function Status" + }, + "Check Function Status": { + "Type": "Choice", + "Choices": [ + { + "Next": "Inactive", + "Output": { + "FunctionConfig": { + "ARN": "{% $states.input.Configuration.FunctionArn %}", + "State": "{% $states.input.Configuration.State %}", + "StateReason": "{% $states.input.Configuration.StateReason %}", + "StateReasonCode": "{% $states.input.Configuration.StateReasonCode %}", + "LastModified": "{% $states.input.Configuration.LastModified %}" + } + }, + "Condition": "{% $states.input.Configuration.State = 'Inactive' %}" + } + ], + "Default": "Pass", + "Output": { + "FunctionConfig": { + "ARN": "{% $states.input.Configuration.FunctionArn %}", + "State": "{% $states.input.Configuration.State %}" + } + } + }, + "Pass": { + "Type": "Pass", + "End": true + }, + "Inactive": { + "Type": "Pass", + "End": true + } + } + }, + "Items": "{% $states.input.Function_ARN_List_existing.lambda_arn_list_combined %}", + "Next": "Filter Inactive Functions", + "MaxConcurrency": 40 + }, + "Filter Inactive Functions": { + "Type": "Pass", + "Next": "Choice", + "Output": { + "Inactive_Functions": "{% $states.input.FunctionConfig[State='Inactive'] ? $states.input.FunctionConfig[State='Inactive'] : false %}" + } + }, + "Choice": { + "Type": "Choice", + "Choices": [ + { + "Condition": "{% $states.input.Inactive_Functions = false %}", + "Next": "No Inactivce Function", + "Output": { + "Inactive_Functions": " No Inactive Functions" + } + } + ], + "Default": "Pass (2)" + }, + "Pass (2)": { + "Type": "Pass", + "Next": "SNS Publish" + }, + "No Inactivce Function": { + "Type": "Pass", + "Next": "SNS Publish" + }, + "SNS Publish": { + "Type": "Task", + "Resource": "arn:aws:states:::sns:publish", + "Arguments": { + "TopicArn": "${TopicARN}", + "Message": "{% $states.input.Inactive_Functions %}", + "Subject": "Inactive Lambda functions List" + }, + "End": true + } + }, + "QueryLanguage": "JSONata" +} \ No newline at end of file diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml b/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml new file mode 100644 index 000000000..18f7d416e --- /dev/null +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml @@ -0,0 +1,120 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: SAM Template for Step Function with daily EventBridge trigger + + +Parameters: + EmailAddress: + Type: String + Description: Email address to subscribe to the SNS topic + + +Resources: + StepFunction: + Type: AWS::Serverless::StateMachine + Properties: + Role : !GetAtt StepFunctionEventRole.Arn + DefinitionUri: statemachine/definition.asl.json + DefinitionSubstitutions: + LambdaFunctionArn: !GetAtt LambdaFunction.Arn + TopicARN : !GetAtt MySNSTopic.TopicArn + + DailyEventRule: + Type: AWS::Events::Rule + Properties: + Description: "Trigger Step Function daily at midnight UTC" + ScheduleExpression: "cron(0 0 * * ? *)" + State: "ENABLED" + Targets: + - Arn: !Ref StepFunction + Id: "StepFunctionTarget" + RoleArn: !GetAtt EventBridgeRule.Arn + Input: '{ "Function_ARN_List_existing": {"lambda_arn_list_combined": []},"NextMarker": null}' + + EventBridgeRule: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: events.amazonaws.com + Action: sts:AssumeRole + Policies: + - PolicyName: StepFunctionExecutionPolicy + PolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Action: + - states:StartExecution + Resource: !Ref StepFunction + + + StepFunctionEventRole: + Type: AWS::IAM::Role + Properties: + AssumeRolePolicyDocument: + Version: "2012-10-17" + Statement: + - Effect: Allow + Principal: + Service: states.amazonaws.com + Action: sts:AssumeRole + Policies: + - PolicyName: StepFunctionExecutionPolicy + PolicyDocument: + Version: "2012-10-17" + Statement: + + - Effect: Allow + Action: + - lambda:ListFunctions + - lambda:GetFunction + Resource: "*" + + - Effect: Allow + Action: + - sns:Publish + Resource: !GetAtt MySNSTopic.TopicArn + + - Effect: Allow + Action: + - lambda:InvokeFunction + Resource: !GetAtt LambdaFunction.Arn + + + LambdaFunction: + Type: AWS::Serverless::Function + Properties: + FunctionName: GetFunction + Handler: lambda_function.lambda_handler + CodeUri: src/lambda_function.zip + Runtime: python3.13 + + + MySNSTopic: + Type: AWS::SNS::Topic + Properties: + DisplayName: Notify-inactive-functions + + MySNSSubscription: + Type: AWS::SNS::Subscription + Properties: + TopicArn: !Ref MySNSTopic + Protocol: email + Endpoint: !Ref EmailAddress + + +Outputs: + StepFunctionArn: + Description: "ARN of the Step Function" + Value: !Ref StepFunction + EventRuleName: + Description: "Name of the EventBridge Rule" + Value: !Ref DailyEventRule + LambdaFunction: + Description: "LambdaFunction ARN" + Value: !GetAtt LambdaFunction.Arn + From f05c85a136680ce57e7e94419d78fdb027f374a6 Mon Sep 17 00:00:00 2001 From: sahilk225579 Date: Thu, 12 Dec 2024 02:19:12 +0530 Subject: [PATCH 2/5] updated readme --- eventbridge-stepfunction-lambda-getfunctionstatus/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md index 09994296f..ed1387950 100644 --- a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md @@ -1,4 +1,4 @@ -# EventBridge Scheduler to invoke Step Functions to get the List of Inactive functions +# EventBridge Scheduler to invoke Step Functions to get the List of Inactive Lambda functions This pattern will create a one time schedule in EventBridge Scheduler with State machine as Target & Invoke the Lambda ListFunctions & GetFunction API using the SDK integration in Step Functions and publish the List of Inactive functions to SNS Topic. From 13f1a53289bf3f1829935c0fd7c923874a19bf5c Mon Sep 17 00:00:00 2001 From: sahilk225579 Date: Sat, 21 Dec 2024 20:58:00 +0530 Subject: [PATCH 3/5] updated template --- .../README.md | 25 +++++++++++-------- .../pattern.json | 6 ++--- .../statemachine/definition.asl.json | 6 ++--- .../template.yaml | 6 +++-- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md index ed1387950..d770e9fd4 100644 --- a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md @@ -1,6 +1,6 @@ -# EventBridge Scheduler to invoke Step Functions to get the List of Inactive Lambda functions +# EventBridge scheduler to invoke step function to get the list of inactive lambda functions -This pattern will create a one time schedule in EventBridge Scheduler with State machine as Target & Invoke the Lambda ListFunctions & GetFunction API using the SDK integration in Step Functions and publish the List of Inactive functions to SNS Topic. +This pattern will create a one time schedule in EventBridge scheduler with state machine as target & invoke the lambda ListFunctions & GetFunction API using the SDK integration in step functions and publish the list of inactive functions to SNS Topic. Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. @@ -30,20 +30,25 @@ Important: this application uses various AWS services and there are costs associ sam deploy --guided ``` 5. During the prompts: - * Enter a stack name - * Enter the desired AWS Region - * Parameter EmailAddress + + + * Enter **stack name**. + * Enter desired **AWS Region**. + * Enter your **Email Address** (e.g. abcd@xyz.com) for the EmailAddress parameter. * Allow SAM CLI to create IAM roles with the required permissions. Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. 6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. +7. **Imp** : Once the SNS topic & subscription is created, verify the subscription by clicking the 'Confirm subscription' link received on your email. + + ## How it works -1. When the stack is deployed it creates a Step Functions state machine, an EventBridge Scheduler,a SNS Topic with Email subscription, a Lambda Function and required IAM roles. +1. When the stack is deployed it creates a state machine, an eventBridge scheduler,a SNS topic with email subscription, a Lambda Function and required IAM roles. -2. Event Bridge scheduler invokes the State machine on defined schedule, Default schedule to invoke the state machine daily at 00::00 UTC with payload. Modify the schedule as per your requirement. +2. Event Bridge scheduler invokes the State machine on defined schedule, Default schedule invokes the state machine daily at 00::00 UTC with the below mentioned payload. Modify the schedule as per your requirement. ```json { @@ -54,13 +59,13 @@ Important: this application uses various AWS services and there are costs associ } ``` -3) In Step function, the 1st step is to get the List of Lambda functions using the ListFunctions API call, ListFunctions API is called multiple times as it return max 50 Functions in a single API Call. NextMarker in the response is used to get the list of next 50 Functions. +3) In state machine, the 1st step is to get the list of all lambda functions in the current region using the ListFunctions API, ListFunctions API is called multiple times as it returns max 50 functions list in a single API call along with NextMarker pagination parameter. Using NextMarker token state machine retrieves list of all the function ARN's. -4) Lambda Invoke - Combine data Step : This a custom lambda function python script to create a combined json array of Lambda function ARN's. +4) Lambda Invoke Combine data Step : This a custom lambda function python script to create a combined json array of Lambda function ARN's retrieved in step 1. 5) Map - Loop Functions State : In this state, GetFunction API is called for each Lambda ARN taken as input from previous step & status of each function is checked and appended to the output. -6) Filter Inactive Functions State : This filters out the Active functions from the Json array and passes only the Inactive Functions list to the next step. +6) Filter Inactive Functions State : This filters out the Active functions from the Json array and passes only the Inactive functions list to the next step. 7) SNS Publish : List of Inactive functions is published to the SNS Topic. diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json b/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json index 159cf15f8..63bcc3a7d 100644 --- a/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/pattern.json @@ -1,13 +1,13 @@ { - "title": "Find Inactive lambda functions and Publish it to SNS topic using Step Functions.", - "description": "Simple pattern that creates a Step Functions & uses SDK integration to find Inactive functions and Publish it to SNS topic.", + "title": "Find list of Inactive lambda functions and publish it to SNS topic using Step Functions.", + "description": "Simple pattern that runs daily using eventbridge scheduler and invokes a state machine to find Inactive functions and publish it to SNS topic.", "language": "Python", "level": "200", "framework": "SAM", "introBox": { "headline": "How it works", "text": [ - "Creates a one time schedule in EventBridge Scheduler with Step Functions as target and uses SDK integration to find the Lambda function Status and return list of Inactive Functions and then publishing to the SNS Topic." + "Creates a one time schedule in eventBridge scheduler with state machine as target and uses SDK integration to find the lambda function status and returns the list of Inactive functions and which is then published to the SNS Topic." ] }, "gitHub": { diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json b/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json index 542941e5c..5e1c78479 100644 --- a/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/statemachine/definition.asl.json @@ -124,9 +124,9 @@ } } ], - "Default": "Pass (2)" + "Default": "Inactive function pass state" }, - "Pass (2)": { + "Inactive function pass state": { "Type": "Pass", "Next": "SNS Publish" }, @@ -140,7 +140,7 @@ "Arguments": { "TopicArn": "${TopicARN}", "Message": "{% $states.input.Inactive_Functions %}", - "Subject": "Inactive Lambda functions List" + "Subject": "Inactive Lambda functions list | ${region} | Account - ${accountID}" }, "End": true } diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml b/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml index 18f7d416e..825ff9d61 100644 --- a/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml @@ -18,6 +18,8 @@ Resources: DefinitionSubstitutions: LambdaFunctionArn: !GetAtt LambdaFunction.Arn TopicARN : !GetAtt MySNSTopic.TopicArn + region : !Sub ${AWS::Region} + accountID : !Sub ${AWS::AccountId} DailyEventRule: Type: AWS::Events::Rule @@ -28,10 +30,10 @@ Resources: Targets: - Arn: !Ref StepFunction Id: "StepFunctionTarget" - RoleArn: !GetAtt EventBridgeRule.Arn + RoleArn: !GetAtt EventBridgeRulerole.Arn Input: '{ "Function_ARN_List_existing": {"lambda_arn_list_combined": []},"NextMarker": null}' - EventBridgeRule: + EventBridgeRulerole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: From 2dab43243398ff5bddbc10204bb2dfed2a62011d Mon Sep 17 00:00:00 2001 From: sahilk225579 Date: Mon, 30 Dec 2024 17:30:13 +0530 Subject: [PATCH 4/5] added changes for readme file --- .../README.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md index d770e9fd4..16a1b3e4e 100644 --- a/eventbridge-stepfunction-lambda-getfunctionstatus/README.md +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/README.md @@ -1,6 +1,6 @@ -# EventBridge scheduler to invoke step function to get the list of inactive lambda functions +# Amazon EventBridge Scheduler to invoke an AWS Step Function to get the list of inactive AWS Lambda functions -This pattern will create a one time schedule in EventBridge scheduler with state machine as target & invoke the lambda ListFunctions & GetFunction API using the SDK integration in step functions and publish the list of inactive functions to SNS Topic. +This pattern will create an Amazon EventBridge Scheduler rule targeting an AWS Step Function state machine. The Step Function invokes an AWS Lambda using `ListFunctions` and `GetFunctions` API using the SDK integration in Step Functions. It then publishes the list of inactive functions to an Amazon SNS Topic. Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. @@ -41,14 +41,14 @@ Important: this application uses various AWS services and there are costs associ 6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. -7. **Imp** : Once the SNS topic & subscription is created, verify the subscription by clicking the 'Confirm subscription' link received on your email. +7. **Important** : Once the SNS topic & subscription is created, verify the subscription by clicking the 'Confirm subscription' link received on your email. ## How it works -1. When the stack is deployed it creates a state machine, an eventBridge scheduler,a SNS topic with email subscription, a Lambda Function and required IAM roles. +1. When the stack is deployed it creates a state machine, an Event Bridge Scheduler, an SNS topic with email subscription, a Lambda function and required IAM roles. -2. Event Bridge scheduler invokes the State machine on defined schedule, Default schedule invokes the state machine daily at 00::00 UTC with the below mentioned payload. Modify the schedule as per your requirement. +2. Event Bridge Scheduler invokes the state machine with a defined schedule. The default schedule invokes the state machine daily at 00:00 UTC with the below mentioned payload. Modify the schedule as per your requirement. ```json { @@ -59,15 +59,15 @@ Important: this application uses various AWS services and there are costs associ } ``` -3) In state machine, the 1st step is to get the list of all lambda functions in the current region using the ListFunctions API, ListFunctions API is called multiple times as it returns max 50 functions list in a single API call along with NextMarker pagination parameter. Using NextMarker token state machine retrieves list of all the function ARN's. +3) In state machine, the 1st step is to get the list of all lambda functions in the current region using the ListFunctions API, ListFunctions API is called multiple times as it returns max 50 functions list in a single API call along with NextMarker pagination parameter. Using NextMarker token state machine retrieves list of all the function ARNs. -4) Lambda Invoke Combine data Step : This a custom lambda function python script to create a combined json array of Lambda function ARN's retrieved in step 1. +4) Lambda Invoke Combine data Step : This a custom Lambda function python script to create a combined JSON array of Lambda function ARNs retrieved in step 1. -5) Map - Loop Functions State : In this state, GetFunction API is called for each Lambda ARN taken as input from previous step & status of each function is checked and appended to the output. +5) Map - loop functions state: In this state, the GetFunction API is called for each Lambda ARN taken as input from the previous step, and the status of each function is checked and appended to the output. -6) Filter Inactive Functions State : This filters out the Active functions from the Json array and passes only the Inactive functions list to the next step. +6) Filter inactive functions state: This filters out the active functions from the JSON array and passes only the list of inactive functions to the next step. -7) SNS Publish : List of Inactive functions is published to the SNS Topic. +7) SNS publish : List of inactive functions is published to the SNS Topic. ## Cleanup @@ -80,6 +80,6 @@ Important: this application uses various AWS services and there are costs associ 2. Also, you have to manually delete any schedules you created if required. ---- -Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. +Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0 \ No newline at end of file From acc9e6ba22e120d88a8bf35dd4f14e2c6142cbea Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 24 Jan 2025 16:28:35 +0100 Subject: [PATCH 5/5] Create eventbridge-stepfunction-lambda-getfunctionstatus.json added patterns json file --- ...stepfunction-lambda-getfunctionstatus.json | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 eventbridge-stepfunction-lambda-getfunctionstatus/eventbridge-stepfunction-lambda-getfunctionstatus.json diff --git a/eventbridge-stepfunction-lambda-getfunctionstatus/eventbridge-stepfunction-lambda-getfunctionstatus.json b/eventbridge-stepfunction-lambda-getfunctionstatus/eventbridge-stepfunction-lambda-getfunctionstatus.json new file mode 100644 index 000000000..3cc7c973b --- /dev/null +++ b/eventbridge-stepfunction-lambda-getfunctionstatus/eventbridge-stepfunction-lambda-getfunctionstatus.json @@ -0,0 +1,97 @@ +{ + "title": "EventBridge Scheduler to publish daily list of inactive functions to SNS", + "description": "Daily running Amazon EventBridge scheduler and invokes a state machine to find Inactive Lambda functions and publish the list to a SNS topic.", + "language": "Python", + "level": "200", + "framework": "SAM", + "introBox": { + "headline": "How it works", + "text": [ + "Creates a one time schedule in EventBridge scheduler with a StepFunctions state machine as target. Uses the SDK integration to find the Lambda function status, returns the list of inactive functions and publishes the list to a SNS Topic." + ] + }, + "gitHub": { + "template": { + "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-stepfunction-lambda-getfunctionstatus", + "templateURL": "serverless-patterns/eventbridge-stepfunction-lambda-getfunctionstatus", + "projectFolder": "eventbridge-stepfunction-lambda-getfunctionstatus", + "templateFile": "template.yaml" + } + }, + "resources": { + "bullets": [ + { + "text": "Introducing Amazon EventBridge Scheduler", + "link": "https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/" + }, + { + "text": "Amazon EventBridge Scheduler Docs", + "link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html" + }, + { + "text": "Amazon EventBridge Scheduler Docs", + "link": "https://docs.aws.amazon.com/lambda/latest/api/API_ListFunctions.html" + }, + { + "text": "Lambda ListFunctions API", + "link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html" + }, + { + "text": "Lambda GetFunction API", + "link": "https://docs.aws.amazon.com/lambda/latest/api/API_GetFunction.html" + }, + { + "text": "SNS Publish API", + "link": "https://docs.aws.amazon.com/sns/latest/api/API_Publish.html" + } + ] + }, + "deploy": { + "text": ["sam deploy"] + }, + "testing": { + "text": ["See the GitHub repo for detailed testing instructions."] + }, + "cleanup": { + "text": ["Delete the stack: sam delete."] + }, + "authors": [ + { + "name": "Sahil Kapoor", + "image": "https://media.licdn.com/dms/image/v2/D5603AQHTVptga3RxcA/profile-displayphoto-shrink_800_800/B56ZO3ZfseHoAc-/0/1733948735068?e=1739404800&v=beta&t=FX6MFZ2JFH17KQc89u4gY6tQXGoMJLiLkB2qT3MtV2g", + "bio": "AWS Cloud Support Engineer", + "linkedin": "sahil-kapoor-503391a7", + "twitter": "" + } + ], + "patternArch": { + "icon1": { + "x": 20, + "y": 50, + "service": "eventbridge-scheduler", + "label": "Amazon EventBridge Scheduler" + }, + "icon2": { + "x": 50, + "y": 50, + "service": "step-functions", + "label": "AWS Step Function" + }, + "icon3": { + "x": 80, + "y": 50, + "service": "sns", + "label": "Amazon SNS" + }, + "line1": { + "from": "icon1", + "to": "icon2", + "label": "invoked daily" + }, + "line2": { + "from": "icon2", + "to": "icon3", + "label": "" + } + } +}