aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.67k stars 1.01k forks source link

Cloudwatch scheduled event naming conflicts with multiple stages #1691

Closed jrbeilke closed 3 years ago

jrbeilke commented 3 years ago

Have a Chalice app with multiple stages deployed to the same AWS account and running into an issue where scheduled events from one stage of the app are conflicting with events for another stage.

ie. dev stage lambda is api-dev-work_request_notifier and is tied to a work_request_notifier-event event in cloudwatch

then we deploy our prod stage and end up with

prod stage lambda is api-prod-work_request_notifier and is tied to the same work_request_notifier-event event in cloudwatch which clobbers the event for the dev lambda

Seems like Chalice should include the stage name when generating Cloudwatch events to avoid conflicts just like when naming the lambda functions

jamesls commented 3 years ago

Can you give me more details/steps to repro this issue? I'm not seeing this behavior when deploy a scheduled event to multiple stages. Here's what I tried:

from chalice import Chalice

app = Chalice(app_name='testsched')

@app.schedule('rate(5 minutes)')
def foo(event):
    return {'hello': 'world'}
$ chalice deploy --stage dev
...
$ chalice deploy --stage prod

Then confirm they're multiple events configured.

 $ aws events list-rules
{
    "Rules": [
        {
            "Name": "testsched-dev-foo-event",
            "Arn": "arn:aws:events:us-west-2:12345:rule/testsched-dev-foo-event",
            "State": "ENABLED",
            "ScheduleExpression": "rate(5 minutes)",
            "EventBusName": "default"
        },
        {
            "Name": "testsched-prod-foo-event",
            "Arn": "arn:aws:events:us-west-2:12345:rule/testsched-prod-foo-event",
            "State": "ENABLED",
            "ScheduleExpression": "rate(5 minutes)",
            "EventBusName": "default"
        }
    ]
}

I also am seeing the stage name being added to the event rule names. Were you running into something else?

no-response[bot] commented 3 years ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

dhay commented 2 years ago

I have this same issue. My scheduled event handler is defined like so:

@app.schedule(Rate(1, unit=Rate.HOURS))
def send_call_reminders(event):
     pass

When I package up my project chalice package --pkg-format terraform --stage dev ., I get the following resource created in my chalice.tf.json file:

  "resource": {
    "aws_lambda_function": { ... },
    "aws_lambda_event_source_mapping": { ... },
    "aws_cloudwatch_event_rule": {
      "send_call_reminders-event": {
        "name": "send_call_reminders-event",
        "schedule_expression": "rate(1 hour)",
        "description": ""
      }
    },

I would have exepcted the cloudwatch event rule to have a name that includes the current stage. To get around this, for now, I have to manually configure my lambda name like so:

@app.schedule(Rate(1, unit=Rate.HOURS), name=f"send_call_reminders-{ENV}")
def send_call_reminders(event):
    pass