|
| 1 | +# Amazon EventBridge Scheduler to invoke an AWS Step Function to get the list of inactive AWS Lambda functions |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* [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. |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 11 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 12 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 13 | + |
| 14 | +## Deployment Instructions |
| 15 | + |
| 16 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 17 | + ``` |
| 18 | + git clone https://github.com/aws-samples/serverless-patterns |
| 19 | + ``` |
| 20 | +2. Change directory to the pattern directory: |
| 21 | + ``` |
| 22 | + cd eventbridge-stepfunction-lambda-getfunctionstatus |
| 23 | + ``` |
| 24 | +3. Build the dependencies: |
| 25 | + ``` |
| 26 | + sam build |
| 27 | + ``` |
| 28 | +4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 29 | + ``` |
| 30 | + sam deploy --guided |
| 31 | + ``` |
| 32 | +5. During the prompts: |
| 33 | +
|
| 34 | +
|
| 35 | + * Enter **stack name**. |
| 36 | + * Enter desired **AWS Region**. |
| 37 | + * Enter your **Email Address** (e.g. [email protected]) for the EmailAddress parameter. |
| 38 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 39 | +
|
| 40 | + 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. |
| 41 | +
|
| 42 | +6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 43 | +
|
| 44 | +7. **Important** : Once the SNS topic & subscription is created, verify the subscription by clicking the 'Confirm subscription' link received on your email. |
| 45 | +
|
| 46 | +
|
| 47 | +## How it works |
| 48 | +
|
| 49 | +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. |
| 50 | +
|
| 51 | +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. |
| 52 | +
|
| 53 | + ```json |
| 54 | + { |
| 55 | + "Function_ARN_List_existing": { |
| 56 | + "lambda_arn_list_combined": [] |
| 57 | + }, |
| 58 | + "NextMarker": null |
| 59 | + } |
| 60 | + ``` |
| 61 | +
|
| 62 | +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. |
| 63 | +
|
| 64 | +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. |
| 65 | +
|
| 66 | +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. |
| 67 | +
|
| 68 | +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. |
| 69 | +
|
| 70 | +7) SNS publish : List of inactive functions is published to the SNS Topic. |
| 71 | +
|
| 72 | +
|
| 73 | +## Cleanup |
| 74 | + |
| 75 | +1. Delete the stack |
| 76 | + ```bash |
| 77 | + sam delete |
| 78 | + ``` |
| 79 | +
|
| 80 | +2. Also, you have to manually delete any schedules you created if required. |
| 81 | +
|
| 82 | +---- |
| 83 | +Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 84 | +
|
| 85 | +SPDX-License-Identifier: MIT-0 |
0 commit comments