Serverless Platform For Code Deployment

Easy Way To Create API For Some Task

Kunal Bharadkar
3 min readAug 4, 2021

Serverless computing is a cloud computing execution model in which the cloud provider allocates machine resources on-demand, taking care of the servers on behalf of their customers.

Serverless computing does not hold resources in volatile memory; computing is rather done in short bursts with the results persisted to storage.

When an app is not in use, there are no computing resources allocated to the app. Pricing is based on the actual amount of resources consumed by an application

But there are still servers in serverless, but they are abstracted away from app development. A cloud provider handles the routine work of provisioning, maintaining, and scaling the server infrastructure. Developers can simply package their code in containers for deployment.

Role of serverless computing in digital transformation:

Serverless computing plays an important part in digital transformation. First, it enables developers to be more productive by helping them focus on writing code that has business value, without having to worry about the underlying infrastructure that will support the code. Regardless of vertical industry or company size, a serverless computing strategy can help increase developer productivity by eliminating management overhead.

Features of a serverless computing software development environment include:

  • Zero server management
  • Auto-scaling to meet changing traffic demands
  • Managed integrated security

The major serverless computing vendors include:

  • Google Cloud Functions
  • AWS Lambda
  • IBM Cloud Functions
  • Microsoft Azure Functions

Business Problem:

Deployment of code for Automatic video edit and share the link of a video to a user

Solution:

Fetch a static file from cloud storage and make a video, then you have to store this video into Cloud Storage and share the link of data that store into Cloud Storage

Here I used Google Cloud Function for deployment

Requirements:

# Function dependencies, for example:
# package>=version
moviepy~=1.0.3
Pillow~=8.2.0
google-cloud-storage~=1.40.0
NumPy

For retrieving data from cloud storage I used the Google-cloud-storage library

Google Cloud Storage allows you to store data on Google infrastructure with very high reliability, performance, and availability and can be used to distribute large data objects to users via direct download.

I used the bucket from where fetch files and save them back

client = storage.Client()
bucket = client.get_bucket("video_editor_data")

How to fetch data from cloud storage path into Cloud Function

bucket.list_blobs(prefix=’Project_video_editing/output/’):

Fetch image from URL into Cloud Function

response = requests.get(url, stream=True)
img2 = Image.open(io.BytesIO(response.content))

Fetch font type from Cloud Storage

# Custom font style and font size
font= bucket.blob("Project_video_editing/HELVETICAINSERATLTSTD-ROMAN.OTF")
myFont = ImageFont.truetype(io.BytesIO(font.download_as_bytes()),60)

Here I used tmp memory of cloud function where I can download static file & save it Because of this no need to download the static file every time

mp4_1= bucket.blob("Project_video_editing/part1.mp4")
mp4_1.download_to_file(open("/tmp/part1.mp4", "wb"))
clip1 = VideoFileClip("/tmp/part1.mp4")

The problem which I face while doing this, there is no direct way to save files into cloud storage from cloud function.

Small hack, I created a temporary file in tmp memory for write and pass it

temp_output="/tmp/outputx.mp4"
with open(temp_output, 'w') as fp:
pass

By changing the pointing direction of OS & save the file there with name.mp4 into tmp memory

finl.write_videofile("/tmp/testing.mp4",temp_audiofile=temp_output)

Then finally upload this file into Cloud Storage from tmp memory

output_blob = bucket.blob("name_of_save_file.mp4")output_blob.upload_from_filename("/tmp/testing.mp4")

Use trigger URL as API for hitting

With the serverless model, a developer can write a function in his or her favorite programming language and posts it to a serverless platform. The cloud service provider manages the infrastructure and the software and maps the function to an API endpoint, transparently scaling function instances on demand.

Conclusion:

Serverless Store consists of both app and functions. It is a hybrid, featuring a standard (traditional, if you may) Python flask web app as the centerpiece, with many critical and supplementary features served by Cloud Functions. For people who prefer the FaaS pattern, with a relatively small amount of effort, it is possible to make Serverless Store purely function-based; you can also easily revert it back to a web app with no Cloud Function workers at all, ready for VM deployment.

You can find the whole code here

Automatic video editing using Python

Thank you for reading this article…

--

--