Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion integration/combination/test_function_with_s3_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ def test_function_with_s3_bucket_trigger(self):
# There should be only One notification configuration for the event
self.assertEqual(len(configurations), 1)
config = configurations[0]
self.assertEqual(set(config["Events"]), {"s3:ObjectCreated:*"})
self.assertEqual(config["Events"], ["s3:ObjectCreated:*"])

def test_function_with_s3_bucket_intrinsics(self):
self.create_and_verify_stack("combination/function_with_s3_intrinsics")

s3_client = self.client_provider.s3_client
s3_bucket_name = self.get_physical_id_by_type("AWS::S3::Bucket")
configurations = s3_client.get_bucket_notification_configuration(Bucket=s3_bucket_name)[
"LambdaFunctionConfigurations"
]

self.assertEqual(len(configurations), 1)
config = configurations[0]
self.assertEqual(config["Events"], ["s3:ObjectCreated:*"])
self.assertEqual(config["Filter"]["Key"]["FilterRules"], [{"Name": "Suffix", "Value": "object_suffix"}])
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"LogicalResourceId": "MyLambdaFunction",
"ResourceType": "AWS::Lambda::Function"
},
{
"LogicalResourceId": "MyLambdaFunctionRole",
"ResourceType": "AWS::IAM::Role"
},
{
"LogicalResourceId": "MyLambdaFunctionS3EventPermission",
"ResourceType": "AWS::Lambda::Permission"
},
{
"LogicalResourceId": "MyBucket",
"ResourceType": "AWS::S3::Bucket"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Conditions:
MyCondition:
Fn::Equals:
- true
- false

Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
CodeUri: ${codeuri}
MemorySize: 128

Events:
S3Event:
Type: S3
Properties:
Bucket:
Ref: MyBucket
Events: s3:ObjectCreated:*
Filter:
Fn::If:
- MyCondition
- S3Key:
Rules:
- Name: prefix
Value: object_prefix
- S3Key:
Rules:
- Name: suffix
Value: object_suffix

MyBucket:
Type: AWS::S3::Bucket
2 changes: 1 addition & 1 deletion samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class S3(PushEventSource):
principal = "s3.amazonaws.com"
property_types = {
"Bucket": PropertyType(True, is_str()),
"Events": PropertyType(True, one_of(is_str(), list_of(is_str()))),
"Events": PropertyType(True, one_of(is_str(), list_of(is_str())), False),
"Filter": PropertyType(False, dict_of(is_str(), is_str())),
}

Expand Down
23 changes: 23 additions & 0 deletions tests/translator/input/error_function_invalid_s3_event.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Parameters:
EventsParam:
Type: String
Default: s3:ObjectCreated:*

Resources:
FunctionInvalidS3Event:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip
Handler: hello.handler
Runtime: python2.7
Events:
S3Event:
Type: S3
Properties:
Bucket:
Ref: MyBucket
Events:
Ref: EventsParam

MyBucket:
Type: AWS::S3::Bucket
40 changes: 40 additions & 0 deletions tests/translator/input/s3_intrinsics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Parameters:
EventsParam:
Type: String
Default: s3:ObjectCreated:*

Conditions:
MyCondition:
Fn::Equals:
- true
- false

Resources:
ThumbnailFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/thumbnails.zip
Handler: index.generate_thumbails
Runtime: nodejs12.x
Events:
ImageBucket:
Type: S3
Properties:
Bucket:
Ref: Images
Events:
- s3:ObjectCreated:*
Filter:
Fn::If:
- MyCondition
- S3Key:
Rules:
- Name: Rule1Prefix
Value: Rule1Value
- S3Key:
Rules:
- Name: Rule2Prefix
Value: Rule2Value

Images:
Type: AWS::S3::Bucket
130 changes: 130 additions & 0 deletions tests/translator/output/aws-cn/s3_intrinsics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"Conditions": {
"MyCondition": {
"Fn::Equals": [
true,
false
]
}
},
"Parameters": {
"EventsParam": {
"Default": "s3:ObjectCreated:*",
"Type": "String"
}
},
"Resources": {
"Images": {
"Type": "AWS::S3::Bucket",
"Properties": {
"NotificationConfiguration": {
"LambdaConfigurations": [
{
"Function": {
"Fn::GetAtt": [
"ThumbnailFunction",
"Arn"
]
},
"Filter": {
"Fn::If": [
"MyCondition",
{
"S3Key": {
"Rules": [
{
"Name": "Rule1Prefix",
"Value": "Rule1Value"
}
]
}
},
{
"S3Key": {
"Rules": [
{
"Name": "Rule2Prefix",
"Value": "Rule2Value"
}
]
}
}
]
},
"Event": "s3:ObjectCreated:*"
}
]
}
},
"DependsOn": [
"ThumbnailFunctionImageBucketPermission"
]
},
"ThumbnailFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"ThumbnailFunctionImageBucketPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"SourceAccount": {
"Ref": "AWS::AccountId"
},
"FunctionName": {
"Ref": "ThumbnailFunction"
},
"Principal": "s3.amazonaws.com"
}
},
"ThumbnailFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.generate_thumbails",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "thumbnails.zip"
},
"Role": {
"Fn::GetAtt": [
"ThumbnailFunctionRole",
"Arn"
]
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
}
}
}
Loading