aws-cdk: (aws-apigateway): RestApi imported with method fromRestApiAttributes not updating current deployment in stage

I’ve imported from a serverless project this restApi.

const restApiId = cdk.Fn.importValue(envRelated(`ApiGateway-restApiId`));
const rootResourceId = cdk.Fn.importValue(envRelated(`ApiGateway-rootResourceId`));
this.apiGw = apigateway.RestApi.fromRestApiAttributes(this, envRelated(`apigw`), {
	restApiId: restApiId,
	rootResourceId: rootResourceId,
});

When deploying with cdk deploy, all resources are updated but not the stage. No deployment is created. I’ve tried also to force a deployment with this snippet.

new Deployment(this,envRelated('deployment-stage') , {
      api: apiGw
    });

Salting the deployment id makes no difference. After the deployment is done, the api gateway is still pointing to the previous deployment instead of the new deployment done.

chrome_sGUK4TBoqT

Specifying the stage with the forced deployment fails because stage already exists.

Environment

  • CDK CLI Version: 1.9.2
  • Module Version: 1.9.2
  • Node.js Version: 12.14.1
  • OS: Ubuntu Linux 20.04
  • **Language: ts

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 21 (7 by maintainers)

Most upvoted comments

Seriously guys, I’m tired of this. CDK Development is 95% google search, and 5% actual coding. The documentation is horrendous, you really need to make it a priority to update it. The above almost worked for me. The issue, if you look at the template, is that the deployment is created in parallel with everything else if there is no dependency on anything, and winds up running first before the actual new resource or whatever is applied to an existing gateway. I couldn’t depend on the “FromResourceAttribute” object cuz it didn’t add a dependency to the template for some reason. I had to have the Deployment depend on the new resource I added. For those who find this, here is example .Net code that worked for me. You can convert this to the language of your choice: (Btw, this is CDK V2 code from version 2.88.0)

` var stack = new Stack(scope, id, stackProps);

    var function = new Function(stack, "TestFunction", new FunctionProps()
    {
	    //NOTE: Substitute withyour own new lambda code
        FunctionName = "MyTestFunction",
        Runtime = Runtime.PYTHON_3_10,
        Code = Code.FromAsset("lambda"),
        Handler = "lambda_function.lambda_handler"
    });

    var restApi = RestApi.FromRestApiAttributes(stack, "ExistingRestApi2", new RestApiAttributes()
    {
	    //You have to export the api-gateway-api-id and api-gateway-root-resource-id values as CfnOutputs from your main stack that initially deployed the gateway.
        RestApiId = Fn.ImportValue("api-gateway-api-id"),
        RootResourceId = Fn.ImportValue("api-gateway-root-resource-id")
    });

    var stage = Amazon.CDK.AWS.APIGateway.Stage.FromStageAttributes(stack, "PublicAPIStage", new StageAttributes()
    {
        RestApi = restApi,
		
		//Replace with your existing stage name
        StageName = "public-api"
    });

    var newResource = restApi.Root.AddResource("graph");
    newResource.AddProxy(new ProxyResourceOptions()
    {
        DefaultIntegration = new LambdaIntegration(function),
        AnyMethod = true,
        DefaultMethodOptions = new MethodOptions()
        {
            AuthorizationType = AuthorizationType.NONE
        }
    });
    newResource.Node.AddDependency(function);

    var deployment = new Deployment(stack, "ApiTestDeployment", new DeploymentProps()
    {
        Api = restApi,
        RetainDeployments = false,
    });
	
	//this salts the deployment
    deployment.AddToLogicalId(DateTime.UtcNow.ToString());
	
    //make the deployment dependent on the resource so it runs last
    deployment.Node.AddDependency(newResource);

    //StageName is unavailable in the L2 deployment class, so you need to get the L1 resource.
    var cfnDeployment = (CfnDeployment)deployment.Node.DefaultChild!;
	
	//set the name of the stage on the L1 deployment object.
    cfnDeployment.StageName = "public-api";`