Introduction to Begin: Create, Build, & Deploy App at Scale

August 18, 2020

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.

Initialization:

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.

Deploy the first app

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.

get-index function

To provision a new HTTP function inside the app.arc manifest file add get /weather like this.

  
@app
begin-app
@http
get /
get /weather
@tables
data
scopeID *String
dataID **String
ttl TTL
  

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)

  
exports.handler = async function http (req) {
  let city = req.queryStringParameters.city;
  const axios = require('axios');
  
  let weather_api = process.env.WEATHER_API;
  let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${weather_api}`
  let res=await axios(url)
  

  return {
    headers:{
      'content-type':'application/json',
      'cache-control': 'no-cache, no-store, must-revalidate, max-age=0, s-maxage=0',
    },
    body:JSON.stringify(res.data)
  }
}
  

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:

  
@testing
WEATHER_API 
  

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.
Automated CI and deployment after github push

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.

Adding environment variable

Conclusion:

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.

Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones
Founder
Speak to a Guru
Edu Marcos
Chief Technology Officer
Speak to a Guru
Mason Toberny
Head of Enterprise Accounts
Speak to a Guru

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.