Serverless-application-model: Conditional event attachment

Created on 27 Nov 2017  路  13Comments  路  Source: aws/serverless-application-model

If conditional resource creation is eventually supported you might also have to support conditional events as in the following example where in some environments a bucket might not already exist and so it can be created and the event attached to the lambda. While in other environments the bucket may already exist and the trigger needs to be manually defined because cloudformation doesn't yet use existing resources.

For example

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: Lambda that responds to S3 events

Parameters:
    PreExistingBucket:
        Description: "Does an existing bucket exist (not managed by cloudformation)"
        Type: String
        Default: 'no'
        AllowedValues:
            - 'yes'
            - 'no'
        ConstraintDescription: must specify yes or no.

Conditions:
  NeedsSomeBucket: !Equals [ !Ref PreExistingBucket, 'no' ]

Resources:
    BucketEventConsumer:
        Type: AWS::Serverless::Function
        Properties:
            Handler: BucketEventConsumer.main.lambda_handler
            Runtime: python3.6
            CodeUri: bundle.zip
            Events:
                CreateMetaEvent:
                    Condition: NeedsSomeBucket
                    Type: S3
                    Properties:
                        Bucket: !Ref SomeBucket
                        Events: "s3:ObjectCreated:*"
                        Filter:
                            S3Key:
                                Rules:
                                    -
                                        Name: suffix
                                        Value: meta.json
    SomeBucket:
        Condition: NeedsSomeBucket
        Type: "AWS::S3::Bucket"
        Properties:
            BucketName: 'some-bucket-somewhere'
        DeletionPolicy: Retain

I'd like to draw your attention to the following crucial lines:

                CreateMetaEvent:
                    Condition: NeedsSomeBucket

Can this already be acheived using Fn::If?

arecloudformation priorit2-important stagpm-review typfeature

Most helpful comment

Any update on this? I've run into a similar use case and would like to use this feature as well.

All 13 comments

Closing in favor of #142

This one was created as a new Feature-Request based on the discussion in #142 .

@ glenveegee That is an interesting thought and idea! Would you mind create a new issue with this use case? It will help us prioritize this in the future and will ensure it does not get lost or forgotten about in this issue (since they are slightly different).

Closing this in favor of #142 doesn't make sense. Both of these are separate and useful features.

Any update on this? I've run into a similar use case and would like to use this feature as well.

There are currently higher priority items we are focusing on. I've increased the priority label for visibility during planning and hope to get to this one along with #142 soon

Thanks for the feedback @brettstack !

@brettstack Any update on this? I would like to conditionally enable or disable the event rule. I tried also using mapping but it seems it doesn't work either... so I'm wondering which kind of intrinsic functions (see doc) are supported for real inside SAM template. Is there any other way to conditionally enable events? Thank you!

@made2591-eb We have passed this along to our product team for possible prioritization. Please +1 on the feature to help with prioritization.

+1
I appreciate if you prioritise this issue. I have the exactly same issue as #1229 and have to manually disable a CloudWatch event once it is deployed in a staging environment. Thank you!

Ran across this with an AWS::Serverless::Function using EventSource - unable to conditionally apply it. Would be great to see.

+1

+1
This will help lessen a lot of cost savings and manual maintenance in dev stages.

Issue was opened in 2017
Soon 2021 arrives
Can you prioritize a bit more?

+1

Was this page helpful?
0 / 5 - 0 ratings