I had an app setup a couple years ago and this worked smoothly, without using CloudWatch or AWS Events. I would push a built docker image from Semaphore CI to my ECR, and my EC2 Auto Scaling Group would immediately terminate the current EC2 instance and start a new one running the new image.
Returning to my project after a couple years it seems I can still push to ECR but the ASG does not restart with a new EC2 instance using the new image.
How is it possible to achieve this behavior? Here is the same question asked previously but the answer seems much more involved than is necessary or than what I was using before (which I can’t quite remember 🙂
How to let EC2 instance know when new image pushed to ECR
— edit
I have realized I was using EBS to manage the EC2s! I believe that was responsible for handling the new images. Still looking into this.
2
Answers
Answer for anyone reading this I realized I had been using EB to manage the EC2 infrastructure. You push a docker image to ECR, and then make a Dockerrun.aws.json file with the image's url and zip that and push it to an S3 bucket. Then you run:
aws elasticbeanstalk create-application-version
andaws elasticbeanstalk update-environment
.More details in: https://docs.semaphoreci.com/examples/deployment-to-aws-elastic-beanstalk/
I assume you have a userdata to pull the latest Docker image and run it when the EC2 instance was booted. By doing so, each time you spin up a new instance, it will run the latest version of your Docker image.
Also, you may already set up an EventBridge to handle ECR push event.
The latest thing would be how you trigger an ASG Instance Refresh event when the EventBridge is triggered. I am guessing you have a Lambda to trigger the ASG Instance Refresh for you.
To summarise, you can have a new EC2 instance using the latest Docker image by following the steps: