{"id":4813,"date":"2022-09-02T09:00:00","date_gmt":"2022-09-02T07:00:00","guid":{"rendered":"https:\/\/blog.besharp.it\/?p=4813"},"modified":"2022-09-05T14:14:04","modified_gmt":"2022-09-05T12:14:04","slug":"building-a-deno-serverless-application-using-lambda-custom-runtime-cdk","status":"publish","type":"post","link":"https:\/\/blog.besharp.it\/building-a-deno-serverless-application-using-lambda-custom-runtime-cdk\/","title":{"rendered":"Building a Deno Serverless Application using Lambda Custom Runtime & CDK"},"content":{"rendered":"\n
Welcome back to Proud2beCloud, the blog by beSharp<\/a>! Today we\u2019ll be exploring some capabilities offered by the Cloud with a focus on customization<\/strong> while always relying on Infrastructure-as-Code (IaaC)<\/strong>, Serverless<\/strong>, and managed services<\/strong>. We\u2019ll talk about Lambda, CDK, pipelines, Serverless Repository, and more.<\/p>\n\n\n\n AWS Lambda Functions (FaaS) are great and versatile tools of the AWS Cloud that can be useful in many different situations and applications.<\/p>\n\n\n\n But how can we leverage them if we want to leverage a programming language that is not already supported? The answer is easy: Custom Runtime<\/strong>.<\/p>\n\n\n\n We\u2019ve already seen in one of our previous articles<\/a> how to create an application C++ on AWS Lambda. Now, two years later, we want to present a new version of the article choosing another vastly talked about a programming language: Deno<\/strong>.\u00a0<\/p>\n\n\n\n We\u2019ll also focus on how to automate the delivery of our application on Lambda thanks to pipelines CI\/CD and CDK<\/strong>.<\/p>\n\n\n\n As previously stated, if you are an assiduous reader of our blog, you probably already know about the possibility to use custom runtime on Lambda<\/a>.<\/p>\n\n\n\n To quickly summarize: Lambda with a custom runtime differs from a \u2018standard\u2019 lambda function because it must contain, other than the source code of the application, all the libraries needed for the execution and to resolve dependencies. Furthermore, if the language is interpreted, the interpreter is also needed in the package. <\/p>\n\n\n\n Using a custom runtime for AWS Lambda guarantees the elasticity<\/strong> of the proposed solutions for the resolution and optimization of the problem that might require a specific programming language.<\/p>\n\n\n\n The previous article described in detail how to create a custom runtime from scratch defining all the bootstrap scripts needed. In Today\u2019s article, we\u2019ll simplify the process further through the use of ready-to-use and reusable elements<\/strong> when possible.<\/p>\n\n\n\n In fact, for the most part of programming languages, it is likely that a custom runtime already exists and is ready to use or at least a good starting point for a custom solution. They are usually shared via Git or other sharing platforms. In our case, we\u2019ll use an existing runtime of Deno.<\/p>\n\n\n\n Deno<\/a> is a runtime for Javascript<\/strong>, Typescript<\/strong>, and WebAssembly<\/strong> based on JS Engine and Rust that has the role of both runtime and package manager. With Deno it is not necessary to have a separate package manager.<\/p>\n\n\n\n Deno aims to be a secure and reliable scripting environment, intuitive for the developer.<\/strong> It is open-source under an MIT license.<\/p>\n\n\n\n Like Node.js, Deno focuses on an event-driven architecture<\/strong> and can be used to create web servers, execute scientific computation, and other solutions.<\/p>\n\n\n\n Deno differs from Node.js in various aspects:<\/p>\n\n\n\n It should be noted that this is not intended to be a Node.js-Deno comparative article. Node.js as a runtime continues to be an excellent choice ensuring a minimum application code time-to-market and a very fast development experience when compared to that of other programming languages.<\/p>\n\n\n\n <\/p>\n\n\n <\/p>\n\n\n\n The scope of the article is to explain and demonstrate how to create a simple project with lambda with custom runtime, all while keeping a IaaC approach<\/strong>. On AWS for Infrastructure as a Code, we use AWS CDK. <\/p>\n\n\n\n AWS Cloud Development Kit (CDK)<\/a> is a framework for software development that allows the usage of languages like Typescript and Python, with the advantages that come with them, to describe and define the resources needed for the cloud infrastructure. CDK translates the file written in the language of choice in Cloudformation autonomously at deployment time.<\/p>\n\n\n\n The language chosen for this PoC is DENO.<\/p>\n\n\n\n For the image of the Deno runtime, we\u2019re gonna use the AWS Serverless Application Repository<\/a>. <\/p>\n\n\n\n AWS Serverless Application Repository, as the name might suggest, is a managed repository for Serverless applications that allows teams and developers to share reusable applications other than enabling them to assemble and deploy pieces of infrastructure already written in a simple and effective manner.<\/p>\n\n\n\n It is possible, in fact, to use pre-existent applications directly from the Serverless Application Repository in your own architecture, avoiding duplicating work and saving time. <\/p>\n\n\n\n Every application is packaged with an AWS Serverless Application Model (AWS SAM) template, which defines the resources used. The publicly shared applications include a link to the source code of the application.<\/p>\n\n\n\n For the solution described in this article, we\u2019re leveraging the AWS Serverless Application Repository for a direct pull of the necessary scripts to set up the Deno custom Runtime. In this way, there\u2019s no need to redefine all the bootstrap scripts:<\/p>\n\n\n\n Let\u2019s get into it! <\/p>\n\n\n\nCustom runtime on Lambda<\/h3>\n\n\n\n
What\u2019s Deno<\/h3>\n\n\n\n
Although still at the beginning of its journey, Deno offers some very interesting opportunities that we will investigate along these lines.<\/p>\n\n\n\n
Let’s start with sandboxing<\/strong>, one of its most important features. Deno is secure by default. There are no already given networks, files, or accesses: all types of access require explicit enabling.
In the image below, you can see the various types of sandboxing supported by Deno.<\/p>\n\n\n\n
https:\/\/medium.com\/deno-the-complete-reference\/sandboxing-in-deno-b3d514d88b63
<\/figcaption><\/figure><\/div>\n\n\nAWS Cloud Development Kit (CDK)<\/h3>\n\n\n\n
const denoRuntime = new CfnApplication(this, \"DenoRuntime\", {\n location: {\n applicationId:\n \"arn:aws:serverlessrepo:us-east-1:390065572566:applications\/deno\",\n semanticVersion: \"1.24.3\",\n },\n});<\/code><\/pre>\n\n\n\n
Hands on<\/h3>\n\n\n\n