Ross Coundon
03/06/2021, 9:01 AMFrank
VPC ... would need to be defined using CDK rather than SST.Yes, that’s correct. Each SST construct is just a thin wrapper grouping several CDK constructs together. For example, when you create an
sst.Api
construct, behind the scene, you are really creating an apigatewayv2.HttpApi
. And for each route that’s added, you are creating a apigatewayv2.HttpRoute
and a lambda.Function
. And if custom domain is configured for the sst.Api
construct, an acm.Certificate
, a apigatewayv2.DomainName
and a couple of route53.Record
s are created.
For VPC, SST doesn’t have a wrapper around it, you can just define it using CDK constructs.
Btw, are you thinking of rewriting serverless.ts
to SST in one go? You also have the option to leave the VPC stuff in serverless.ts
, and imports the VPC in to SST. So if you want to create ie. a new Api endpoint in SST and have the Lambda functions launched into the VPC created in serverless.ts
, the code looks something like:
import * as ec2 from "@aws-cdk/aws-ec2";
import * as sst from "@serverless-stack/resources";
// Import VPC
const existingVpc = ec2.Vpc.fromLookup(this, 'VPC', {
vpcName: `MyExistingVPC`,
});
// Create an API
new sst.Api(this, "Api", {
defaultFunctionProps: {
vpc: existingVpc,
},
routes: {
"GET /notes": "src/list.main",
"POST /notes": "src/create.main",
"GET /notes/{id}": "src/get.main",
"PUT /notes/{id}": "src/update.main",
"DELETE /notes/{id}": "src/delete.main",
},
});
Another thing I wanted to mention is that for local functions to be able to connect to your RDS while live developing, either the RDS has to be publicly exposed, or connected through a VPN. Some ppl are fine with the former for their dev servers. But here’s some info on setting up a VPN using AWS Client VPC service https://docs.serverless-stack.com/live-lambda-development#connecting-to-a-vpc
lol.. there is a lot of stuff to digest here. Let me know if you prefer to chat over zoom.Ross Coundon
03/06/2021, 8:56 PMFrank
Frank
Ross Coundon
03/06/2021, 9:06 PMFrank
Frank
import * as ec2 from "@aws-cdk/aws-ec2";
import * as sst from "@serverless-stack/resources";
// Create VPC using CDK construct
const vpc = new ec2.Vpc(this, 'VPC', { ... });
// Create an Function using SST construct
new sst.Function(this, "Function", { vpc });
Frank
Ross Coundon
03/06/2021, 9:09 PMFrank
sst.json
https://docs.serverless-stack.com/migrating-from-cdkRoss Coundon
03/06/2021, 9:12 PMRoss Coundon
03/06/2021, 9:13 PMFrank
Frank
Ross Coundon
03/06/2021, 10:22 PMRoss Coundon
03/16/2021, 9:09 PMRoss Coundon
03/16/2021, 9:13 PMsst start
I get the message
===================
Starting debugger
===================
Transpiling Lambda code...
Nothing has been transpiled
Debug session started. Listening for requests...
And I just get a internal server error and nothing is routed to my machineRoss Coundon
03/16/2021, 9:20 PMCannot find module 'lambda'
Which I guess is related to SST saying
Nothing has been transpiled
Which I assume is because this lambda is defined by CDK rather than SST, or am I on the wrong track?Frank
const handler = new lambda.Function(this, "MyFirstSst", {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromAsset('src'),
handler: "lambda.handler",
});
to
const handler = new sst.Function(this, "MyFirstSst", {
runtime: lambda.Runtime.NODEJS_14_X,
handler: "src/lambda.handler",
});
Frank
lambda.Function
, just swap it with sst.Function
Ross Coundon
03/16/2021, 9:54 PMFrank
sst.Function
extends lambda.Funtion
.. so it’s pretty much a drop in and replace.Ross Coundon
03/16/2021, 9:55 PMRoss Coundon
03/16/2021, 9:55 PMFrank
sst.Function
, you don’t need to set code
, just handler
is normally fine.Ross Coundon
03/16/2021, 9:57 PMFrank
lambda.Function
.. which you don’t need nowRoss Coundon
03/16/2021, 9:57 PMFrank
Ross Coundon
03/16/2021, 9:59 PMFrank