Now let's talk
about a problem we might be facing here.
This public IP address changes every time
we deploy an updated container.
And that's, of course, not really ideal.
Now, that's why I did create that load balancer.
If you go to Services and you search
for application load here,
you should find EC2 as a suggestion
because that's where load balancers are managed.
We don't use EC2 instances here
but the load balancer feature belongs to that
and Fargate also utilizes it.
So here under Load Balancers,
you should find the load balancer we created.
And here, we find the DNS name,
which looks very much like a domain.
We can copy that,
enter it in the browser
and send a request to /goals.
However, you will notice that this won't work here
for a couple of reasons.
For one, I made a tiny mistake
in the target groups of that load balancer.
If we go back to ECS,
you will notice that on your services,
the tasks connected to the service
are stopped and restarted all the time.
And that stopping and restarting
is done by our load balancer
because that load balancer is checking the health
of the services it's redirecting requests to
and it shuts down a service
and restarts it if finds that this service is unhealthy.
For example, if it's not responding anymore.
That generally is a good behavior
because it ensures that if our application should crash
for whatever reason, it's restarted.
However, unfortunately, I made a tiny mistake
when we set up this load balancer.
On the target groups area of that EC2 service page,
you should find the target group we created earlier.
There under Health check settings, click on Edit.
And here, by default, the load balancer
is sending a request to our deployed service slash nothing.
Now, in our case, we are handling requests to /goals
but not to slash nothing.
Therefore, requests sent to slash nothing
will respond with a 404 error,
which is treated as unhealthy.
Therefore, we should change this health check path to /goals
so that the request is sent to an endpoint
of our service, which will respond
with a success code.
Now, that's one thing we've gotta change
to ensure that our load balancer will work correctly.
Another thing which I accidentally messed up a bit
is on the load balancer itself
in the basic configuration here,
the security groups I added,
I added this default security group.
Now, actually I wanna add this goals security groups here,
not the default one.
So here, make sure you add your security group
besides that default security group
so that the security group we're also using
for our ECS services is also attached here
and then click Save.
Now, all these changes will take a couple of seconds
or minutes to have an effect
but thereafter, our services should not be stopped
and restarted all the time anymore
or our tasks here, I should say.
And we should be able
to also use this URL here,
which we find on the load balancer page
to send a request to our deployed services
and to our running tasks.
And that means that we now can send a request
to the same URL, to the same domain
without caring about changing IP addresses.
Of course, you can also map your own custom domain
to this one if you want to
and the AWS documentation is the place to go
if you wanna learn more about that.
So if we now wait for all this deployment
to finish up here,
and we wait for this service to be restarted one more time,
and therefore, after a couple of minutes,
we will be able to send the request
to this domain /goals.
And of course, we can also use this in Postman now
instead of the IP address
and we can add a new goal.
We can, of course, then also get our goals.
Oops, I had a typo there.
We can get our goals
and of course, if we want to,
we can also delete a goal there.
Just make sure you grab the correct goal ID,
plug it into the URL
and here we go.
So this is now all working
and that's how we could deploy multiple containers
with AWS ECS
and how we can also use such a load balancer
to then ensure that we have a unchanging domain,
which points at these deployed containers.