DEV Community

Cover image for Building Cloud-Native Applications Made Easy with Pluto: A Guide for Developers
jianzs@pluto-lang
jianzs@pluto-lang

Posted on

Building Cloud-Native Applications Made Easy with Pluto: A Guide for Developers

Developers define variables in their code, and Pluto takes care of automatically creating and managing the required cloud resource components based on those variables. This simplifies the process of deploying and managing cloud infrastructure, enabling developers to make better use of the cloud.

In the context, cloud resources do not refer to Infrastructure as a Service (IaaS), but rather to managed resource components such as Backend as a Service (BaaS) and Function as a Service (FaaS). These managed components generally provide enhanced reliability and cost-effectiveness compared to building and managing your own instances.

In this article, we will guide you through the steps of getting started with Pluto and help you become familiar with its features.

Installation

Prerequisites

  • Node.js: Pluto supports writing cloud applications using TypeScript.
  • Pulumi: Pluto uses Pulumi to interact with cloud platforms (such as AWS or Kubernetes) and deploy cloud resources.

Pluto CLI

The Pluto command-line tool is distributed via npm. Install it by running the following command:

npm install -g @plutolang/cli
Enter fullscreen mode Exit fullscreen mode

Verify your installation:

pluto --version
Enter fullscreen mode Exit fullscreen mode

Hello, Pluto

Now, let's get started with your first Pluto program.

Create your project

Create a Pluto project using the Pluto CLI by running:

pluto new
Enter fullscreen mode Exit fullscreen mode

This command will interactively create a project and create a directory with the provided project name. Here's an example:

$ pluto new
? Project name hello-pluto
? Stack name dev
? Select a platform AWS
? Select an IaC engine Pulumi
Info:  Created a project, hello-pluto
Enter fullscreen mode Exit fullscreen mode

After you have created the new project, go to the project root directory and install the dependencies.

cd <project_root>  
npm install
Enter fullscreen mode Exit fullscreen mode

Write your business logic

Use your preferred code editor to write the following code in <project_root>/src/index.ts:

import { Router, Queue, KVStore, CloudEvent, HttpRequest, HttpResponse } from "@plutolang/pluto";

const router = new Router("router");
const queue = new Queue("queue");
const kvstore = new KVStore("kvstore");

// Publish the access time to the queue, and respond with the last access time.
router.get("/access", async (req: HttpRequest): Promise<HttpResponse> => {
  const name = req.query["name"] ?? "Anonym";
  await queue.push(JSON.stringify({ name, accessAt: `${Date.now()}` }));
  const lastAccess = await kvstore.get(name).catch(() => undefined);
  const respMsg = lastAccess
    ? `Hello, ${name}! The last access was at ${lastAccess}`
    : `Hello, ${name}!`;
  return { statusCode: 200, body: respMsg };
});

// Subscribe to messages in the queue and store them in the KV database.
queue.subscribe(async (evt: CloudEvent): Promise<void> => {
  const data = JSON.parse(evt.data);
  await kvstore.set(data["name"], data["accessAt"]);
  return;
});
Enter fullscreen mode Exit fullscreen mode

arch

This code includes 3 resource variables and 2 processes:

  • An HTTP service called "router" that accepts /access HTTP requests. It publishes the access time to the message queue "queue" and retrieves the last access time from the KV database "kvstore" and returns it in the response.
  • A message queue named "queue" with a subscriber that saves messages from the queue to the KV database "kvstore".
  • A KV database named "kvstore" used to store users' last access time.

Deploy your application

To deploy your application to the cloud platform you configured initially, run the following command:

pluto deploy
Enter fullscreen mode Exit fullscreen mode

If you specified AWS as the cloud platform, make sure the AWS_REGION environment variable is correctly configured, for example:

export AWS_REGION=us-east-1
Enter fullscreen mode Exit fullscreen mode

aws-resource

Pluto will create 3 resource components and 2 function objects on the specified cloud platform. For example, if you chose AWS, it will create:

  • An ApiGateway named "router"
  • An SNS named "queue"
  • A DynamoDB named "kvstore"
  • Two Lambda functions starting with "function"

Multi-platform deployment

If you want to deploy to another cloud platform, you can create a new stack and specify the stack during deployment. Here are the steps:

Create a new stack:

pluto stack new 
Enter fullscreen mode Exit fullscreen mode

Specify the stack during deployment:

pluto deploy --stack <new_stack>
Enter fullscreen mode Exit fullscreen mode

More Resources

Pluto's main approach is to leverage techniques such as static program analysis and infrastructure as code to automatically create cloud resource components on the cloud platform by defining a variable. The goal of Pluto is to assist individual developers in building cloud-native applications with ease and reduce the learning curve associated with cloud capabilities.

Please note that Pluto is still in its early stages, and we welcome contributions from interested developers. If you are using AWS or Kubernetes, you can provide us with your requirements. We also appreciate any ideas or suggestions you may have, as they can be implemented in future versions. Feel free to join our Slack community

Top comments (0)