AWS Fargate a Lambda Function on Steroids

Building your Serverless Microservices without the limitation of AWS Lambda another popular Microservice, this guide will go through why AWS Fargate might be your next solution for your application, and how to implement using Docker and AWS Amplify.

Why use containers for your web app?

Containers by use case

The two primary container instance use cases for each deployment methodology. However we will only focus on ECS Fargate Tasks since we are looking for an alternative to a Lambda Function.

What distinguishes AWS Fargate with AWS Lambda is the fact that you can execute a serverless API with potentially high performance CPU/RAM and with no limit on the run-time, where as AWS Lambda currently has a run-time limit of 15 minutes.

Tasks

Similarly to AWS Lambda functions CRON Function provided by AWS CloudWatch Events, you are able to either schedule Fargate with either a scheduled execution or recurring rate.

For an alternative to the current Lambda function which executes once, you may use Fargate stand-alone tasks which does just that. Though to execute it securely you may want to use a lambda function to initialize the AWS Fargate by running ‘RunTask’ a ECS API Action.

Services

Using AWS Elastic Load Balancing you are able to accept incoming traffic, allowing for an active API that can be accessed anytime or a Hosted website that may be running on a Docker Container.

Deploying an Fargate instance in which ECS handles for scaling up depending on the demand for containers. The amount of scaling can be adjusted by the service parameters; Scheduled Scaling, Step Scaling Policies, and Target Tracking Scaling Policies are types of automatic scaling.

Setup AWS Fargate

We will be using AWS Copilot to manage the creation and deployment of container images, which can be later deployed by the initialization parameters or by the User using RunTask.

$ brew install aws/tap/copilot-cli
$ cd ../my-app
$ copilot app init

After installing copilot and initializing your project, a config folder is generated as shown

my-app/
copilot/
├─ my-app/
│ ├─ manifest.yml
├─ .workspace

With AWS Copilot you can deploy jobs to a specific environment, in this case test for running our application

  • manifest.yml

Understand that you must probably use a User Access/Secret since IAM does not seem to register with copilot automatically. The Task-Execution Role for your container instance (copilot job) should include your policies for executing your AWS services.

Create your docker application with the following format, unless you are familiar with docker application then you may structure your project as you please. For your Dockerfile you may use any this tutorial for node.js, or any other frame-work you prefer such as Go, C#, Python, etc.

AWS ECR Public Gallery offers official container images, though not required I do recommend since they are maintained regularly.

my-app/
├─ .Dockerignore
├─ Dockerfile
├─ package.json
├─ src/
│ ├─ index.js
│ ├─ app.js
  • Dockerfile

An example Dockerfile, though you may modify it. This Dockerfile includes AWS CloudWatch Logs to generate logs for potential debugging ( Highly Recommend). Don’t forget to add .Dockerignore so you don’t upload node_modules npm-debug.log .env etc.

FROM public.ecr.aws/bitnami/node:14
# FROM node:14
WORKDIR /usr/src/app## Enable AWS CloudWatch Logs for debian OS
RUN wget https://s3.us-east-2.amazonaws.com/amazoncloudwatch-agent-us-east-2/debian/amd64/latest/amazon-cloudwatch-agent.deb
RUN dpkg -i -E ./amazon-cloudwatch-agent.debCOPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

COPY . .

CMD [ "node", "src/index.js" ]
  • index.js

Since we are using stand-alone tasks, we do not need to create an express.js app as you would for an ECS Service or an AWS API Gateway-Lambda Function. This is because we plan to remove our container instance as soon as our application is done, whereas an ECS service would run 24/7 and require Load-Balancing for public facing networking.

This probably not the best implementation for a main function for our index.js file. However since we need to have Async/Await operations this will suffice.

  • Lambda Function & API Gateway
Not all code is included, usually inside a Form Submission function.

If you are using AWS Amplify this can be one way of initializing a AWS Fargate Instance. Otherwise use your preferred HTTPS request API.

This is a lambda function with an express.js template, though not all code is included. Note if you plan on passing on variables you must include the job name as shown in ‘ContainerOverrides’.

The Following code shows an example as to how to utilize ECS Action API runTask which initializes AWS Fargate executing the container image you uploaded with AWS Copilot.

Your code now can be deployed from either the end-user or through a scheduled function depending on your CRON settings.

You now have the ability to use a powerful version of what a lambda function could do and now execute long-performing batch jobs as such.

--

--

--

M.S. Electrical Engineer, Working as a Software Developer mainly with AWS.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

from Buffer

Amazing Flight Deals

Example-Guided, A Brief History

How to completely self-host Standard Notes

Six Pieces of Advice I Wish Someone Had Given Me When I Started Coding

Dancing with play/pause app to make your life easy…

2020 NFLPA Legits are Back!

try Helm for first time

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Isaac

Isaac

M.S. Electrical Engineer, Working as a Software Developer mainly with AWS.

More from Medium

AWS Lambda: A Serverless Framework

Serverless Data Engineering

Serverless 101

Creating an AWS Lambda Function for Matplotlib Data Visualizations