From a4fa49844d23afea90f7c9aa4616beedd2a80db8 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Wed, 24 Nov 2021 22:15:22 +0100 Subject: [PATCH] feat(AWS S3): Support `Fn::If` CF function for s3 event --- .../aws/package/compile/events/s3/index.js | 6 +++- lib/plugins/aws/provider.js | 2 +- .../package/compile/events/s3/index.test.js | 36 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/plugins/aws/package/compile/events/s3/index.js b/lib/plugins/aws/package/compile/events/s3/index.js index 099bdf508f2..947102a9f2d 100644 --- a/lib/plugins/aws/package/compile/events/s3/index.js +++ b/lib/plugins/aws/package/compile/events/s3/index.js @@ -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' }, diff --git a/lib/plugins/aws/provider.js b/lib/plugins/aws/provider.js index 9d4d1c894fe..a47738bd9c7 100644 --- a/lib/plugins/aws/provider.js +++ b/lib/plugins/aws/provider.js @@ -356,7 +356,7 @@ class AwsProvider { type: 'array', minItems: 3, maxItems: 3, - items: { type: 'string', minLength: 1 }, + items: { $ref: '#/definitions/awsCfInstruction' }, }, }, required: ['Fn::If'], diff --git a/test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js b/test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js index f0b49c9bbb1..620f9751b0c 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/s3/index.test.js @@ -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', @@ -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({