Skip to content

Commit d5ee3d7

Browse files
authored
Merge pull request #2338 from archiev4/archiev4-feature-eventbridge-s3-lambda
New Serverless Pattern - EventBridge - S3 - Lambda
2 parents e6a27af + 0d312dc commit d5ee3d7

File tree

6 files changed

+325
-0
lines changed

6 files changed

+325
-0
lines changed

eventbridge-s3-lambda/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# AWS Lambda sending a notification when a new Amazon S3 bucket is created
2+
3+
This pattern creates an automated monitoring system for Amazon S3 bucket create events. It defines an Amazon EventBridge rule that listens for S3 bucket creation events and invokes an AWS Lambda function. The Lambda function processes the event details and sends a notification to an Amazon SNS topic, informing subscribers of the new bucket creation.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/eventbridge-s3-lambda
6+
7+
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.
8+
9+
## Requirements
10+
11+
* [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.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [Terraform](https://learn.hashicorp.cxom/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
15+
16+
17+
## Deployment Instructions
18+
19+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
20+
```
21+
git clone https://github.com/aws-samples/serverless-patterns
22+
```
23+
1. Change directory to the pattern directory:
24+
```
25+
cd eventbridge-s3-lambda
26+
```
27+
1. From the command line, initialize terraform to downloads and installs the providers defined in the configuration:
28+
```
29+
terraform init
30+
```
31+
1. From the command line, apply the configuration in the main.tf file:
32+
```
33+
terraform apply
34+
```
35+
1. During the prompts
36+
#var.region
37+
- Enter a value: {enter the region for deployment}
38+
39+
## Testing
40+
41+
1. After deploying the stack, create a Subscriber for your Amazon SNS topic (For ex, your email) and confirm the subscription.
42+
https://docs.aws.amazon.com/sns/latest/dg/sns-create-subscribe-endpoint-to-topic.html
43+
44+
1. Create a new S3 bucket using the following CLI command
45+
```
46+
aws s3api create-bucket --bucket BUCKET_NAME --region REGION_NAME
47+
```
48+
Note: Make sure that the region is same as the region in which you deployed the Terraform code.
49+
50+
1. The event invokes the Lambda function and you will receive an email from the SNS Topic.
51+
52+
## Cleanup
53+
54+
1. Delete the SNS Subscription:
55+
Go to SNS > Subsciptions > Select your Subscription and click on Delete
56+
57+
https://docs.aws.amazon.com/sns/latest/dg/sns-delete-subscription-topic.html
58+
59+
1. Change directory to the pattern directory:
60+
```
61+
cd serverless-patterns/eventbridge-s3-lambda
62+
```
63+
1. Delete all created resources
64+
```
65+
terraform destroy
66+
```
67+
68+
1. During the prompts:
69+
```
70+
Enter all details as entered during creation.
71+
```
72+
1. Confirm all created resources has been deleted
73+
```
74+
terraform show
75+
```
76+
----
77+
Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
78+
79+
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": "Amazon S3 bucket creation notifications",
3+
"description": "Notify users via AWS Lambda and Amazon SNS when a new Amazon S3 Bucket is created.",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This sample project demonstrates how to notify users when a new S3 bucket is created. An Amazon EventBridge rule detects S3 bucket creation events and invokes a Lambda function. This Lambda function processes the event details and publishes a notification to an SNS topic. This enables automated monitoring and notification of new S3 Bucket creations."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-s3-lambda",
16+
"templateURL": "serverless-patterns/eventbridge-s3-lambda",
17+
"projectFolder": "eventbridge-s3-lambda",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Events from AWS services",
25+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html"
26+
},
27+
{
28+
"text": "How can I publish a message to an Amazon SNS topic using a Lambda function?",
29+
"link": "https://repost.aws/knowledge-center/sns-topic-lambda"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"terraform destroy",
47+
"terraform show"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Archana V",
53+
"image": "https://media.licdn.com/dms/image/D5603AQF_QwVjCkS_UQ/profile-displayphoto-shrink_200_200/0/1670929520771?e=1724284800&v=beta&t=FFJJko4OO8h1tCFrxMyneTyRPAKmyEmIaDOYOeTaFEk",
54+
"bio": "Cloud Support Engineer at AWS",
55+
"linkedin": "archana-venkat-9b80b7184"
56+
}
57+
],
58+
"patternArch": {
59+
"icon1": {
60+
"x": 10,
61+
"y": 50,
62+
"service": "s3",
63+
"label": "Amazon S3"
64+
},
65+
"icon2": {
66+
"x": 40,
67+
"y": 50,
68+
"service": "eventbridge",
69+
"label": "Amazon EventBridge"
70+
},
71+
"icon3": {
72+
"x": 65,
73+
"y": 50,
74+
"service": "lambda",
75+
"label": "AWS Lambda"
76+
},
77+
"icon4": {
78+
"x": 90,
79+
"y": 50,
80+
"service": "sns",
81+
"label": "Amazon SNS"
82+
},
83+
"line1": {
84+
"from": "icon1",
85+
"to": "icon2",
86+
"label": "Bucket Created"
87+
},
88+
"line2": {
89+
"from": "icon2",
90+
"to": "icon3"
91+
},
92+
"line3": {
93+
"from": "icon3",
94+
"to": "icon4"
95+
}
96+
}
97+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"title": "Lambda Function sending SNS notification when a new S3 Bucket is created",
3+
"description": "Notify users when a new S3 Bucket is created using Lambda and SNS",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This sample project demonstrates how to invoke a notify users when a new S3 bucket is created. The EventBridge rule detects S3 bucket creation events and triggers a Lambda function. This Lambda function further processes the event details and sends a notification to an SNS topic. This enables automated monitoring and notification of new S3 Bucket creations."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-s3-lambda",
16+
"templateURL": "serverless-patterns/eventbridge-s3-lambda",
17+
"projectFolder": "eventbridge-s3-lambda",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Events from AWS services",
25+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html"
26+
},
27+
{
28+
"text": "How can I publish a message to an Amazon SNS topic using a Lambda function?",
29+
"link": "https://repost.aws/knowledge-center/sns-topic-lambda"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"terraform destroy",
47+
"terraform show"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Archana V",
53+
"image": "https://media.licdn.com/dms/image/D5603AQF_QwVjCkS_UQ/profile-displayphoto-shrink_200_200/0/1670929520771?e=1724284800&v=beta&t=FFJJko4OO8h1tCFrxMyneTyRPAKmyEmIaDOYOeTaFEk",
54+
"bio": "Cloud Support Engineer at AWS",
55+
"linkedin": "archana-venkat-9b80b7184"
56+
}
57+
]
58+
}
69.2 KB
Loading
926 Bytes
Binary file not shown.

