# Cloud Computing

Although you can build and host your own server, it is more common to use large-scale hosted systems instead.

Cloud computing is the delivery of computing services—including servers, storage, databases, networking, software, analytics, and intelligence—over the internet (“the cloud”) to offer economies of scale (https://azure.microsoft.com). Cloud computing as a concept has been around since the 1960s, but it has only become popular since around 2007, when Amazon launched its Elastic Compute Cloud (EC2) service.

Rosenberg & Mateos (2014) define cloud computing as "a model for enabling ubiquitous, convenient, on-demand network access to a shared pool of configurable computing resources (e.g., networks, servers, storage, applications, and services) that can be rapidly provisioned and released with minimal management effort or service provider interaction" (https://en.wikipedia.org/wiki/Cloud_computing).

# Characteristics

They also define five essential characteristics of cloud computing:

  • Pooled computing resources available to any subscribing users
  • Virtualized computing resources to maximize hardware utilization
  • Elastic scaling up or down according to need
  • Automated creation of new virtual machines or deletion of existing ones
  • Resource usage billed only as used

This represents a major shift away from previous models of computing, where companies would purchase and maintain their own servers, and instead rent resources from large-scale providers.

There are many advantages of using a hosted services, relative to building your own solution:

  1. Cost. You don't need to purchase and setup hardware, but can instead allocate services as you need them. This allows you to just pay for what you need, and scale up if demand increases.
  2. Flexibility: You can allocate just the services that you need, and add different services as your needs change.
  3. Scalability: You can add more resources on-demand, as needed. Systems can even do this automatically, e.g. as more users login to your application, more servers are launched automatically to meet demand.
  4. Efficiency: These systems are extremely large-scale and finely tuned to provide excellent performance. You will likely not have the resources to design and build something comparable yourself.

# Features

So what does a cloud service provide? Here are some common features:

  • Compute: Virtual machines, containers, serverless computing. Commonly, you can deploy code the cloud, and the cloud will run it for you. This is often called "serverless" computing, because you don't need to worry about the server that runs your code. We will discuss this when we build services with Firebase.

  • Storage: Object storage, databases, file storage. Typically, some type of scalable database is provided, as well as storage for files and other binary objects. This will often be a NoSQL database, since these are easier to scale.

  • Networking: Content delivery networks, load balancing, virtual private networks. In the context of users accessing your services or databases, you will need to manage network traffic. This can be done through load balancing, or by using a content delivery network to cache data closer to users.

  • Security: Identity and access management, encryption, firewalls. These services will all have sophisticated user management and security features. You can control who has access to your services, and how they can interact with them.

  • Advanced Features: Machine learning, Big Data, Internet of Things. Platforms are increasingly offering advanced services, such as machine learning, that you can leverage directly in your cloud-based applications. This can be a huge advantage, as you can use these services without needing to build them yourself.

# Service Providers

Today, there are many cloud solutions, each offering similar functionality, including Amazon Web Services (AWS), Google Cloud (GCP), Google Firebase, and Microsoft Azure.

The main questions you should ask when choosing a cloud provider are:

  1. What services do you need? Different providers offer different services, and you should choose the one that best fits your needs. In this course, you will need database support, and user authentication. You may also need compute i.e. the ability to deploy a service to the cloud.
  2. What platform are you developing for? Some services are better suited to mobile applications, others to web applications. Firebase, for instance, is a great choice for mobile applications, but doesn't have official support for desktop (Kotlin Multiplatform) - at least at the time of writing.

We'll review getting started with AWS and Firebase in the sections below. You are also welcome to use other services if you wish.

# Amazon Web Services (AWS)

AWS consists of approximately 300 web services, each with distinct capabilities. Popular services include:

  • Storage (S3)
  • Compute (EC2, Lambda)
  • Networking (Route53)
  • Security (IAM)
  • Big Data (DynamoDB)
  • Machine Learning (SageMaker)

Developers can access AWS services through the AWS console (web page), CLI, or through a programmatic interface (SDK). There are native language SDKs for many different programming languages, including Java, Python and Kotlin.

# Kotlin SDK

The AWS SDK for Kotlin is meant to provide idiomatic Kotlin functionality for accessing AWS services.

  • Kotlin syntax and conventions
  • Kotlin types and null-safety
  • Kotlin multiplatform (JVM, Android)
  • Coroutine support

AWS dependencies can be added to any existing Kotlin project. AWS is huge, and you are expected to identify and import just what you require, based on your services.

The following video also provides a great introduction to the SDK.

# Google Firebase

Firebase is a mobile and web application development platform, built on top of Google Cloud (GCP). If offers several services, including authentication, a real-time database, object storage and push messaging.

There are advantages of Firebase compared to other hosted solutions:

  • Rapid implementation: you can get up and running very quickly.
  • Cost efficient for small projects: free tier, and low cost for small projects.
  • Cloud storage: storage for binaries e.g. images with API support.
  • Real-time database: NoSQL database with an API.
  • Great platform support for Android, iOS, Web, Flutter.
  • Well-documented on the Firebase documentation site.

Disadvantages of Firebase:

  • Vendor lock-in: it's difficult to migrate to a different solution.
  • Costly for large projects: it's a managed solution, which makes it easy to setup and monitor, but costlier than GCP. You typically would migrate to GCP for larger projects.
  • Database limits: the NoSQL database only allows queries on a single key value i.e. it returns full records only, and cannot process queries. It's much less flexible than a SQL database in processing complex data.

This is a great introductory video for Firebase:

# Definitions

A Firebase Project is a container for your applications, and resources or services that are provisioned for your use. All registered applications have access to the same resources and services.

For example, you could have both iOS and Android applications registered to your project, and provision access to authentication and storage for both of them.

Firebase Projects
Firebase Projects

A Firebase project is actually just a Google Cloud project that has additional Firebase-specific configurations and services enabled for it. -- FIrebase Documentation

This means that:

# Creating a Project

You can setup a project in the Firebase Console. Define how users will authenticate, what applications exist and what services you will require.

Firebase Console
Firebase Console

There are multiple billing options. The Spark Plan is a free-tier that is probably sufficient for what you'll do in this course. You should probably select that to avoid getting billed!

Firebase project identifiers

A Firebase project can be identified in the Firebase backend and in various developer interfaces using different identifiers, including the project name, the project number, and the project ID.

  • When you create a project, you provide a project name. This identifier is the internal-only name for a project in theFirebase console, the Google Cloud console, and the Firebase CLI.
  • A Firebase project (and its associated Google Cloud project) has a project number. This is the Google-assigned globally unique canonical identifier for the project. Use this identifier when configuring integrations and/or making API calls to Firebase, Google, or third-party services.
  • A Firebase project (and its associated Google Cloud project) has a project ID. This identifier should generally be treated as a convenience alias to reference the project.

Firebase config files and objects

When you register an app with a Firebase project, the Firebase console provides a Firebase configuration file (Apple/Android apps) or a configuration object (web apps) that you add directly to your local app directory.

  • For Apple apps, you add a GoogleService-Info.plist configuration file.
  • For Android apps, you add a google-services.json configuration file.
  • For web apps, you add a Firebase configuration object.

A Firebase config file or object associates an app with a specific Firebase project and its resources (databases, storage buckets, etc.). The configuration includes "Firebase options", which are parameters required by Firebase and Google services to communicate with Firebase server APIs:

  • API key: a simple encrypted string used when calling certain APIs that don't need to access private user data (example value: AIzaSyDOCAbC123dEf456GhI789jKl012-MnO)
  • Project ID: a user-defined unique identifier for the project across all of Firebase and Google Cloud. This identifier may appear in URLs or names for some Firebase resources, but it should generally be treated as a convenience alias to reference the project. (example value: myapp-project-123)
  • Application ID ("AppID"): the unique identifier for the Firebase app across all of Firebase with a platform-specific format:
    • Firebase Apple apps: GOOGLE_APP_ID (example value: 1:1234567890:ios:321abc456def7890) This is not an Apple bundle ID.
    • Firebase Android apps: mobilesdk_app_id (example value: 1:1234567890:android:321abc456def7890) This is not an Android package name or Android application ID.
    • Firebase Web apps: appId (example value: 1:65211879909:web:3ae38ef1cdcb2e01fe5f0c)

# Configuring your Project

Once your project is created, you need to register your application. The full instructions to setup your application are here.

From the Firebase Console, open your project, click on Add App and select your platform.

Firebase Add App
Firebase Add App

We'll choose an Android application for this example. A wizard will walk you through each step.

Step 1: Register your application

Firebase Step 1
Firebase Step 1

Step 2: Add the config file to your Gradle project

Firebase Step 2
Firebase Step 2

Step 3: Add the Firebase SDK dependencies to your Gradle project

Firebase Step 3
Firebase Step 3

Step 4: Confirm registration in Firebase console

Firebase Step 4
Firebase Step 4

Make sure to click on the Gradle sync button to load your build.gradle.kts changes. Your Android project should now be able to connect to your Firebase project!

Detailed package and dependency information used to import Kotlin libraries can be found here.

# Adding Authentication

  1. Make sure to include the appropriate dependencies. By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.
dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:32.7.4"))

    // Add the dependency for the Firebase Authentication library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-auth")
}
  1. Enable authentication in the Firebase Console (``Build>Authentication>Sign in Method`). Minimally, you should allow email/password authentication; this will let users specify their own username and password, and supports functionality related to that.

You should now be able to use the Firebase classes to create accounts, or login using exisiting accounts. See the authentication documentation for specific examples.

# Save Records

Cloud Firestore is a cloud-hosted, NoSQL database, that can be accessed through a REST API, or other client APIs.

Data is stored in documents that contain fields mapping to values. Documents support many different data types, from simple strings and numbers, to complex, nested objects. You can also create subcollections within documents and build hierarchical data structures (see documentation).

Although it doesn't support the range of SQL operations, you can form simple queries against this data store, including sorting and filtering data. Firebase has real-time notifications that can be configured to let your application know when data has been updated or added to a collection.

The Cloud Firestore documentation contains a walkthrough on setting up this service.

Create a Cloud Firestore database

  1. Navigate to the Cloud Firestore section of the Firebase console. Select your Firebase project. Follow the database creation workflow.
  2. Select a starting mode for your Cloud Firestore Security Rules: Test mode (allows full public access) or Locked mode (locked to your registered apps).
  3. Select a location for your database, or let it use the default storage location.

Set up your development environment

We assume you've already created the project, and added Firebase dependencies from the Configuring your project section.

Declare the dependency for the Cloud Firestore library for Android in your module (app-level) Gradle file (usually app/build.gradle.kts or app/build.gradle).

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

You should now be able to perform common tasks from your project:

# Other Services

  • see storage docs for details on storing binary objects
  • see hosting docs for details on running microservices from Firebase