sst / sst

Build modern full-stack applications on AWS

Home Page:https://sst.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Export xxxxx cannot be deleted error when removing Restful API routes

tchcxp opened this issue · comments

Hi there,

I have a problem removing or updating routes in an imported API stack.

I use SST to build an API gateway and microservices in the backend. I love the live lambda, which saves heaps of time to build and debug lambda functions locally. 👍

The infrastructure is simple: there is one API stack file and a few service stack files, which use the exported API resource from the API stack.

The problem is when I remove or update any route in a service stack file, and deploy, I get an error message like: Export xxxxx cannot be deleted as it is in use by xxxxx.


Here is a quick demo to reproduce the issue:

  1. In the API stack api-stack.ts, I set up the Restful API gateway:
import { ApiGatewayV1Api } from 'sst/constructs'

 const api = new ApiGatewayV1Api(stack, 'Api', {
    cdk: {
      restApi: {
        restApiName: 'demo-api',
      },
    },
  })

return { api }
  1. In the Demo service stack file demo-service.ts, I added 2 routes to the API gateway:
import { use, StackContext } from 'sst/constructs'
import { ApiStack } from './api-gateway'

export function DemoServiceStack({ stack }: StackContext) {
  const { api } = use(ApiStack)
  api.addRoutes(stack, {
    'GET /demo-path':  'packages/functions/v1/demo-service/get.handler',
    'POST /demo-path':  'packages/functions/v1/demo-service/post.handler'
    })
})
  1. In the sst.config.ts file, I added stacks:
import { SSTConfig } from 'sst'
import { ApiStack } from './stacks/api-gateway'
import { DemoServiceStack } from './stacks/demo-service'

export default {
  config(_input) {
    ...
  },
   stacks(app) {
    app
      .stack(ApiStack)
      .stackk(DemoServiceStace)
    }
}

Now I remove a route (e.g. 'GET /demo-path': 'packages/functions/v1/demo-service/get.handler', in the 2nd file above), I receive the error message below:

demo-service: Export demo-service:ExportsOutputFnGetAttLambdaGETv1xxxxxxxxxxxxxxCD5D cannot be deleted as it is in use by api-gateway

I understand why this error message happens, but I guess it was resolved when I found automatic export injection.

I was thinking about removing the reference, in this case, the api resource; however, I still need the other routes in the same file.

Please let me know if there is any setting I was not aware of or if there is a workaround that can fix this issue. I do love using SST to build serverless applications but not confident about using it in production in this case because I cannot delete or update a route in the API.

Thanks,

commented

Yeah this is painful and not intuitive. It's partly why we are moving away from CDK and CloudFormation for our next version.

This specific issue I talk about it here: https://sst.dev/blog/moving-away-from-cdk.html#3-export-in-use