Hey All - I'm trying deploy my SST application to ...
# help
b
Hey All - I'm trying deploy my SST application to an existing RestApi in AWS. I was able to follow the example for importing an existing RestApi into SST, but I can't seem to get my resources associated to a stage/deployment in the RestApi. Here's my stack:
Copy code
// MyStack.ts
import * as sst from "@serverless-stack/resources";
import { Deployment, RestApi, Stage } from "@aws-cdk/aws-apigateway";

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

    const apiGateway = RestApi.fromRestApiAttributes(this, "MyRestApi", {
      restApiId: "8w6b74ksx0",
      rootResourceId: "6lhf5t9hje",
    });

    const deployment = new Deployment(this, `${scope.stage}-deployment`, {
      api: apiGateway
    });

    const stage = new Stage(this, `${scope.stage}-stage`, {
      deployment: deployment,
      stageName: scope.stage
    });

    apiGateway.deploymentStage = stage;

    // Create a HTTP API
    const api = new sst.ApiGatewayV1Api(this, "Api", {
      restApi: apiGateway,
      importedPaths: {
        "/serverless": "tvg3ia"
      },
      routes: {
        "GET /serverless/hello": "src/lambda.handler",
      },
    });
  }
}
Any help would be greatly appreciated!
For reference, here's what I see in the AWS Console after running `sst deploy --stage test`:
And stages:
f
Hey @Bill Koch, hmm…
Copy code
apiGateway.deploymentStage = stage;
I’m not sure if you can set the
deploymentStage
on an imported RestApi.
Just curious, if the RestApi you are importing doesn’t already have a
Stage
and
Deployment
, why not creating a new RestApi instead of importing one?
b
Oh, sorry. This is just a proof of concept for now. Just trying to see how things would work before I hook it up to the "real" RestApi 😅
The real RestApi would have existing `Stage`s and `Deployment`s though
f
Ah that makes sense.
Maybe as a test, create the Stage and Deployment in ur AWS console, and see if that works.
b
Ok -- so I deleted the existing
test
stage and created a new
test
stage via the AWS console:
Then I re-ran
sst deploy --stage test
, and the output shows there were no changes to deploy:
Copy code
npx sst deploy --stage test
Preparing your SST app
Detected tsconfig.json
Transpiling source
Linting source

/Users/kochwm/dev/sst/restapi-example/lib/MyStack.ts
  17:11  warning  'stage' is assigned a value but never used  @typescript-eslint/no-unused-vars
  25:11  warning  'api' is assigned a value but never used    @typescript-eslint/no-unused-vars

✖ 2 problems (0 errors, 2 warnings)

Running type checker
Building Lambda function src/lambda.handler
Linting Lambda function source

/Users/kochwm/dev/sst/restapi-example/src/lambda.ts
  4:3  warning  'event' is defined but never used  @typescript-eslint/no-unused-vars

✖ 1 problem (0 errors, 1 warning)

Type checking Lambda function source
Deploying stacks

 ✅  test-restapi-example-my-stack (no changes)


Stack test-restapi-example-my-stack
  Status: no changes
f
Can you remove the Stage and Deployment from ur CDK code?
b
And, perhaps expectedly based on the deploy command output, there aren't any new deployments in the
test
stage in the AWS console:
Weird. The command output said there were no changes to deploy, but now the
test
stage is gone in the AWS Console 🤔
f
Yeah, when u add/remove stuff in the Console directly, CloudFormation doesn’t know u did that.
b
Shouldn't the stage that I created via the console still be there?
f
Let’s try from scratch: 1. Removed the Stage and Deployment from ur CDK code. 2.
npm run remove
to remove ur stack 3. Setup Stage/Deployment in ur AWS cnosole manually 4.
npm run deploy
to deploy ur stack
the
test
stage was created by CF, and then it was manually removed and added in AWS console, CF doesn’t know that.. so it removed the one u created
b
Ok -- doing those steps updated the Resources of the RestApi, but it didn't update the Stage
FWIW, I don't see a
AWS::ApiGateway::Deployment
or
AWS::ApiGateway::Stage
in the generated CF
f
I see. Lemme give it a try tonite. I will let you know.
b
AWS::ApiGateway::Deployment
  and 
AWS::ApiGateway::Stage
are in the generated CF when I don't try to import an existing RestApi (have SST generate one for me)
Thank you for your help!
f
Np. Lemme ask @Ross Coundon quickly, I think he might’ve done something similar. Hey Ross, are you importing an existing
RestApi
into
sst.ApiGatewayV1Api
? Did you have to create an Api Gateway Deployment resource in ur CDK code, ie.
Copy code
const importedApi = RestApi.fromRestApiAttributes(...)
const deployment = new Deployment(this, `deployment`, {
  api: importedApi
});
r
I'm afraid not, I went down the route of creating a new RestApi
f
Hey @Bill Koch, I managed to reproduce the issue you ran into, and I just released v0.26.1 with the fix.
Copy code
new sst.ApiGatewayV1Api(this, "Api", {
      restApi: apig.RestApi.fromRestApiAttributes(this, "ImportedApi", {
        restApiId: "2x7syiara1",
        rootResourceId: "rz22vbtuih",
      }),
      routes: {
        "GET /new": "src/lambda.main",
      },
    });
The above should work. No need to create a
Stage
or
Deployment
construct.
Give it a try and let me know if it works for you!
b
@Frank Thanks so much for looking at this! The changes work great!
One quick observation/question: if I run
npx sst deploy --stage dev
and then attempt to deploy a different stage
npx sst deploy --stage tst
, I get this error:
Copy code
CREATE_FAILED | AWS::ApiGateway::Resource | ApiResourceserverlesshello57FB95D2 Another resource with the same parent already has this name: hello (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException; Request ID: 1cb91abb-1607-48a3-8d82-d9df4eb13f28; Proxy: null)
Should multiple stages work?
f
Are you importing the same REST API?
Copy code
RestApi.fromRestApiAttributes(this, "MyRestApi", {
      restApiId: "8w6b74ksx0",
      rootResourceId: "6lhf5t9hje",
    });
I’m guessing the
restApiId
and
rootResourceId
should be different for different stages?
b
Yep, I am using the same RestApi. I was hoping to have a single RestApi that developers could deploy to for their feature development, using RestApi stages for isolation. So, for example: • somedomain.com/feature-ABC-123 (
sst deploy --stage feature-ABC-123
) • somedomain.com/feature-XYZ-456 (
sst deploy --stage feature-XYZ-456
)
But maybe that's not possible? I apologize -- I'm definitely new to this 😅
f
Yeah, that’s not recommended. B/c this will only work if all developers are sharing the same AWS account.
The best practice is to use a new API project for each stage.