Shinya Mochida@型
2 min readDec 8, 2018

--

Running vim as a AWS Lambda Function

I heard that Vim can run as a Web server, so I thought it could be possible to run vim as a AWS Lambda Function, so I tried the assumption.

It seems that vim is not installed in the VM on which lambda function runs. So It is needed to build vim and compose it in the artifact zip file. And the VM also doesn’t have libtinfo.so so the archive file should contain the library in it. Here is the build script of Vim, which I run the script on Amazon Linux 2.

cd $HOME
sudo yum install -y git libncurses-devel
sudo yum groupinstall "Development Tools"
git clone https://github.com/vim/vim.git
cd vim
./configure \
--with-features=normal \
--prefix=$HOME/release
make
make install
cd $HOME/release
mkdir lib
cp /lib64/libtinfo.so.6 lib/

AWS Lambda custom runtime requires deployments to contain bootstrap file, because Lambda calls it first. So I have made bootstrap associated with my builded vim via shebang, and calling bootstrap will start Vim.

See the official documentation on how to make AWS Lambda custom runtime. https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html

#!bin/vim -u

:let header = system("mktemp")[:-2]
:let request = "http://" . $AWS_LAMBDA_RUNTIME_API . "/2018-06-01/runtime/invocation/next"

:while 1
: let event = system("curl -L -D " . header . " " . request)
: let payload = "{\"receive\":" . event . "}"

: let headers = readfile(header)
: let id = ""
: for item in headers
: if item =~ "Lambda-Runtime-Aws-Request-Id"
: let id = substitute(item, "Lambda-Runtime-Aws-Request-Id", "", "g")
: let id = substitute(id, ":", "", "g")
: let id = substitute(id, " ", "", "g")
: endif
: endfor

: echo "==request=="
: echo event
: echo "==id=="
: echo id

: let response = "http://" . $AWS_LAMBDA_RUNTIME_API . "/2018-06-01/runtime/invocation/" . id . "/response"
: echo response
: let result = system("curl " . response . " -d '" . payload . "'")
:endwhile

:exit

Put this bootstrap file into $HOME/release. Of cause, do not forget to make it executable (chmod +x bootstrap).

The release` directory’s structure would become as bellow.

release
├── bin
│ └── vim
├── bootstrap
└── lib
└── libtinfo.so.6

Keep this structure and compose them into one zip file. And put it to S3, then create function.

zip vim.zip bootstrap bin/vim lib/libtinfo.so.6
aws s3api put-object \
--bucket my-bucket \
--key vim.zip
aws lambda create-function \
--function-name vim-lambda \
--role <role for lambda> \
--handler foo \
--runtime provided \
--code S3Bucket=my-bucket,S3Key=vim.zip

Now, it’s time to invoke our Vim function!!!

$ aws lambda invoke \
> --function-name vim-lambda \
> --payload '{"editor":"vim"}' \
> response.txt
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
$ cat response.txt
{"receive":{"editor":"vim"}}

Now Vim script can be thought as one of Serverless application description language, and Vim is the framework for Serverless application.

--

--