eventbridge-s3-lambda/main.tf

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
variable "region" {}
2+
3+
provider "aws" {
4+
region = "${var.region}"
5+
}
6+
7+
data "aws_partition" "current" {}
8+
resource "aws_iam_role" "lambda_execution_role" {
9+
name = "lambda-execution-role-sns"
10+
assume_role_policy = jsonencode({
11+
Version = "2012-10-17"
12+
Statement = [{
13+
Effect = "Allow"
14+
Principal = { Service = "lambda.amazonaws.com" }
15+
Action = "sts:AssumeRole"
16+
}]
17+
})
18+
}
19+
resource "aws_iam_role_policy" "lambda_policy" {
20+
name = "LambdaPolicy"
21+
role = aws_iam_role.lambda_execution_role.id
22+
policy = jsonencode({
23+
Version = "2012-10-17",
24+
Statement = [
25+
{
26+
Effect = "Allow",
27+
Action = [
28+
"logs:CreateLogGroup",
29+
"logs:CreateLogStream",
30+
"logs:PutLogEvents"
31+
],
32+
Resource = "arn:${data.aws_partition.current.partition}:logs:*:*:*"
33+
},
34+
{
35+
Effect = "Allow",
36+
Action = "sns:Publish",
37+
Resource = aws_sns_topic.sns_topic.arn
38+
}
39+
]
40+
})
41+
}
42+
resource "aws_sns_topic" "sns_topic" {
43+
name = "s3-lambda-topic"
44+
}
45+
resource "aws_lambda_function" "sns_lambda" {
46+
function_name = "sns-lambda"
47+
handler = "lambda_function.lambda_handler"
48+
runtime = "python3.12"
49+
role = aws_iam_role.lambda_execution_role.arn
50+
filename = "lambda_function_payload.zip"
51+
source_code_hash = filebase64sha256("lambda_function_payload.zip")
52+
environment {
53+
variables = {
54+
SNS_TOPIC_ARN = aws_sns_topic.sns_topic.arn
55+
}
56+
}
57+
}
58+
resource "aws_cloudwatch_log_group" "lambda_log_group" {
59+
name = "/aws/lambda/sns-lambda"
60+
retention_in_days = 7
61+
}
62+
resource "aws_lambda_permission" "allow_eventbridge" {
63+
statement_id = "AllowEventBridge"
64+
action = "lambda:InvokeFunction"
65+
function_name = aws_lambda_function.sns_lambda.function_name
66+
principal = "events.amazonaws.com"
67+
source_arn = aws_cloudwatch_event_rule.s3_create_bucket_rule.arn
68+
}
69+
resource "aws_cloudwatch_event_rule" "s3_create_bucket_rule" {
70+
name = "s3-create-bucket-rule"
71+
description = "Rule to capture S3 bucket creation events"
72+
event_pattern = jsonencode({
73+
source = ["aws.s3"],
74+
"detail-type" = ["AWS API Call via CloudTrail"],
75+
detail = {
76+
eventSource = ["s3.amazonaws.com"],
77+
eventName = ["CreateBucket"]
78+
}
79+
})
80+
}
81+
resource "aws_cloudwatch_event_target" "s3_create_bucket_target" {
82+
rule = aws_cloudwatch_event_rule.s3_create_bucket_rule.name
83+
arn = aws_lambda_function.sns_lambda.arn
84+
}
85+
output "lambda_function_arn" {
86+
value = aws_lambda_function.sns_lambda.arn
87+
}
88+
output "sns_topic_arn" {
89+
value = aws_sns_topic.sns_topic.arn
90+
}
91+

0 commit comments

Comments
 (0)