← Cloud Practitioner study path

Technology and services · 12 min

Containers and AWS Lambda

In this lesson you will learn what a container is, what Amazon ECR, Amazon ECS, Amazon EKS and AWS Fargate are for, how AWS Lambda works and its limits, and how to choose where your code runs based on how much you want to manage.

Besides EC2 instances, which are computers you rent on AWS, AWS offers other ways to run your code, and each one leaves you a different part of the work. With an EC2 instance you pay while it is running, and you also take care of the , its and deciding how many instances there are. At the other end is AWS Lambda, where you don't pay for compute on a day when your code doesn't run. Between those two ends are containers and AWS Fargate. For example, an online store can have its website on EC2 and, at the same time, use Lambda for a small job that only runs when a new photo is uploaded.

How much do you manage with each option?

Who manages each layer on your own server, EC2, Fargate and Lambda

The compute options can be ordered by how many layers AWS manages for you. A layer is each part of what makes your code work: the building and the machines, the operating system, the program that runs the code and the code itself. Going from your own server to EC2, to Fargate and to Lambda, AWS takes on more layers at each step, so you have less to manage and also less control. For example, with its own server the store buys the machine and plugs it in, with EC2 it no longer buys machines but it updates the operating system, and with Lambda it only writes its code.

What is a container?

A container is a package that carries your code together with the it needs to work, but without an operating system of its own, because the containers that run on the same machine share that machine's operating system. That is why a container is lighter than a and starts without waiting for a full operating system to load. For example, the store packages the program that calculates the shipping cost in a container, and that container starts in seconds.

Because the container carries everything the code needs, it runs the same way on your laptop, in the test environment and in production, which is where real customers use it. Also, the machine where the containers run can be an EC2 instance, so using containers doesn't force you to leave EC2. For example, the store's developer tests the shipping cost container on their laptop, and that same container, unchanged, later runs on an EC2 instance for customers.

Container services on AWS

Flow of an image to Amazon ECR, the orchestrator and EC2 or Fargate

AWS has four services for working with containers, and each one covers a different part:

  • Amazon ECR (Elastic Container Registry), which stores your container images in private or public repositories and can scan them for . An image is the package each container comes from, like a mold that identical copies come out of.
  • Amazon ECS (Elastic Container Service), which is the AWS container orchestrator, that is, the service that decides where and how many containers run.
  • Amazon EKS (Elastic Kubernetes Service), which is managed , where AWS operates the and you use the same Kubernetes tools you would use anywhere else.
  • AWS Fargate, which is serverless compute for containers, works with ECS and with EKS, and lets you run containers without provisioning or scaling virtual machines, that is, without setting up machines or deciding how many are needed.

These services are used in order: first you build the image and push it to ECR, and then the orchestrator pulls it and runs it. Finally, you decide where the containers run, which can be on EC2 instances that you manage or on Fargate, where AWS takes care of the servers. For example, the store pushes the shipping cost image to ECR, ECS starts three containers from that image and Fargate provides the machines they run on.

ECS or EKS?

Between ECS and EKS, the difference lies in the tool the team uses. If the team already uses Kubernetes, the option is EKS, and if it wants to get started without learning Kubernetes, the option is ECS. For example, a store whose team has never used Kubernetes starts with ECS, while a bank that already organizes its containers with Kubernetes on its own machines moves to EKS and keeps using the same tools.

Match each service with what it does

0 of 4 pairs

Tap a service and then what it does.

0 of 4 pairs.

What is serverless?

AWS defines serverless as a model where the provider provisions, scales and maintains the servers, and you deploy your code, that is, upload it so it starts working. The servers still exist, but AWS manages them and you don't see them. AWS Fargate, for containers, and AWS Lambda, for code, are serverless services. For example, the store uploads its code to Lambda and never chooses a machine, never updates an operating system and never decides how many machines it needs.

AWS Lambda

AWS Lambda runs your code when something happens, without you provisioning or managing servers. That something that happens is called an event, and the piece of code that runs is called a function. With Lambda you pay only for the compute time you consume, and while your code isn't running there is no charge.

A photo uploaded to Amazon S3 triggers a Lambda function that creates a thumbnail

For example, when someone at the store uploads a product photo to an S3 bucket, that event triggers the function that creates the thumbnail, the small version of the photo that appears in the product list. If a thousand photos arrive at the same time, Lambda runs many copies of the function in parallel, and if none arrive, nothing runs. Besides S3, a function can be triggered from Amazon API Gateway, Amazon SQS, Amazon EventBridge and more than 200 services.

Lambda limits

Lambda has two limits that decide whether a task fits in the service:

  • Duration, where an invocation, which is each time the function runs, lasts at most 900 seconds, that is, 15 minutes, so a process that takes an hour in a single run doesn't go on Lambda. For example, creating a thumbnail takes little time and fits, but a report that takes an hour doesn't.
  • Memory, which is what you choose instead of , from 128 MB to 10,240 MB, and with which CPU grows, so with 1,769 MB the function has the equivalent of one vCPU. For example, if the thumbnail function is slow, you give it more memory and with that it also gets more CPU.

True or false?

0 of 4
  1. A process that takes an hour in a single run can run on Lambda.

  2. If nobody uploads photos to the bucket on a given day, the thumbnail function doesn't charge you for compute that day.

  3. In Lambda you choose how many vCPUs your function has.

  4. If a thousand photos arrive at the same time, Lambda runs many copies of the function in parallel.

Other compute options

Other AWS compute services: Amazon Lightsail, AWS Elastic Beanstalk and AWS Batch, each with its use

Besides EC2, containers and Lambda, AWS has three compute services for more specific cases:

  • Amazon Lightsail, which gives you virtual servers, containers and databases at a predictable monthly price and is the simplest way to set up a site or a small app. For example, a school puts the website where it posts its announcements there and knows how much it will pay each month.
  • AWS Elastic Beanstalk, which takes your web application's code and creates the instances, the , the scaling (adding or removing instances based on visits) and the health monitoring (checking that each instance responds). For example, the store's developer uploads their code and Beanstalk sets up everything needed for the website to work.
  • AWS Batch, which runs batch jobs at any scale, that is, large tasks that are processed all at once and don't have to respond right away. For example, a bank calculates the interest on all its accounts every night with Batch.

Where does it run?

0 of 6

Tap the correct column for each case.

  1. You need full control of the operating system

  2. You want to run containers without managing servers

  3. Create a thumbnail every time someone uploads a photo to S3

  4. A simple WordPress site with a predictable monthly price

  5. A process that takes an hour in a single run and needs software installed on the server

  6. Code that shouldn't be charged for while it isn't running

How do you recognize each service on the exam?

In compute questions, look in the question for how much the company wants to manage, because "without managing servers" rules out EC2. These are the phrases that point to each service:

  • "Containers without managing servers", which points to Fargate.
  • "Kubernetes", which points to EKS.
  • "Store container images", which points to ECR.
  • "Respond to an event", "code that runs for a short time" or "don't pay when it isn't used", which point to Lambda, as long as the process lasts less than 15 minutes.
  • "Simple" together with "predictable monthly price", which points to Lightsail.
  • "Just upload the code" of a web application, which points to Elastic Beanstalk.

For example, if a question says that a photo app wants to send an email every time a user signs up and doesn't want to pay when nobody signs up, the phrase points to Lambda.

cloud path tip: if you use an account with the new AWS experience, Lambda, ECS, EKS, ECR, Lightsail, Elastic Beanstalk and Batch are included in the free plan.

Review question

A team packages its application in containers. It doesn't want to patch operating systems or decide how many instances the cluster needs. What should it use to run the containers?

Next