Hey All! Has anyone been able to create a CDK Rest...
# help
b
Hey All! Has anyone been able to create a CDK RestApi in one stack and share it with other stacks in the project? Here's my setup:
Copy code
// index.ts
import PeopleStack from './people-stack';
import PlanetsStack from './planets-stack';
import SharedStack from './shared-stack';
import * as sst from '@serverless-stack/resources';

export default function main(app: <http://sst.App|sst.App>): void {
  app.setDefaultFunctionProps({
    runtime: 'nodejs12.x'
  });

  const shared = new SharedStack(app, 'shared-stack');
  new PeopleStack(app, 'people-stack', shared);
  new PlanetsStack(app, 'planets-stack', shared);
}
Copy code
// shared-stack.ts
import * as sst from '@serverless-stack/resources';
import { IRestApi, RestApi } from '@aws-cdk/aws-apigateway';

export default class SharedStack extends sst.Stack {

  public readonly restApi: IRestApi;

  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props);

    this.restApi = <IRestApi> new RestApi(this, `${scope.stage}-sst-demo`);
  }
}
Copy code
// people-stack.ts
import * as sst from '@serverless-stack/resources';
import SharedStack from './shared-stack';

export default class PeopleStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, shared: SharedStack, props?: sst.StackProps) {
    super(scope, id, props);

    const api = new sst.ApiGatewayV1Api(this, "Api", {
      restApi: shared.restApi,
      routes: {
        "GET /people/{id}": "src/people/lambda.handler",
      },
    });

    this.addOutputs({
      "ApiEndpoint": api.url,
    });
  }
}
Copy code
// planets-stack.ts
import * as sst from '@serverless-stack/resources';
import SharedStack from './shared-stack';

export default class PlanetsStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, shared: SharedStack, props?: sst.StackProps) {
    super(scope, id, props);

    const api = new sst.ApiGatewayV1Api(this, "Api", {
      restApi: shared.restApi,
      routes: {
        "GET /planets/{id}": "src/planets/lambda.handler",
      },
    });

    this.addOutputs({
      "ApiEndpoint": api.url,
    });
  }
}
With the above setup, I get the following error when trying to `npx sst build`:
Copy code
Error: 'dev-sst-demo-shared-stack' depends on 'dev-sst-demo-people-stack' (dev-sst-demo-shared-stack -> dev-sst-demo-people-stack/Api/Lambda_GET_--people--{id}/Resource.Arn). Adding this dependency (dev-sst-demo-people-stack -> dev-sst-demo-shared-stack/dev-sst-demo/Resource.Ref) would create a cyclic reference.
    at PeopleStack._addAssemblyDependency (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/stack.ts:730:13)
    at Object.addDependency (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/deps.ts:52:20)
    at PeopleStack.addDependency (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/stack.ts:485:5)
    at resolveValue (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/private/refs.ts:100:12)
    at Object.resolveReferences (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/private/refs.ts:30:24)
    at Object.prepareApp (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/private/prepare-app.ts:31:3)
    at Object.synthesize (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/private/synthesis.ts:24:3)
    at App.synth (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/stage.ts:180:23)
    at App.synth (/Users/kochwm/dev/sst/sst-demo/node_modules/@serverless-stack/resources/src/App.ts:199:33)
    at process.<anonymous> (/Users/kochwm/dev/sst/sst-demo/node_modules/@aws-cdk/core/lib/app.ts:123:45)
Any help would be greatly appreciated!
FWIW, I was able to get this to work rather easily with the CDK's
HttpApi
and
sst.Api
, but I wanted to experiment with the CDK's
RestApi
as well because there are some features (like X-Ray integration) that are missing from
HttpApi
f
Hi @Bill Koch, can you try creating the
sst.ApiGatewayV1Api
in
SharedStack
and add routes to it in the other stacks?
Copy code
// shared-stack.ts
this.restApi = new sst.ApiGatewayV1Api(this, `Api`, {
    routes: {
      "GET /people/{id}": "src/people/lambda.handler",
    },
});
Copy code
// index.ts
const shared = new SharedStack(app, 'shared-stack');
new PeopleStack(app, 'people-stack', {
  restApi: shared.restApi
});
Copy code
// people-stack.ts
props.restApi.addRoutes(this, {
  "GET /people/{id}": "src/people/lambda.handler"
});
b
@Frank Thank you very much! That worked perfectly, and your approach makes more sense that what I had originally 😅. Thanks again!
f
Nice! No worries!