Introduction to Begin: Create, build, & deploy applications at scale
Part Two: Less management, more serverless development with Begin functions
Developing applications with Node.js is great. However, the modern world requires building applications for hyper scalability if a business is to be sustainable. Getting an idea or a concept to market as quickly as possible can define the profitable future of an organization. It’s this process that Begin addresses and optimizes for with their platform.
Begin was created on the foundation of providing developers the ability to create, build, and deploy cloud applications at scale in the shortest time possible. Along with super fast CI/CD (continuous integration/continuous deployment) functionality, all applications deployed via Begin are powered by AWS, taking advantage of Lambdas and integrated CDNs to ensure the software you build is scalable and fast. With serverless technology coming into play, you only pay for what you use without having to worry about maintenance, which in the case of traditional servers. The agility to ship code quickly that can scale automatically, without being concerned with the underlying infrastructure, is the major value Begin offers.
We’ll see how Begin works by deploying a function that calls a third-party weather API to get relevant data based on the city specified in the query string parameter value of the URL.
The signup process involves a Github account, so if you don’t have one, an account must be created. At the Begin onboarding process, you can start by choosing a runtime, either Node.js or Deno (Deno, if you haven’t heard of, has a run time similar to Node.js but offers several advantages over Node. It is developed by the same creator who built Node.js. Learn more), and then it will subsequently deploy the application on Github for Begin to start managing.
Start your first deployment by selecting the ‘Hello world’ app. After a repository name is provided, Begin will build and deploy the code automatically with a production and staging URL. You can then clone the project to your local system for further development.
Understanding the project structure:
Underneath every Begin project is powered by a runtime library called Architect, that consists of a src folder, which optionally contains the following folders:
- An http folder housing individual directories of fully isolated, and independently deployable cloud functions. Note that each function has its independent dependencies and does not relate to your project’s dependency modules.
- A shared folder that can be used to provide reusable code across all your functions. It’s possible to include global dependency modules in this folder too.
- A views folder containing front-end code that can be shared only across all HTTP GET functions.
- The public directory storing all your deployable static assets that would be part of your CDN distribution.
- A tests folder managing all tests for your app.
With the hello world project, you will notice an http/get-index function that returns HTML content. Each function directory must contain an index.js that services the cloud function handler and related dependencies. In general, HTTP functions are responsible for handling synchronous requests.
Creating new functions:
We are going to slightly modify our existing hello-world project and also add another function.
Inside the get-index directory, add this HTML snippet to the body variable belonging to the handler file, index.js.
<a method="get" href="/weather?city=New York">Get Weather</a>
To provision a new HTTP function inside the app.arc manifest file add get /weather like this.
Run npm start, and you will notice some boilerplate code for the new automatically created handler. Stop the server, and run npm init inside the get-weather folder directory.
We do this because every function that Begin manages should have its own dependencies residing inside that folder. Install axios by running npm install axios
You can modify the function’s timeout and memory size inside the arc.config file which is present inside the get-weather folder.
The following snippet should be added inside the handler function contained within the get-weather directory. (The https://home.openweathermap.org/ API is used here for getting the weather details. The API key can be obtained after signing up)
The function takes in a city name as query string parameters and returns the weather details. By default, New York has been added at the index page link, you can modify the value at the URL to some other city.
For using environment variables locally, create .arc-env at the root of the project and add it like this:
Commit these changes to your github. Generally, with Begin, any commit done to github will trigger an automated deployment to the staging environment. Begin CI executes three default, non-configurable steps:
- verify — validates the repo payload from git and prep’s Begin’s infrastructure for a deployment.
- install — responsible for installing dependencies.
- deploy — deploys the cloud functions and static assets.
Deploying to production can only happen when staging is green. Production deployments can be done in two ways: 1) hitting the button on the top left corner at the Begin console or 2) from the local terminal by adding a git tag git tag -a 1.0.1 -m “Push to prod” followed by git push origin 1.0.1.
Environment variables for your deployments need to be manually added in the console to the corresponding environment you are working with. For our staging deployment, we need to add the weather API key for the function to work.
Begin is a great platform for building applications that work using serverless technology. Even your database requirements can be fulfilled quite simply since every Begin app comes bundled with Begin Data, a durable, easy to use, fully managed, SSD-based key-value and document database that’s free and in-network. With integrated CDNs, you can be sure you’re serving content in the quickest possible time. You don’t have to figure out the provisioning and deployment process because the platform handles that for you, thereby allowing you to focus more on the development.
Who is Serverless Guru?
Serverless Guru helps companies build scalable and cost-effective applications on the cloud. We help train companies on how to leverage IAC, serverless, and cloud-native services. We help migrate existing applications to the cloud and optimize existing applications on the cloud to be more cost-effective. We are a Serverless development partner and an AWS Consulting partner.
Serverless Developer — Serverless Guru
Twitter — @mohd_izzy
Thanks for reading 😃