CloudFormation add trigger for existing s3 bucket

Question:

My goal is to pack my lambda code which is invoked on each image upload to bucket, into CloudFormation template. I achieved so far to create new resources, and trigger from scratch, but I have existing bucket to which I need to add trigger and get errors in 2 cases:

  1. when I put creation of lambda and trigger configuration in one template and try to create stack as new resources – it says that bucket already exists
  2. when I move trigger to new file – and first create new resources like in 1., then I import existing resources into the stack – I get:

There was an error creating this change set

You have modified resources [ScaleImages, ScaleImagesRole] in your template that are not being imported. Update, create or delete operations cannot be executed during import operations.

My templates looks like:

  • lambda creation – new lambda and role – create stack with new resources

  • adding trigger – bucket exists – Import resources

In last one I tried also "Function": "ScaleImages", but in both cases I had same error about:

modified resources [ScaleImages, ScaleImagesRole] in your template

Can somebody shed some light on what I’m doing wrong?

Answer:

You have to do it in stages:

1. Create new stack

No bucket yet, just stack with your function and lambda permissions which you are missing.

2. Import bucket to existing stack

Use Import resources into stack option and upload stack using this template. It adds bucket, but no notifications yet

3. Update stack

Update stack by adding notifications to the bucket. Use the following template:

Leave a Reply