Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
docs: lambda vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Oct 8, 2024
1 parent 7e15972 commit 6ef6756
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/aws-lambda-vpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# sst
.sst
22 changes: 22 additions & 0 deletions examples/aws-lambda-vpc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Resource } from "sst";
import { Cluster } from "ioredis";

const redis = new Cluster(
[{ host: Resource.MyRedis.host, port: Resource.MyRedis.port }],
{
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
tls: {},
username: Resource.MyRedis.username,
password: Resource.MyRedis.password,
},
}
);

export const handler = async () => {
const counter = await redis.incr("counter");
return {
statusCode: 200,
body: `Hit counter: ${counter}`
};
};
19 changes: 19 additions & 0 deletions examples/aws-lambda-vpc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "aws-lambda-vpc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ioredis": "^5.4.1",
"sst": "3.1.68"
},
"devDependencies": {
"@types/aws-lambda": "8.10.145"
}
}
25 changes: 25 additions & 0 deletions examples/aws-lambda-vpc/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyFunction": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"MyRedis": {
"host": string
"password": string
"port": number
"type": "sst.aws.Redis"
"username": string
}
"MyVpc": {
"bastion": string
"type": "sst.aws.Vpc"
}
}
}
66 changes: 66 additions & 0 deletions examples/aws-lambda-vpc/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/// <reference path="./.sst/platform/config.d.ts" />

/**
* ## AWS Lambda in a VPC
*
* You can use SST to locally work on Lambda functions that are in a VPC. To do so, you'll
* need to enable `bastion` and `nat` on the `Vpc` component.
*
* ```ts title="sst.config.ts"
* new sst.aws.Vpc("MyVpc", { bastion: true, nat: "managed" });
* ```
*
* The NAT gateway is necessary to allow your Lambda function to connect to the internet. While,
* the bastion host is necessary for your local machine to be able to tunnel to the VPC.
*
* You'll need to install the tunnel, if you haven't done this before.
*
* ```bash "sudo"
* sudo sst tunnel install
* ```
*
* This needs _sudo_ to create the network interface on your machine. You'll only need to do
* this once.
*
* Now you can run `sst dev`, your function can access resources in the VPC. For example, here
* we are connecting to a Redis cluster.
*
* ```ts title="index.ts"
* const redis = new Cluster(
* [{ host: Resource.MyRedis.host, port: Resource.MyRedis.port }],
* {
* dnsLookup: (address, callback) => callback(null, address),
* redisOptions: {
* tls: {},
* username: Resource.MyRedis.username,
* password: Resource.MyRedis.password,
* },
* }
* );
* ```
*
* The Redis cluster is in the same VPC as the function.
*/
export default $config({
app(input) {
return {
name: "aws-lambda-vpc",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
};
},
async run() {
const vpc = new sst.aws.Vpc("MyVpc", { bastion: true, nat: "managed" });
const redis = new sst.aws.Redis("MyRedis", { vpc });
const api = new sst.aws.Function("MyFunction", {
vpc,
url: true,
link: [redis],
handler: "index.handler"
});

return {
url: api.url,
};
},
});
1 change: 1 addition & 0 deletions examples/aws-lambda-vpc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
74 changes: 74 additions & 0 deletions www/src/content/docs/docs/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,64 @@ return {
View the [full example](https://github.com/sst/ion/tree/dev/examples/aws-lambda-stream).


---
## AWS Lambda in a VPC

You can use SST to locally work on Lambda functions that are in a VPC. To do so, you'll
need to enable `bastion` and `nat` on the `Vpc` component.

```ts title="sst.config.ts"
new sst.aws.Vpc("MyVpc", { bastion: true, nat: "managed" });
```

The NAT gateway is necessary to allow your Lambda function to connect to the internet. While,
the bastion host is necessary for your local machine to be able to tunnel to the VPC.

You'll need to install the tunnel, if you haven't done this before.

```bash "sudo"
sudo sst tunnel install
```

This needs _sudo_ to create the network interface on your machine. You'll only need to do
this once.

Now you can run `sst dev`, your function can access resources in the VPC. For example, here
we're connecting to a Redis cluster.

```ts title="index.ts"
const redis = new Cluster(
[{ host: Resource.MyRedis.host, port: Resource.MyRedis.port }],
{
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
tls: {},
username: Resource.MyRedis.username,
password: Resource.MyRedis.password,
},
}
);
```

The Redis cluster is in the same VPC as the function.
```ts title="sst.config.ts"
const vpc = new sst.aws.Vpc("MyVpc", { bastion: true, nat: "managed" });
const redis = new sst.aws.Redis("MyRedis", { vpc });
const api = new sst.aws.Function("MyFunction", {
vpc,
url: true,
link: [redis],
handler: "index.handler"
});

return {
url: api.url,
};
```

View the [full example](https://github.com/sst/ion/tree/dev/examples/aws-lambda-vpc).


---
## AWS multi-region

Expand Down Expand Up @@ -1624,6 +1682,22 @@ new sst.aws.StaticSite("Web", {
View the [full example](https://github.com/sst/ion/tree/dev/examples/aws-vite).


---
## Cloudflare Cron

This example creates a Cloudflare Worker that runs on a schedule.
```ts title="sst.config.ts"
const cron = new sst.cloudflare.Cron("Cron", {
job: "index.ts",
schedules: ["* * * * *"]
});

return {};
```

View the [full example](https://github.com/sst/ion/tree/dev/examples/cloudflare-cron).


---
## Cloudflare KV

Expand Down

0 comments on commit 6ef6756

Please sign in to comment.