This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
# sst | ||
.sst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters