Skip to content

Commit

Permalink
feat(AWS S3): Support Fn::If CF function for s3 event
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Nov 25, 2021
1 parent 44511f3 commit a4fa498
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/plugins/aws/package/compile/events/s3/index.js
Expand Up @@ -18,7 +18,11 @@ class AwsCompileS3Events {
type: 'object',
properties: {
bucket: {
anyOf: [{ type: 'string' }, { $ref: '#/definitions/awsCfFunction' }],
anyOf: [
{ type: 'string' },
{ $ref: '#/definitions/awsCfFunction' },
{ $ref: '#/definitions/awsCfIf' },
],
},
event: { type: 'string', pattern: '^s3:.+$' },
existing: { type: 'boolean' },
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/aws/provider.js
Expand Up @@ -356,7 +356,7 @@ class AwsProvider {
type: 'array',
minItems: 3,
maxItems: 3,
items: { type: 'string', minLength: 1 },
items: { $ref: '#/definitions/awsCfInstruction' },
},
},
required: ['Fn::If'],
Expand Down
36 changes: 36 additions & 0 deletions test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js
Expand Up @@ -865,6 +865,24 @@ describe('test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js', ()
},
],
},
withIf: {
handler: 'basic.handler',
events: [
{
s3: {
bucket: {
'Fn::If': [
'isFirstBucketEmtpy',
{ Ref: 'FirstBucket' },
{ Ref: 'SecondBucket' },
],
},
event: 's3:ObjectCreated:*',
existing: true,
},
},
],
},
},
},
command: 'package',
Expand Down Expand Up @@ -919,6 +937,24 @@ describe('test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js', ()
});
});

it('should support `bucket` provided as CF If function', () => {
expect(cfResources[naming.getCustomResourceS3ResourceLogicalId('withIf')]).to.deep.equal({
Type: 'Custom::S3',
Version: 1,
DependsOn: ['WithIfLambdaFunction', 'CustomDashresourceDashexistingDashs3LambdaFunction'],
Properties: {
ServiceToken: {
'Fn::GetAtt': ['CustomDashresourceDashexistingDashs3LambdaFunction', 'Arn'],
},
FunctionName: `${serverlessInstance.service.service}-dev-withIf`,
BucketName: {
'Fn::If': ['isFirstBucketEmtpy', { Ref: 'FirstBucket' }, { Ref: 'SecondBucket' }],
},
BucketConfigs: [{ Event: 's3:ObjectCreated:*', Rules: [] }],
},
});
});

it('should disallow referencing multiple buckets in context of single function with CF references', async () => {
await expect(
runServerless({
Expand Down

0 comments on commit a4fa498

Please sign in to comment.