ECS Services#
Finally! Time to build the ECS Service that ties everything together, and answers these questions:
- Where do we place our tasks?
- How do we scale them?
- How do we connect to the load balancer?
Once we create an ECS service with load balancer settings, ECS automatically registers tasks with the target group as they start.
Assignment#
Create an ECS service patientping-svc that runs your task definition and connects it to the load balancer (cluster patientping-ecs, ALB patientping-alb, target group patientping-tg).
Cost check: An ECS service creates the serverless containers (Fargate tasks). It costs about $0.012/hr for the task. The load balancer we created earlier costs about $0.0225/hr (~$16/mo). Combined, you’re looking at roughly $0.0345/hr or about $25/mo total! Don’t leave these up if you’re going to step away from the lessons.
Create the ECS service from the console:
In the AWS Console, navigate to
Elastic Container Service, openClusters, then click onpatientping-ecs.On the cluster page, go to the Services tab and click Create.
Under Service details, set:
- Family (task definition) to
patientping-ecs. - Service name to
patientping-svc.
- Family (task definition) to
Under Environment, use Capacity provider strategy with the
FARGATEcapacity provider.Under Networking:
Choose the
patientpingVPCSelect both public subnets (
patientping-public-aandpatientping-public-b)Select the
patientping-internalsecurity group, and set Public IP to Turned on.Now you may wonder why we’re using a public subnet. Well, when a new Fargate container starts, it needs to be able to interact with the container repository (ECR) and pull in a fresh container. Putting this in a public subnet means it can just leverage our internet gateway, and save us from setting up a specific endpoint to fetch them privately.
Under Load balancing, enable Application Load Balancer, “use existing”:
- Container:
patientping-ecs - Use existing load balancer:
patientping-alb - Use existing listener:
HTTP:80(the one that routes traffic to the target group) - Use existing target group:
patientping-tg
- Container:
Create the service.
It may take a minute or two for the service to start tasks and register them with the load balancer. Once they’re registered, your containers will be ready to receive traffic through the load balancer.
Once its online, verify the service by testing the endpoint:
In the Services tab of the
patientping-ecscluster, confirm thatpatientping-svcshows1/1running tasks.In the AWS Console, navigate to
EC2→Load Balancers, selectpatientping-alb, and copy the DNS name.Visit
http://<DNS_NAME>in your browser. (If it hasn’t come online yet, you should get a503from the ALB).Once online, confirm that the service serves up a
Hello from the container! From Dr. Strangelove.response.Remember, we set this service up with permissions to read the
/CMO_NAMEfrom SSM parameters!