Hi There, I’ve been getting my head around SST for...
# help
b
Hi There, I’ve been getting my head around SST for the past few days, and really impressed with the setup so far. I’ve run into one problem that I can’t seem to get past … I need to connect my API/Lambda functions to an RDS MySQL instance (in an Isolated Subnet). I’ve configured an SST API stack to do this, with the Lambda function associated with a VPC, SGs, etc. And I have it working when deployed. However, as soon as I add the Lambda function to my VPC, my Live Lambda Development environment stops working. It seems that the API GW fails to find/reach the debug Lambda? I get absolutely no output in the console, and the API request just times out. Any suggestions would be really appreciated!
t
I would recommend using a local MySQL during development
I've found using RDS, especially with VPC requirements a bit annoying
b
Thanks @thdxr My issue seems to occur earlier than that though. I’ve got a bare bones “hello world” Lambda at the moment. No RDS connection or anything. As soon as I add the function to the VPC, the Live Dev stops working.
t
Yeah I keep my lambdas out of VPC during local dev as well. It seems that in the isolated subnet the Lambda cannot talk to the service it needs to in order to send the websocket message to your local env
fwiw it works fine in a private subnet (still has internet access)
b
Ok. Cool. That’s pretty much what I had figured.
So can I conditionally configure a stack to deploy differently for dev/staging/prod?
t
I try to avoid my cdk code being aware of any specific environments and only have 2 modes. Local and non-local. You can check if it's local with
scope.local
which returns true when in debug mode
b
Thanks. That gives me something to work with. I’ll give that a try. 🙏
t
Here's some sample code
Copy code
export class Stack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, props?: StackProps) {
    super(scope, id, props)

    const network = Network(this, scope)
    const prisma = Prisma(this, scope)
    const database = Database(this, scope, { network, prisma })
    Api(this, { network, prisma, database })
  }
}
Copy code
export function Api(stack: sst.Stack, props: Props) {
  const router = new sst.Function(stack, "router", {
    vpc: props.network.vpc,
    vpcSubnets: props.network.vpc && {
      subnetType: ec2.SubnetType.PRIVATE,
    },
    layers: [...props.prisma.layers],
    runtime: lambda.Runtime.NODEJS_14_X,
    handler: `src/functions/router.handler`,
    environment: props.database.env,
    bundle: {
      externalModules: props.prisma.externalModules,
    },
  })
Copy code
export function Network(stack: sst.Stack, scope: <http://sst.App|sst.App>) {
  if (scope.local)
    return {
      vpc: undefined,
    }
so vpc is
undefined
in local dev
b
👍
f
@Brett Gullan I just opened an issue to track this https://github.com/serverless-stack/serverless-stack/issues/487
Do you mind sharing a code snippet of ur VPC configuration? I just want to add a note to the issue when I get to this.
b
This is the VPC config, currently created in CDK and imported into my SST stack(s):
Copy code
this.vpc = new Vpc(this, 'CustomVPC', {
      cidr: '10.0.0.0/16',
      natGateways: 0,
      maxAzs: 2,
      subnetConfiguration: [
        {
          name: 'publicSubnet',
          subnetType: SubnetType.PUBLIC,
          cidrMask: 24,
        },
        {
          name: 'isolatedSubnet',
          subnetType: SubnetType.ISOLATED,
          cidrMask: 24,
        },
      ],
    })
f
Thanks Brett!