Skip to content

Commit f2a87b0

Browse files
committed
Added additional S3 Event blueprints
1 parent 8160398 commit f2a87b0

8 files changed

Lines changed: 226 additions & 3 deletions

File tree

PowerShell/Module/Templates/Blueprints/S3Event/s3event.ps1.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#
99
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement
1010
# indicating the module and version.
11-
#
12-
# The following link contians documentation describing the structure of the S3 event object.
11+
#
12+
# The following link contains documentation describing the structure of the S3 event object.
1313
# https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html
1414

15-
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.335.0'}
15+
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.428.0'}
1616

1717
# Uncomment to send the input event to CloudWatch Logs
1818
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This sample creates a Lambda function written in PowerShell that can be subscribed
2+
to an SNS Topic. For this sample, the SNS Topic would be subscribed to S3 Events,
3+
for when a new object, or object version is created in a bucket.
4+
5+
For example: S3 Event -> SNS Topic -> Lambda Function.
6+
7+
The script uses a cmdlet from the AWS Tools for PowerShell module
8+
(AWSPowerShell.NetCore) to read the object size and version and output them to
9+
the function logs.
10+
11+
The script has a Requires statement for the latest version of the AWS Tools for
12+
PowerShell module. If you modify this example to not need cmdlets from that
13+
module you can safely delete this statement.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# PowerShell script file to be executed as a AWS Lambda function.
2+
#
3+
# When executing in Lambda the following variables will be predefined.
4+
# $LambdaInput - A PSObject that contains the Lambda function input data.
5+
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
6+
#
7+
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
8+
#
9+
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement
10+
# indicating the module and version.
11+
#
12+
# The following link contains documentation describing the structure of the S3 event object.
13+
# https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html
14+
#
15+
# This example demonstrates how to process an S3 Event that follows the process:
16+
# S3 Event -> SNS Topic -> Lambda Function
17+
18+
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.428.0'}
19+
20+
# Uncomment to send the input event to CloudWatch Logs
21+
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
22+
23+
foreach ($snsRecord in $LambdaInput.Records)
24+
{
25+
$snsMessage = ConvertFrom-Json -InputObject $snsRecord.Sns.Message
26+
foreach ($s3Event in $snsMessage.Records)
27+
{
28+
$bucket = $s3Event.s3.bucket.name
29+
$key = $s3Event.s3.object.key
30+
31+
Write-Host "Processing event for: bucket = $bucket, key = $key"
32+
33+
# TODO: Add logic to handle S3 event record, for example
34+
$obj = Get-S3Object -Bucket $bucket -Key $key
35+
Write-Host "Object $key is $($obj.Size) bytes"
36+
}
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
This sample creates a Lambda function written in PowerShell that can be subscribed
2+
to an SQS Queue. For this sample, the SQS Queue would be subscribed to an SNS Topic,
3+
which would be subscribed to S3 Events, for when a new object, or object version
4+
is created in a bucket.
5+
6+
For example: S3 Event -> SNS Topic -> SQS Queue -> Lambda Function.
7+
8+
The SNS Subscription can be configured for "Raw Message Delivery" or not, this sample
9+
is configured to handle both options.
10+
11+
The script uses a cmdlet from the AWS Tools for PowerShell module
12+
(AWSPowerShell.NetCore) to read the object size and version and output them to
13+
the function logs.
14+
15+
The script has a Requires statement for the latest version of the AWS Tools for
16+
PowerShell module. If you modify this example to not need cmdlets from that
17+
module you can safely delete this statement.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# PowerShell script file to be executed as a AWS Lambda function.
2+
#
3+
# When executing in Lambda the following variables will be predefined.
4+
# $LambdaInput - A PSObject that contains the Lambda function input data.
5+
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
6+
#
7+
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
8+
#
9+
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement
10+
# indicating the module and version.
11+
#
12+
# The following link contains documentation describing the structure of the S3 event object.
13+
# https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html
14+
#
15+
# This example demonstrates how to process an S3 Event that follows the process:
16+
# S3 Event -> SNS Topic -> SQS Queue -> Lambda Function
17+
18+
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.428.0'}
19+
20+
# Uncomment to send the input event to CloudWatch Logs
21+
#Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
22+
23+
foreach ($sqsRecord in $LambdaInput.Records)
24+
{
25+
$sqsRecordBody = ConvertFrom-Json -InputObject $sqsRecord.body
26+
27+
try
28+
{
29+
# If this call works, then the SNS Subscription is configured with
30+
# "Raw Message Delivery" = "True"
31+
$snsMessage = ConvertFrom-Json -InputObject $sqsRecordBody.Message
32+
}
33+
catch
34+
{
35+
# If we hit the catch statement, then the SNS Subscription is configured
36+
# with "Raw Message Delivery" = "False"
37+
$snsMessage = $sqsRecordBody
38+
}
39+
40+
if ($snsMessage.Records.Count -gt 0)
41+
{
42+
# We have an array of SNS Records, lets process them
43+
44+
foreach ($s3Event in $snsMessage.Records)
45+
{
46+
$bucket = $s3Event.s3.bucket.name
47+
$key = $s3Event.s3.object.key
48+
49+
Write-Host 'Processing event for:' (ConvertTo-Json -InputObject @{Bucket = $bucket; Key = $key} -Compress)
50+
51+
# TODO: Add logic to handle S3 event record, for example
52+
$obj = Get-S3Object -Bucket $bucket -Key $key
53+
Write-Host "Object $key is $($obj.Size) bytes"
54+
}
55+
}
56+
else
57+
{
58+
# We likely have an S3 Test Event, write it out to logs
59+
Write-Host 'SNS Message:' (ConvertTo-Json -InputObject $snsMessage -Compress)
60+
}
61+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This sample creates a Lambda function written in PowerShell that can be subscribed
2+
to an SQS Queue. For this sample, the SQS Queue would be subscribed to S3 Events,
3+
for when a new object, or object version is created in a bucket.
4+
5+
For example: S3 Event -> SQS Queue -> Lambda Function.
6+
7+
The script uses a cmdlet from the AWS Tools for PowerShell module
8+
(AWSPowerShell.NetCore) to read the object size and version and output them to
9+
the function logs.
10+
11+
The script has a Requires statement for the latest version of the AWS Tools for
12+
PowerShell module. If you modify this example to not need cmdlets from that
13+
module you can safely delete this statement.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# PowerShell script file to be executed as a AWS Lambda function.
2+
#
3+
# When executing in Lambda the following variables will be predefined.
4+
# $LambdaInput - A PSObject that contains the Lambda function input data.
5+
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
6+
#
7+
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
8+
#
9+
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement
10+
# indicating the module and version.
11+
#
12+
# The following link contains documentation describing the structure of the S3 event object.
13+
# https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html
14+
#
15+
# This example demonstrates how to process an S3 Event that follows the process:
16+
# S3 Event -> SQS Queue -> Lambda Function
17+
18+
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.428.0'}
19+
20+
# Uncomment to send the input event to CloudWatch Logs
21+
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)
22+
23+
foreach ($sqsRecord in $LambdaInput.Records)
24+
{
25+
$sqsRecordBody = ConvertFrom-Json -InputObject $sqsRecord.body
26+
foreach ($s3Event in $sqsRecordBody.Records)
27+
{
28+
$bucket = $s3Event.s3.bucket.name
29+
$key = $s3Event.s3.object.key
30+
31+
Write-Host "Processing event for: bucket = $bucket, key = $key"
32+
33+
# TODO: Add logic to handle S3 event record, for example
34+
$obj = Get-S3Object -Bucket $bucket -Key $key
35+
Write-Host "Object $key is $($obj.Size) bytes"
36+
}
37+
}

PowerShell/Module/Templates/Blueprints/ps-lambda-blueprint-manifest.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,51 @@
9191
}
9292
]
9393
},
94+
{
95+
"name": "S3EventToSNS",
96+
"description": "Script to process SNS Records triggered by S3 events",
97+
"content": [
98+
{
99+
"source": "s3tosns.ps1.txt",
100+
"output": "{basename}.ps1",
101+
"filetype": "lambdaFunction"
102+
},
103+
{
104+
"source": "readme.txt",
105+
"output": "readme.txt"
106+
}
107+
]
108+
},
109+
{
110+
"name": "S3EventToSNSToSQS",
111+
"description": "Script to process SQS Messages, subscribed to an SNS Topic that is triggered by S3 events",
112+
"content": [
113+
{
114+
"source": "s3tosnstosqs.ps1.txt",
115+
"output": "{basename}.ps1",
116+
"filetype": "lambdaFunction"
117+
},
118+
{
119+
"source": "readme.txt",
120+
"output": "readme.txt"
121+
}
122+
]
123+
},
124+
{
125+
"name": "S3EventToSQS",
126+
"description": "Script to process SQS Messages triggered by S3 events",
127+
"content": [
128+
{
129+
"source": "s3tosqs.ps1.txt",
130+
"output": "{basename}.ps1",
131+
"filetype": "lambdaFunction"
132+
},
133+
{
134+
"source": "readme.txt",
135+
"output": "readme.txt"
136+
}
137+
]
138+
},
94139
{
95140
"name": "SNSSubscription",
96141
"description": "Script to be subscribed to an SNS Topic",

0 commit comments

Comments
 (0)