So to add MongoDB,
we simply add another container
to this task definition on which we are working here
and give this a name of our choice, for example, MongoDB.
The image name is just Mongo here
because that is an official image on Docker Hub.
And as I explained before,
AWS ECS will automatically look
for image names you specify here on Docker Hub.
So we just use it like this.
Now, if we're to port mappings,
we wanna map port 27017 which is the default port
the MongoDB image exposes.
It is the port on which we also connect here
in our connection string.
So that's the port we wanna map here.
And then if we scroll down,
we can leave everything the way it is.
But under environment variables,
I of course wanna pass in the environment variables
which are defined in mongo.env.
So here I will grab the first environment variable
for the default username and set this to max,
and grab the password environment variable
and set that to secret here to ensure that we set
these two environment variables inside of our container.
Now, if we scroll down further,
we can ignore the network settings,
but for storage and logging,
we of course, do we have a volume here
in our docker-compose-yami file
which ensures that data persists.
Now at the moment, I will leave it like that.
I will not add anything here in this setup,
but it will come back to this later.
For the moment, let's keep that storage
and logging part empty and add to this container like this.
Now with that, we can leave
all the other settings the way they are and click create.
And now this creates this new task definition
with our two containers inside of them.
With that created,
we can now launch a service based on this task,
which then actually launches these containers
with the defined configuration.
So for that, we go back to clusters,
our goals-app cluster and under services,
we click create.
Here, choose Fargate as a launch type
and under task definition,
choose that goals task definition we just worked on.
You can give this serve as a name,
for example, goals_service,
under a number of tasks we choose one here.
We can keep the deployment type and then click next step.
Now here on this page,
at the top you should have a VPC which you can select.
So select this VPC, and under subnets,
simply add both subnets
which you should be able to choose from here.
Make sure auto-assign public IP is enabled
so that we'll be able to reach this
with help of a public ID.
And now actually under load balancing,
choose application load balancer.
This will ensure that incoming traffic is handled
in an efficient way, but it will also help us
with assigning a custom domain later if we want to.
Hence, I'll choose application load balancer here.
Now it won't find a load balancer by default.
So go to the EC2 Console as it prompts you here.
As a side note, if you use the wizard, the first run wizard
which you saw earlier, it would create one automatically,
but here on this side,
simply create an application load balancer,
give it any name of your choice,
I'll name it ecs-lb for load balancer.
It should be a internet-facing one
so that it can be reached from the internet.
It should definitely expose port 80
so make sure you have that listener here,
and it should be connected to the same VPC
you chose as a VPC here on your service.
This ensures that the load balancer
is working on the same network.
We can also check these two boxes here
and click next, configure security settings.
Now for the moment, we can leave this empty.
You could look into the docs you're pointed at here
to ensure that you actually use HTTPS.
But our Node application is not using HTTPS,
so I will leave it the way it is here.
Click on configure security groups
and there, selected existing security group,
and choose the security group which you should find here.
Click on configure routing, and there under target group,
give it any name of your choice
and choose IP as a targets type.
This is required since we're using Fargate
for our container, for running our containers,
and click register targets.
Now here under registered targets,
you don't need to do anything.
AWS ECS will automatically register running containers
as targets here, and therefore we click next review,
and create this load balancer.
Now, once this was created,
and by the way, I will say that, of course,
these are all features which might incur costs.
So you might wanna check out the AWS pricing pages
before you continue with all of that.
Now with that created, we can go back to ECS,
and then here, refresh the list of load balancers
and choose the load balancer you just created.
Choose that goals-backend 80:80 port mapping here
which should be added to the load balancer,
and add it to that target group
we already created in advance.
Alternatively, of course, you can create a new one.
Choose any name of your choice then.
And with all of that, we can click next step here,
ignore auto scaling here and click next step.
Auto scaling, just as a side note,
could of course be configured to ensure
that for incoming traffic spikes,
more and more simultaneous instances off your container
are being executed to be able to handle
all these spikes without delay.
It's an advanced feature and therefore,
I'll not dive into it here.
If you are planning to deploy bigger
and realistic applications on AWS,
you will need to dive deeply
into their documentation anyways.
So therefore, we got a basic setup here which we can use,
and now we can click create service
and this should now set up everything we need
in order to be able to run these containers.
So on our cluster here, the goals-app,
we now have a service.
We got tasks here, and if you click on this service,
we see the tasks associated with that service.
And if we click on that task,
we of course see that public ID again.
We also see the containers that belong to that task,
and you see that at the moment,
they're still starting up here.
So let's wait for that to finish and let's ensure
that both containers are up and running
before we then try working with them.
Now they are both up and running
and now we can use the public IP address
to visit this address slash goals,
to fetch all the goals stored in our database.
And we get back an empty goals array here initially,
of course, but that proof standard works
because otherwise, we would be getting an error.
Now of course we can now use Postman,
this tool which allowed us to send requests to APIs,
to test the other API endpoints as well.
And I wanna send a post request to my IP slash goals
and add a body which should be in raw format
and then JSON format, which has a text field
where I add a first test as content.
And if I click send, this is being sent
and I get back my goal here.
So that's looking very good.
If I now send a Get request to my IP slash goals,
I see that goal here.
And if I try deleting it by sending a Delete request
to slash goals slash the ID of that goal
which we can find here in the response from before,
I hopefully also can delete that goal.
It says it did so, and indeed it's missing.
So that now proves that all our containers
were successfully deployed with AWS ECS,
now working on a multi-container app.