Skip to content

Commit 5c58adc

Browse files
authored
Merge pull request #2533 from sahilk225579/sahilk225579-feature-lambda-function-status
New serverless pattern - EventBridge Scheduler to invoke Step Functions to get the List of Inactive Lambda functions
2 parents 024612e + acc9e6b commit 5c58adc

File tree

6 files changed

+526
-0
lines changed

6 files changed

+526
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"title": "EventBridge Scheduler to publish daily list of inactive functions to SNS",
3+
"description": "Daily running Amazon EventBridge scheduler and invokes a state machine to find Inactive Lambda functions and publish the list to a SNS topic.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"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."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-stepfunction-lambda-getfunctionstatus",
16+
"templateURL": "serverless-patterns/eventbridge-stepfunction-lambda-getfunctionstatus",
17+
"projectFolder": "eventbridge-stepfunction-lambda-getfunctionstatus",
18+
"templateFile": "template.yaml"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Introducing Amazon EventBridge Scheduler",
25+
"link": "https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/"
26+
},
27+
{
28+
"text": "Amazon EventBridge Scheduler Docs",
29+
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html"
30+
},
31+
{
32+
"text": "Amazon EventBridge Scheduler Docs",
33+
"link": "https://docs.aws.amazon.com/lambda/latest/api/API_ListFunctions.html"
34+
},
35+
{
36+
"text": "Lambda ListFunctions API",
37+
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html"
38+
},
39+
{
40+
"text": "Lambda GetFunction API",
41+
"link": "https://docs.aws.amazon.com/lambda/latest/api/API_GetFunction.html"
42+
},
43+
{
44+
"text": "SNS Publish API",
45+
"link": "https://docs.aws.amazon.com/sns/latest/api/API_Publish.html"
46+
}
47+
]
48+
},
49+
"deploy": {
50+
"text": ["sam deploy"]
51+
},
52+
"testing": {
53+
"text": ["See the GitHub repo for detailed testing instructions."]
54+
},
55+
"cleanup": {
56+
"text": ["Delete the stack: <code>sam delete</code>."]
57+
},
58+
"authors": [
59+
{
60+
"name": "Sahil Kapoor",
61+
"image": "https://media.licdn.com/dms/image/v2/D5603AQHTVptga3RxcA/profile-displayphoto-shrink_800_800/B56ZO3ZfseHoAc-/0/1733948735068?e=1739404800&v=beta&t=FX6MFZ2JFH17KQc89u4gY6tQXGoMJLiLkB2qT3MtV2g",
62+
"bio": "AWS Cloud Support Engineer",
63+
"linkedin": "sahil-kapoor-503391a7",
64+
"twitter": ""
65+
}
66+
],
67+
"patternArch": {
68+
"icon1": {
69+
"x": 20,
70+
"y": 50,
71+
"service": "eventbridge-scheduler",
72+
"label": "Amazon EventBridge Scheduler"
73+
},
74+
"icon2": {
75+
"x": 50,
76+
"y": 50,
77+
"service": "step-functions",
78+
"label": "AWS Step Function"
79+
},
80+
"icon3": {
81+
"x": 80,
82+
"y": 50,
83+
"service": "sns",
84+
"label": "Amazon SNS"
85+
},
86+
"line1": {
87+
"from": "icon1",
88+
"to": "icon2",
89+
"label": "invoked daily"
90+
},
91+
"line2": {
92+
"from": "icon2",
93+
"to": "icon3",
94+
"label": ""
95+
}
96+
}
97+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"title": "Find list of Inactive lambda functions and publish it to SNS topic using Step Functions.",
3+
"description": "Simple pattern that runs daily using eventbridge scheduler and invokes a state machine to find Inactive functions and publish it to SNS topic.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"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."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-stepfunction-lambda-getfunctionstatus",
16+
"templateURL": "serverless-patterns/eventbridge-stepfunction-lambda-getfunctionstatus",
17+
"projectFolder": "eventbridge-stepfunction-lambda-getfunctionstatus",
18+
"templateFile": "eventbridge-stepfunction-lambda-getfunctionstatus/template.yaml"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Introducing Amazon EventBridge Scheduler",
25+
"link": "https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/"
26+
},
27+
{
28+
"text": "Amazon EventBridge Scheduler Docs",
29+
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html"
30+
},
31+
{
32+
"text": "Amazon EventBridge Scheduler Docs",
33+
"link": "https://docs.aws.amazon.com/lambda/latest/api/API_ListFunctions.html"
34+
},
35+
{
36+
"text": "Lambda ListFunctions API",
37+
"link": "https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html"
38+
},
39+
{
40+
"text": "Lambda GetFunction API",
41+
"link": "https://docs.aws.amazon.com/lambda/latest/api/API_GetFunction.html"
42+
},
43+
{
44+
"text": "SNS Publish API",
45+
"link": "https://docs.aws.amazon.com/sns/latest/api/API_Publish.html"
46+
}
47+
]
48+
},
49+
"deploy": {
50+
"text": [
51+
"sam deploy"
52+
]
53+
},
54+
"testing": {
55+
"text": [
56+
"See the GitHub repo for detailed testing instructions."
57+
]
58+
},
59+
"cleanup": {
60+
"text": [
61+
"Delete the stack: <code>sam delete</code>."
62+
]
63+
},
64+
"authors": [
65+
{
66+
"name": "Sahil Kapoor",
67+
"image": "https://media.licdn.com/dms/image/v2/D5603AQHTVptga3RxcA/profile-displayphoto-shrink_800_800/B56ZO3ZfseHoAc-/0/1733948735068?e=1739404800&v=beta&t=FX6MFZ2JFH17KQc89u4gY6tQXGoMJLiLkB2qT3MtV2g",
68+
"bio": "AWS Cloud Support Engineer",
69+
"linkedin": "sahil-kapoor-503391a7",
70+
"twitter": ""
71+
}
72+
]
73+
}
Binary file not shown.

0 commit comments

Comments
 (0)