Building Cloud-Native Applications Made Easy with Pluto: A Guide for Developers
Tags: typescript, tutorial, serverless, cloudnative
Published: 2023-11-22 15:13:13

Developers define variables in their code, and [Pluto](https://github.com/pluto-lang/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](https://nodejs.org/en/): Pluto supports writing cloud applications using TypeScript.
- [Pulumi](https://www.pulumi.com/docs/install/): 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](https://www.npmjs.com/). Install it by running the following command:

```shell
npm install -g @plutolang/cli
```

Verify your installation:

```shell
pluto --version
```

## 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:

```shell
pluto new
```

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
```

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

```shell
cd <project_root>  
npm install
```

### Write your business logic

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

```typescript
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;
});
```

![arch](http://cdn.zhengsj.cn/ob-1700630175532.png)

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:

```shell
pluto deploy
```

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

```shell
export AWS_REGION=us-east-1
```

![aws-resource](http://cdn.zhengsj.cn/ob-1700630203893.png)

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:

```shell
pluto stack new 
```

Specify the stack during deployment:

```shell
pluto deploy --stack <new_stack>
```

## More Resources

- Example: [Command-line chatbot based on OpenAI](https://github.com/pluto-lang/pluto/tree/main/examples/chat-bot)
- Example: [Share a daily computer joke on Slack](https://github.com/pluto-lang/pluto/tree/main/examples/daily-joke-slack)
- Repository: [Pluto | GitHub](https://github.com/pluto-lang/pluto)

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](https://join.slack.com/t/plutolang/shared_invite/zt-25gztklfn-xOJ~Xvl4EjKJp1Zn1NNpiw) community**