So welcome to the first part
of this ECS demo. And to create an ECS service,
I first need to go ahead and create a task definition.
So let's create a new task definition
and task definition indicates how to create an ECS task.
So this one will be nginxdemos-hello.
And that's the name of the image I'm going to use.
So if you type nginxdemos-hello,
you find a docker image from dockerhub.
And this image we're going to be using.
So I can copy this right here.
So the name of this is
nginxdemos-hello.
And the image URI,
I have the one thing that I copy and pasted
including the slash.
So that's my first and only essential container
in this definition and it's going to create
an HTTP server that will be binded to port 80.
So I could have container port as 80.
I will not add any environment variables
and I will not add any more containers.
So I will leave everything as default.
Then for the app environment
I can choose either Fargate or Amazon EC2 instances.
So if we set Fargate, we're going to launch task directly
in serverless mode or Amazon instances,
we're going to launch a task on Amazon EC2 instances
that we launched before.
To keep things simple,
I'm going to use only the AWS Fargate mode right here.
Next we need to choose a system, so Linux is great.
And then a task size, so I can choose 0.5 CPU
as well as one gigabytes of RAM. Okay.
So this is how much CPU and RAM we allocate to each task.
We could go less, but this is a good idea.
And if you wanted to have more memory, of course
you could scroll down and add up to 30 gigabytes of memory,
for example. Okay. More importantly,
if that task was, for example
doing API calls into Amazon S3,
then we could add an IAM task role.
And this IAM task role would allow us to run API calls
against the AWS services.
But right now, because we're just running
a simple HTTP server, we don't need to do anything else.
In terms of storage, we can leave everything as is.
The task will come with 20 gigabytes
of ephemeral storage from Fargate, so we're good.
And in terms of everything else, we won't touch it.
So we review and create
and we have defined our first task definition family
called the nginxdemos-hello.
So let's create this.
And now the task definition is being created.
So next what I want to do is
to actually run this task definition in a service
behind a load balancer, and as such,
I need to define two kinds of security groups.
So if I go into security groups,
I'm going to create a new security group
and I will call this one sg-alb-ecs
which is my ALB for ECS SG.
And in inbound rule, I will allow to connect to port 80
from Anywhere- IPv6
and Anywhere- IPv4
to make sure I can connect.
Okay.
And then I will create this security group
and it cannot begin by this.
So alb-ecs-sg.
Perfect. So this security group has been created.
And the second security group I need to create
is the one for my ECS task.
So I'll call this one nginx-demo-sg
and we're going to allow inbound rule on anywhere
but coming from a specific security group
which is my alb-ecs-sg.
So allow traffic from the ALB, which is very similar setup
to what we had when we had EC2 instances.
Okay. And then I will choose all TCP.
So all TCP on the security group.
Okay. Let's create this one and SG for NGINX.
So now that I've created two security groups,
we can go ahead and create our first service.
So back into ECS, I will go in clusters,
find my DemoCluster, and then under services I will create
and deploy a new service.
So the deployment configuration is going to be
of type 'service' because we are launching
a long-running computing work that is a web application.
But if you wanted to start a standalone task that runs
and terminates, for example, a batch job,
then you would use the 'task' application type.
So we'll use service and we'll specify a family
which is the one we created, the nginxdemos-hello,
as well as a revision number, which is one, the latest.
Service name is going to be called nginxdemos,
and we for now want only one desired tasks.
For deployment options, we can leave this as default.
Now for load balancing, yes, we do want a load balancer
and we want an application load balancer to be working
and let's go ahead and create it.
So I'll call this one DemoALBForECS.
It's going to listen on port 80
and then we need to have a target group name.
So this one is gonna be called nginx-ecs
and the protocol as HTTP.
The health check path is slash,
and the health grace period is, let's say, 20 seconds.
For networking, so this is for our ALB right here,
and for networking, we can select a security group
that is already existing.
And the one that we are going to use
is going to be the nginx-demo-sg, which is a security group
that's going to be attached directly into our Fargate tasks.
And we'll leave public IP enabled and now click on deploy.
So what's going to happen now
is that our service is going to be deployed,
a load balancer is going to be created,
and then let's observe the results.
Okay, so my service has been deployed successfully.
Now let's have a look to see if everything is working.
So I'm gonna go under nginxdemos,
and as you can see, one task is active
and it looks like it was registered.
So if we go back into the EC2 management console
and go under load balancers,
there's my load balancer right here.
And if we look at the settings,
the security group that was attached to it is the wrong one,
it's the nginx-demo-sg.
But we want to attach the alb-ecs-sg
to be able to access our load balancer.
Okay. So now we assigned the correct security group.
Now let's go and open this URL for our load balancer.
And now it says, refuse to connect.
So that means that it's connecting to the ALB
but the ALB is not connecting to the ECS tasks.
So it took me a while to figure that one out.
But if you go to listeners,
somehow the listener ID was on port HTTP 79.
I'm not sure why, maybe it's a bug from the new UI of ECS.
But what I should be setting is port HTTP on port 80
right here in my listener detail. And that should fix it.
So let's save my changes.
Now, my ALB is connected to the right listener port
and let's go back in here and refresh this page.
And very soon, I should wait a little bit.
Here we go, perfect.
We are connected to the nginx web server
running on the ECS task.
And we get the server address, the binding port,
the server name, as well as the date right now.
And the URL we hit. So if we hit slash test,
we're going to get a slash test as well in here.
Okay. So that's pretty good.
So we have one ECS task running behind our load balancer
in our ECS service. Okay.
So if I go to services right here, it's there.
But I can click on services
and we can start scaling our service.
So I can do edits, and then instead of one task,
and this is going to cost you money if you do it by the way,
so if you don't want to spend a lot, then don't do this,
but I'm going to specify four tasks and update.
So now we're launching four tasks in our ECS services
and they're all running on Fargate.
So very, very quickly four tasks are going to
launch run at one out of four. Okay.
But very quickly three tasks are in the pending states.
And very, very soon, because it's running on Fargate,
it's going to be provisioned and run right away.
So this is the whole power of Fargate, it's serverless,
and we don't have to manage EC2 infrastructure
behind the scene to make it work.
So, as you can see now, one, two,
and then a last task is going to run very, very soon.
So as you can see, it's quite quick to scale an ECS service.
And what I can do now is go back to my load balancer
and refresh this page. And as I refresh this page,
as you can see, I am switching between my various ECS tasks
in the back end.
So it's working just like we did for EC2 instances
but now we run docker containers
and these docker containers can be whatever we want.
So super good. We have now four ECS task running.
And so if I refresh this page many times
I'm going to switch between four instances
which I find pretty awesome.
And we were able to, with a bit of struggle,
to launch an ECS service, ECS task,
link it to a load balancer, and so on.
So just to scale back this demo and save on cost,
I'm going to take my service, I will edit it,
and I will say I want zero number of tasks
as a specified number which will allow me to scale back
all my tasks back to zero, but still keep my setup
in case I need it later on.
So you can keep the service at zero tasks
and they all going to be terminated.
And you can keep the ALB for now,
if you have one ALB running in your account,
it doesn't cost you any money,
but if you have more than one, you could also delete it.
Okay. That's it for this lecture.
I hope you liked it. And I will see you in the next lecture.