ECS - Updating service with new image uploaded to ECR

Hi!

I’m using ECS + Fargate for the first time. Uploaded a new version of my image on ECR (tagged ‘latest’) and I want to update the running task to use it.

For what I read, I should run “aws ecs update-service --cluster my-cluster --service my-service --force-new-deployment” and that’s it. Also I can do this through AWS portal, if I go to Service → Update.

Now my question: Do this procedure change the public IP my ENI is given? I have this doubt as I think -force-new-deployment kills the tasks and creates a new one, so another ENI is used.

I would need to mantain the public IP if it’s possible.

Are you using EC2, or Fargate? Task Networking, or not?

Hi! It’s in the description, I’m using Fargate. I’ve read some minutes ago that I would need to use Application Load Balancer, is it correct? Thanks!

Yes, afaik, the IP will change. Do you need the IP to stay so things can access your service or for your service to access other things?

I think the ENI is on the task, and a new task will be started and the old one removed.

Oops, sorry, I missed the Fargate part. I’ve never used Fargate so I don’t know for sure.

If your IP address is not an Elastic IP, then anything that removes the ENI will likely lose that address. I don’t know whether a task update would do that.

If the IP address is an Elastic IP, then you may still lose the association between the IP and the task but in a way which should be repairable afterwards.

If I were doing this and needed a static IP for inbound traffic, then I would get myself an Elastic IP, attach it to an Application Load Balancer, and use weighted routing to control which version of the service is publicly available. Using DNS instead of IP addresses would also help here so that the IP could change, but the hostname wouldn’t.

If this is for outbound traffic, then putting your Elastic IP on a NAT gateway may help, so that connections initiated by the ECS task always had the same source IP address. This would have to be combined with an ALB also, but the ALB would have a standard public IP (which may change) and you’d probably use DNS to get inbound traffic to your ALB without clients having to worry about what its IP is

IMO you are looking for pain if you expect the IP not to change (if it isn’t one you’ve allocated and have dedicated). Get away from that as fast as possible.

With Fargate, we’ve had the best success with using CloudFormation. We simply update the (versioned) Docker tag and it adds the new tasks and scales down the old ones. Virtually no downtime.

You are correct - there is no guarantee you will be issued the same public ip, and you should expect it to change.

If you want a consistent target you will need to use a load balancer. You can assign the target group in your ECS task definition.

Yes, I think that’s what happens.

I need my task to retain the IP so it can access other things, we allow that public IP to access our on-prem resources.

Thank you!!!

Thanks! Do you have any document that can help me adopting this implementation?

Keep in mind that load balancers are expensive, so if you’re only running one small ECS task then it may be cheaper to spin up a dedicated EC2 and use web hooks to redeploy when you push new images

https://github.com/1Strategy/fargate-cloudformation-example

  1. Create an Application Load Balancer
  2. Register a domain/sub domain with the ip of the LB, or just use the ip
  3. Create a new target group in the LB
  4. Set a rule in the LB to forward traffic to the target group
  5. Set the target group in your ECS task definition
  6. Profit

I have a single LB and many sub domains, with each sub domain forwarding to a different target group / ECS task

If your need for a public IP is because you’ve allowed it access to your on-prem resources, a load balancer won’t help you - those are for inbound traffic to your container as a web service.

If you need a fixed outbound IP, you’ll want to run your tasks in a private subnet, and set up a NAT gateway in a public subnet. The IP address of the NAT gateway will be the one that you allow into your on-prem resources.

Alternatively, you could set up a small EC2 instance in a public subnet to perform NAT if you don’t want to spend on the NAT gateway.

In this case you’ll need some form of NAT gateway. The AWS NAT Gateway’s can become quite expensive, but I’ve heard it’s possible to just run your own NAT instance on a t3.micro or similar lower cost instance.

Thank you so so much!!! Appreciate the steps a lot.

This is the way

It’ll cost you a few more bucks a month but it solves your problem.

And if you want static IPs in front of your ELB, a Global Accelerator is a cheap way to get two static IPs in front of it with a bonus of faster routing.

Wow TIL you can do Fargate without an ALB. The thought of using Fargate without an ALB in front never crossed my mind before. I guess it’s useful if you are only doing outgoing connections and don’t care about stuffs coming in.

I would maybe add on to this that nowadays for new stacks you’re probably better off using CDK. Especially ECS/FARGATE gets pretty complex in CF alone. It took me like a day to get up my first stack.