-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional event attachment #214
Comments
Closing in favor of #142 |
This one was created as a new Feature-Request based on the discussion in #142 .
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 |
Ran across this with an AWS::Serverless::Function using EventSource - unable to conditionally apply it. Would be great to see. |
+1 |
+1 |
Issue was opened in 2017 |
+1 |
3 similar comments
+1 |
+1 |
+1 |
As a workaround, you can use AWS::Events::Rule |
+1 |
1 similar comment
+1 |
How do you add an |
It's the other way around. As one of the ExampleRule:
Type: AWS::Events::Rule
Properties:
EventBusName: !Ref ExampleEventBus
EventPattern: # the pattern to match, or use `ScheduleExpression` instead
Targets:
- Arn: !GetAtt ExampleFunction.Arn
Id: Example # But use a unique value, natch. And it's there in the resource that one can specify optionality. Either by |
@chrisoverzero if the event emitter is an SNS topic, is there any difference between creating an AWS::SNS::Subscription between the topic and the function as opposed to using AWS::Events::Rule? |
I'm afraid I must not understand your question correctly. Are you asking whether you can use a rule to subscribe to a topic? SNS doesn't emit messages onto EventBridge buses, EventBridge does. An |
The proposed workaround doesn't seem to work, at least not for SNS events. I believe it's the same issue as this one, even though the OP was asking about EventBridge. Here's the relevant parts of my template:
With the above resources, the subscription was created and confirmed only when TestEventsTopicArn was set to a non-empty string, as desired, but the function was never invoked. Viewing the function in the Lambda console, it had no associated triggers. I suspected the issue might have been the lack of a ":live" alias at the end of the endpoint ARN and updated the subscription like so:
This correctly added the ":live" alias to the subscription endpoint but the function but had the same issue, i.e. no events or triggers. In the end I had to revert to defining the event source on the function itself:
Of course, the problem now is I can't conditionally turn the subscription off, same as the OP. |
Not entirely sure but can this request not already be solved: Resources:
BucketEventConsumer:
Type: AWS::Serverless::Function
Properties:
Handler: BucketEventConsumer.main.lambda_handler
Runtime: python3.6
CodeUri: bundle.zip
Events:
!If
- NeedsSomeBucket
-
CreateMetaEvent:
Condition: NeedsSomeBucket
Type: S3
Properties:
Bucket: !Ref SomeBucket
Events: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: suffix
Value: meta.json
- !Ref "AWS::NoValue" The yaml is a little uggly. But is is similar to Haven't testes this yet. The way described above could be syntactic sugar then. |
Using the @aws: It's been more than 4.5 years since this feature was requested. Please implement a solution for this! |
I had a similar issue trying to conditionally add a Schedule event to a SAM function. I post here all the relevant parts for a workaround (including the necessary permission to invoke the function) in case anyone needs it. Parameters:
TargetFunctionSchedule:
Type: String
Default: ""
Conditions:
EnableSchedule: !Not [ !Equals [ !Ref TargetFunctionSchedule, "" ] ]
Resources:
TargetFunction: # function whose execution may be scheduled or not
Type: AWS::Serverless::Function
Properties: ...
FunctionScheduleRule:
Condition: EnableSchedule
Type: AWS::Events::Rule
Properties:
ScheduleExpression: !Ref TargetFunctionSchedule
Targets:
Id: InvokeFunction # an arbitrary name
Arn: !GetAtt TargetFunction.Arn
FunctionSchedulePermission:
Condition: EnableSchedule
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref TargetFunction
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt FunctionScheduleRule.Arn |
aws/serverless-application-model#214 のIssueの通り、コミット時点での AWS SAMでは、Lambdaのイベントソース指定時にFn::Ifを利用できない。 そこで、別リソースに分割することで、イベントソース生成に条件を付けられる ようにする。
aws/serverless-application-model#214 のIssueの通り、コミット時点での AWS SAMでは、Lambdaのイベントソース指定時にFn::Ifを利用できない。 そこで、別リソースに分割することで、イベントソース生成に条件を付けられる ようにする。
This is now doable. Save the following as Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
Parameters:
NeedBucket:
Type: String
Conditions:
NeedBucketCondition: !Equals [!Ref NeedBucket, "yes"]
Resources:
MyBucket:
Condition: NeedBucketCondition
Type: AWS::S3::Bucket
MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
Handler: foo
InlineCode: bar
Events:
S3Event:
Fn::If:
- NeedBucketCondition
- Type: S3
Properties:
Bucket: !Ref MyBucket
Events: s3:ObjectCreated:*
- !Ref AWS::NoValue Then deploy with the S3 event:
Or without the S3 event:
Adding |
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
I'd like to draw your attention to the following crucial lines:
The text was updated successfully, but these errors were encountered: