The Elusive Lambda Console; A Specification Proposal.
Tags: lambda, bash, cli, ruby
Published: 2023-06-17 20:15:35

After years of smashing Cloud & Rails together, I've come up with an idea. Better than an idea, a working specification! One where us [Rails & Lambda](https://lamby.cloud) enthusiasts can once again "console into" our "servers" and execute CLI tasks like migrations or interact via our beloved IRB friend, the Rails console. Today, I would like to present, the [Lambda Console](https://github.com/rails-lambda/lambda-console) project. An open specification proposal for any AWS Lambda runtime to adopt.

<!--truncate-->

![Lambda Console CLI Screenshot](https://lamby.cloud/img/docs/lambda-console-cli-dark.png)

## Lambda Console

```shell
npm install -g lambda-console-cli
```

The Lambda Console is a CLI written in Node.js that will interactively create an AWS SDK session for you to invoke your Lambda functions with two types of modes. 

1. CLI Runner
2. Interactive Commands

Think of the CLI Runner as a bash prompt. You can run any process command or interact with the filesystem or environment. For Rails users, running rake tasks or DB migrations. These tasks assume the Lambda task root as the present working directory.

Interactive commands however are evaluated in the context of your running application. For Ruby and Rails applications, this simulates IRB (Interactive Ruby Shell). For [Lamby](https://lamby.cloud) users, this mode simulates the Rails console. Making it easy for users to query their DB or poke their models and code.

## The Proposal

There is nothing about the [Lambda Console](https://github.com/rails-lambda/lambda-console) that is coupled to Ruby or Rails. The idea is simple, as a Lambda community, could we do the following?

1. Finalize a Lambda Console request/response specification.
2. Create more runtime-specific language implementations.
2. Build an amazing CLI client for any runtime.

Here is what we have today. The request specification, a simple [event structure](https://github.com/rails-lambda/lambda-console#event-structure) that is only a few dozen lines of JSON schema.

```json
{ "X_LAMBDA_CONSOLE": { "run": "cat /etc/os-release" } }
```

```json
{ "X_LAMBDA_CONSOLE": { "interact": "User.find(1)" } }
```

Any Lambda runtime code or framework could implement the handling of these event in their own language-specific pakages. You can find the Ruby implementation of these in the Lambda Console's first reference implementations.

* Ruby: The [lambda-console-ruby](https://github.com/rails-lambda/lambda-console-ruby) gem for any Ruby Lambda.
* Rails: Integrated into the [Lamby](https://github.com/rails-lambda/lamby) v5.0.0 for Rails on Lambda.

## The Possibilities

What I really want is an amazing CLI client. The current Lambda Console CLI was hacked together in a few days using some amazing Node.js tools that make building interactive CLIs so so easy. But I've never done this before. If this type of tooling sounds interesting to you and you like Node.js, let me know! It would be amazing to see implementation packages for these for Node, PHP, Python, and other frameworks using these languages. Here are some ideas on where I could see this going.

**Live STDOUT & STDERR:** We could take advantage of Lambda's new [Response Streaming](https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/) and send output buffers as they happen. 

**Pseudo TTY:** Is there a way to better simulate a real TTY session? Could this even include ANSI colors?

**Quality of Life Improvements:** Everything from, Allowing the CLI tool to switch modes without restarting it; Creating a command buffer to up arrow navigate history; Prettier UI. 

**Formal Response JSON Schema:** As the features grow, should the response JSON be standardized? For example, if the client wanted to syntax highlight interactive language commands, how would it know what language was being used? We could have a `X_LAMBDA_CONSOLE_LANG` response header.

What else would you like to see in a Lambda Console client?