Creating serverless functions using AWS Fargate

Introduction

Serverless architecture has gained popularity among developers over the past few years. It is an approach that allows developers to build and run applications without having to worry about server infrastructure. AWS Fargate is one of the many serverless computing services that AWS provides to developers. In this tutorial, we will be discussing how to create serverless functions using AWS Fargate.

Prerequisites

Before we begin, there are a few things that you will need:

  • An AWS Account
  • AWS CLI installed on your computer
  • Docker installed on your computer
  • Basic understanding of Docker

What is AWS Fargate?

AWS Fargate is a serverless computing service provided by AWS. It allows developers to run containers without having to manage the underlying servers. With Fargate, developers can focus on building and shipping containers to AWS, and AWS will take care of running and scaling the containers.

Creating a Docker Image

To get started with AWS Fargate, we need to create a Docker image. If you’re not familiar with Docker, it’s an open-source platform that allows developers to build, ship, and run applications as containers.

In this tutorial, we will be creating a Docker image that runs a simple Node.js application.

Create a new file in your favorite text editor and add the following code:

const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello, World!");
});

server.listen(8080, () => {
  console.log("Server running on port 8080");
});

Save this file as index.js in a new folder on your computer called app.

Next, create a new file called Dockerfile in the same folder and add the following code:

FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD [ "node", "index.js" ]

This Dockerfile sets up a basic Node.js environment, installs the dependencies for our app using npm, copies the files from our local app folder to the Docker image, exposes port 8080 for the container, and then starts the Node.js server.

Now that we have our Dockerfile and Node.js application, we can build our Docker image. Open up your terminal, navigate to the app folder, and run the following command:

docker build -t my-node-app .

This command will build a Docker image with the tag my-node-app.

Pushing the Docker Image to AWS ECR

Now that we have our Docker image, we need to push it to an AWS Elastic Container Registry (ECR). This is where we will store the Docker image so that AWS Fargate can use it to run our serverless function.

Before we can push our image to ECR, we need to create an ECR repository. To do this, navigate to the AWS Console, select “ECR”, and then click “Create repository”.

Give your repository a name and leave all the other settings as default. Once your repository has been created, take note of the repository URL. We will need this in the next step.

To push our Docker image to ECR, we need to authenticate our Docker client with AWS. To do this, run the following command in your terminal:

$(aws ecr get-login --no-include-email --region us-east-1)

Replace us-east-1 with the region that your ECR repository is located in.

Next, tag our Docker image with our repository URL:

docker tag my-node-app:latest <your-repo-url>/my-node-app:latest

Replace <your-repo-url> with the repository URL that you copied earlier.

Finally, push our Docker image to ECR using the following command:

docker push <your-repo-url>/my-node-app:latest

This will push the Docker image to ECR, and we can now use it to run our serverless function on AWS Fargate.

Creating an AWS Fargate Cluster

Now that our Docker image is stored in ECR, we can use it to run our serverless function on AWS Fargate. To do this, we need to create an AWS Fargate cluster.

To create a cluster, navigate to the AWS Console, select “ECS”, and then click “Clusters”. Select “Create cluster”, and then choose “Fargate” as the cluster type.

Give your cluster a name, choose the VPC and subnets that you want your cluster to run in, and leave all other settings as default. Click “Create” to create your cluster.

Creating an AWS Fargate Task Definition

Now that our Fargate cluster is created, we need to create a task definition. A task definition is a blueprint for our containerized application that contains information about the container image, memory requirements, CPU requirements, and more.

To create a task definition, navigate to your Fargate cluster in the AWS Console, select “Task Definitions”, and then click “Create new Task Definition”.

Choose “Fargate” as the launch type, give your task definition a name, and then click “Add container”.

In the “Container Definition” section, give your container a name, and then copy the name of your Docker image from ECR into the “Image” field. Set the “Memory Limits” to at least 512MB, and then click “Add”.

Once you have added your container, click “Create” to create your task definition.

Creating an AWS Fargate Service

Now that we have a task definition, we can create an AWS Fargate service. A service manages the running tasks in our cluster and ensures that there is always at least one task running.

To create a service, navigate to your Fargate cluster in the AWS Console, select “Services”, and then click “Create”.

Choose your task definition, give your service a name, and then set the number of tasks to 1. Click “Next Step”.

In the “Configure Network” section, choose the VPC and subnets that you want your service to run in, and then click “Next Step”.

In the “Configure Security Group” section, leave all settings as default, and then click “Create Service” to create your AWS Fargate service.

Testing the AWS Fargate Service

Now that our AWS Fargate service is up and running, we can test it to make sure that it’s working correctly.

To test our service, navigate back to the AWS Console, select “ECS”, and then select your Fargate cluster.

Under “Tasks”, click on the task that was created by your service. You will see an “External link” that you can click on to test your service.

Click on the “External link”, and you should see a “Hello, World!” message in your browser.

Conclusion

Congratulations! You have just created a serverless function using AWS Fargate! You now have a basic understanding of how to create a Docker image, push it to ECR, create a Fargate cluster, define a task, and create a Fargate service using AWS Fargate. With this knowledge, you can start building more complex applications using AWS Fargate!

Related Post