> ## Documentation Index
> Fetch the complete documentation index at: https://upstash-vector.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Serverless Framework

This tutorial implements a serverless application and deploy it to AWS Lambda
using Serverless Framework

<Note>
  See [Using AWS SAM](/redis/tutorials/using_aws_sam), if you prefer AWS SAM
  over Serverless Framework.
</Note>

### Step 1: Create database on Upstash

If you do not have one, create a database following this
[guide](../overall/getstarted).

### Step 2: Project Setup

If you do not have it already install serverless framework via:
`npm install -g serverless`

In any folder run `serverless` as below:

```text
>> serverless

Serverless: No project detected. Do you want to create a new one? Yes
Serverless: What do you want to make? AWS Node.js
Serverless: What do you want to call this project? test-upstash

Project successfully created in 'test-upstash' folder.

You can monitor, troubleshoot, and test your new service with a free Serverless account.

Serverless: Would you like to enable this? No
You can run the “serverless” command again if you change your mind later.
```

Inside the project folder create a node project with the command:

```
npm init
```

Then install the redis client with:

```
npm install ioredis
```

Edit handler.js file as below. Replace the Redis URL (copy ioredis url from your
Upstash console).

```javascript
var Redis = require("ioredis");

if (typeof client === "undefined") {
  var client = new Redis(REDIS_URL);
}
module.exports.hello = async (event) => {
  await client.set("foo", "bar");
  let response = await client.get("foo");
  return { response: response };
};
```

<Snippet file="redis/ioredisnote.mdx" />

### Step 3: Deploy Your Function

In the project folder run:

```
serverless deploy
```

The output will be:

```shell
➜  test-upstash> serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service test-upstash.zip file to S3 (178.2 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...............
Serverless: Stack update finished...
Service Information
service: test-upstash
stage: dev
region: us-east-1
stack: test-upstash-dev
resources: 6
api keys:
  None
endpoints:
  None
functions:
  hello: test-upstash-dev-hello
layers:
  None
```

Run your function via `serverless invoke -f hello`:

```shell
serverless invoke -f hello
{
    "response": "bar"
}
```

You can also test your function using AWS console. In your AWS Lambda section,
click on your function. Scroll down to the code sections and click on the `Test`
button on the top right.

<Frame>
  <img src="https://mintlify.s3-us-west-1.amazonaws.com/upstash-vector/img/awslambda/success2.png" />
</Frame>

Congratulations, now your lambda function inserts entry to your Upstash
database.

### What can be the next?

* You can write and deploy another function to just get values from the
  database.
* You can integrate
  [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html)
  so you can call your function via http.
* You can learn about how to monitor your functions from CloudWatch as described
  [here](https://docs.aws.amazon.com/lambda/latest/dg//monitoring-functions-logs.html).

#### Redis Connections in AWS Lambda

Although Redis connections are very lightweight, a new connection inside each
Lambda function can cause a notable latency. On the other hand, reusing Redis
connections inside the AWS Lambda functions has its own drawbacks. When AWS
scales out Lambda functions, the number of open connections can rapidly
increase. Fortunately, Upstash detects and terminates the idle and zombie
connections thanks to its smart connection handling algorithm. Thanks to this
algorithm; we have been recommending caching your Redis connection in serverless
functions.

<Info>
  See [the blog post](https://blog.upstash.com/serverless-database-connections)
  about the database connections in serverless functions.
</Info>
