geoff-young

📝📞📹💻
Migrate from Serverless framework to SST CDK
Created
Oct 13, 2021 11:00 PM
status
In progress
I'm using TypeScript now, I tried using CDK in the past but it had email limits or something? Anyways I don't think those limits exist anymore. But, I have my setup as a multi repo right now, and I'm right on the edge, I'd rather just have it is a mono repo - this is because of a cloudformation limit, that you might be able to raise if you contact them? What is the cloudformation limit with SST CDK? Wow Oct 2020, it was changed. The 'maximum number of [Cloudformation] resources is 500 (previously 200)'.
DynamoDB - I was having issues with getting undefined resource errors, because I was adding/removing attributes from users. I decided to add all the attributes when the user account was created. Now, I'm realizing that this is not the way, it's best to have nothing initialized, and the app be able to compensate for changes. Not as annoying to pull off with TypeScript.
I've moved over a function and made it .ts - I can't use require?
I'm reading the serverless stack docs and they have
import AWS from "aws-sdk";
But should it be:
import { DynamoDB } from 'aws-sdk'
etc?
How do I access my table from the other stack?
The architecture of CDK is way more pleasing, choosing which group of calls gets which environment, etc.
 
 
'table' does not exist in type 'StackProps
Property 'table' does not exist on type 'StackProps'
Argument of type '{ table: any; }' is not assignable to parameter of type 'StackProps'.
I tried really hard to seperate the stacks out, I am just going to put them all together and deal with that later
Property 'table' does not exist on type 'DynamoDbStack'
 
local-stage-talktreeSST-dynamodb - why do I have 2 app titles "local-stage" and "talktreeSST", what are the SST deployment functions, I feel like an old deploy has merged with my new one somehow - its when you first deployed the app... or cloned it..?
Property does not exist on type 'StackProps'!
  • I tried creating an interface sstStackProps { table: sst.TableProps }, but it said that shit didn't exist?
Haven't made an IAM role to access API gateway before.. since I was using Cognito authorizer
Type '"AWS_IAM"' is not assignable to type 'ApiAuthorizationType | undefined'
The specified permissions are not supported. - I'm trying to attach the api permissions using the api object ... maybe it's just name. I just needed to use api.stackName instead of the whole object xD
I need to allow a function to use SES - create a new SES construct, add it to api?
 
Use user image from Notion
get rid of TAVS and user image in front end
change receiver to always be 'on'
I removed my service worker file now I have fuckton of workbox console.logs - its because of next-pwa, even though i removed the config and rebuilt, guess I would need to delete the workbox public files. I have the "self.__WB_DISABLE_DEV_LOGS = true" in my worker/index.js file - that's why.
I reinstated my service worker cause why not next-pwa seems decently beneficial
These names are complicated af - can I simplify them just the tiniest bit.. (dynamodb tables)
your page - [id] no longer auto generates, forward from sign up goes to 500
403 forbidden on getUsers in /[id]
Render a 'create notion page' when on own [id] if no notionId or nonexistent - would take too much time, sign up should just forward to account settings
make saveNotionId work - does dynamodb.putItem() overwrite all attributes?
I have cors: true in my APIStack but it's not showing in console. - It is, its just all * and greyed out.
I have getUser as a post, there was a mismatch in post/get so I was getting a CORS error. Now getting a 403 error ..
Getting a 500 error in saveNOtionId - no authorizer?
event.requestContext.authorizer?.jwt.claims['cognito:username']

SST Multi repo/stack TypeScript CDK

Example main/index.ts
import * as sst from "@serverless-stack/resources";
import * as lambda from "@aws-cdk/aws-lambda";
import DynamoDbStack from './DynamoDbStack'
import ApiStack from './ApiStack'
import CognitoStack from './CognitoStack'
import * as cdk from '@aws-cdk/core'

export default function main(app: sst.App) {
  app.setDefaultFunctionProps({
    runtime: lambda.Runtime.NODEJS_14_X,
  });

  app.stage === "dev" && app.setDefaultRemovalPolicy(cdk.RemovalPolicy.DESTROY)

  const dynamoDbStack = new DynamoDbStack(app, "dynamodb");

  const apiStack = new ApiStack(app, "api", { 
    usersTable: dynamoDbStack.usersTable, 
    receiversTable: dynamoDbStack.receiversTable,
    privateUsers: dynamoDbStack.privateUsers,
    sessionData: dynamoDbStack.sessionData
  });

  new CognitoStack(app, "cognito", { api: apiStack.api })

}
Example ApiStack
import * as sst from "@serverless-stack/resources";
import { CorsHttpMethod } from "@aws-cdk/aws-apigatewayv2"

export default class ApiStack extends sst.Stack {
  api
  constructor(scope: sst.App, id: string, props: any) {
    super(scope, id, props);
    const { 
      usersTable, 
      receiversTable,
      privateUsers,
      sessionData
     }  = props;
    // const domain = (scope.stage === "prod") ? "api.talktree.me" : "dev-api.talktree.me"

    this.api = new sst.Api(this, "Api", {
      cors: true,
      defaultAuthorizationType: sst.ApiAuthorizationType.AWS_IAM,
      defaultFunctionProps: {
        environment: {
          UsersTable: usersTable.tableName,
          ReceiversTable: receiversTable.tableName,
          PrivateUser: privateUsers.tableName,
          SessionData: sessionData.tableName,
        },
      },
      routes: {
        "GET /getUsers": "src/getUsers.handler",
        "POST /createUnpaidSession": "src/createUnpaidSession.handler",
        "POST /getUser": "src/getUser.handler",
        "POST /saveNotionId": "src/saveNotionId.handler" 
      },
    });
    this.api.attachPermissions([usersTable, receiversTable, privateUsers, sessionData, "ses"]);

    // Show the endpoint in the output
    this.addOutputs({
      "ApiEndpoint": this.api.url,
      "Routes": JSON.stringify(this.api.routes),
    });
  }
}