CCarlos
CCarlos

Reputation: 153

How to document rest api using aws cdk

I'm creating a REST API using AWS CDK version 1.22 and I would like to document my API using CDK as well, but I do not see any documentation generated for my API after deployment.

I've dived into aws docs, cdk example, cdk reference but I could find concrete examples that help me understand how to do it.

Here is my code:

const app = new App();
const api = new APIStack(app, 'APIStack', { env }); // basic api gateway

// API Resources
const resourceProps: APIResourceProps = {
  gateway: api.gateway,
}

// dummy endpoint with some HTTP methods
const siteResource = new APISiteStack(app, 'APISiteStack', {
  env,
  ...resourceProps
});

const siteResourceDocs = new APISiteDocs(app, 'APISiteDocs', {
  env,
  ...resourceProps,
});

// APISiteDocs is defined as follow:
class APISiteDocs extends Stack {

  constructor(scope: Construct, id: string, props: APIResourceProps) {
    super(scope, id, props);

    new CfnDocumentationVersion(this, 'apiDocsVersion', {
      restApiId: props.gateway.restApiId,
      documentationVersion: config.app.name(`API-${config.gateway.api.version}`),
      description: 'Spare-It API Documentation',
    });

    new CfnDocumentationPart(this, 'siteDocs', {
      restApiId: props.gateway.restApiId,
      location: {
        type: 'RESOURCE',
        method: '*',
        path: APISiteStack.apiBasePath,
        statusCode: '405',
      },
      properties: `
        {
          "status": "error",
          "code": 405,
          "message": "Method Not Allowed"
        }
      `,
    });
  }
}

Any help/hint is appreciated, Thanks.

Upvotes: 4

Views: 4311

Answers (3)

Mel Delgado
Mel Delgado

Reputation: 11

I had the same exact problem. The CfnDocumentationVersion call has to occur after you create all of your CfnDocumentationPart. Using your code as an example, it should look something like this:

class APISiteDocs extends Stack {

  constructor(scope: Construct, id: string, props: APIResourceProps) {
    super(scope, id, props);

    new CfnDocumentationPart(this, 'siteDocs', {
      restApiId: props.gateway.restApiId,
      location: {
        type: 'RESOURCE',
        method: '*',
        path: APISiteStack.apiBasePath,
        statusCode: '405',
      },
      properties: JSON.stringify({
        "status": "error",
        "code": 405,
        "message": "Method Not Allowed"
      }),
    });

    new CfnDocumentationVersion(this, 'apiDocsVersion', {
      restApiId: props.gateway.restApiId,
      documentationVersion: config.app.name(`API-${config.gateway.api.version}`),
      description: 'Spare-It API Documentation',
    });

  }
}

Upvotes: 1

user3401323
user3401323

Reputation: 95

I have tested with CDK 1.31 and it is possible to use the CDK's default deployment option and also add a document version to the stage. I have used the deployOptions.documentVersion in rest api definition to set the version identifier of the API documentation:

import * as cdk from '@aws-cdk/core';
import * as apigateway from "@aws-cdk/aws-apigateway";
import {CfnDocumentationPart, CfnDocumentationVersion} from "@aws-cdk/aws-apigateway";

export class CdkSftpStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const documentVersion = "v1";

    // create the API
    const api = new apigateway.RestApi(this, 'books-api', {
      deploy: true,
      deployOptions: {
        documentationVersion: documentVersion
      }
    });

    // create GET method on /books resource
    const books = api.root.addResource('books');
    books.addMethod('GET');

    // // create documentation for GET method
    new CfnDocumentationPart(this, 'doc-part1', {
      location: {
        type: 'METHOD',
        method: 'GET',
        path: books.path
      },
      properties: JSON.stringify({
        "status": "successful",
        "code": 200,
        "message": "Get method was succcessful"
      }),
      restApiId: api.restApiId
    });

    new CfnDocumentationVersion(this, 'docVersion1', {
      documentationVersion: documentVersion,
      restApiId: api.restApiId,
      description: 'this is a test of documentation'
    });
  }
}

enter image description here

Upvotes: 3

jmp
jmp

Reputation: 2375

From what I can gather, if you use the CDK's default deployment options which create stage and deployment on your behalf, it won't be possible to append the stage with a documentation version set.

Instead, the solution would be to set the RESTAPI's option object to deploy:false and define the stage and deployment manually.

stack.ts code

import * as cdk from '@aws-cdk/core';
import * as apigateway from '@aws-cdk/aws-apigateway';
import { Stage, Deployment, CfnDocumentationPart, CfnDocumentationVersion, CfnDeployment } from '@aws-cdk/aws-apigateway';

export class StackoverflowHowToDocumentRestApiUsingAwsCdkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // create the API, need to not rely on CFN's automatic deployment because we need to 
    // make our own deployment to set the documentation we create
    const api = new apigateway.RestApi(this, 'books-api',{
      deploy: false
    });

    // create GET method on /books resource
    const books = api.root.addResource('books');
    books.addMethod('GET');

    // // create documentation for GET method
    const docpart = new CfnDocumentationPart(this, 'doc-part1', {
      location: {
        type: 'METHOD',
        method: 'GET',
        path: books.path
      },
      properties: JSON.stringify({
        "status": "successful",
        "code": 200,
        "message": "Get method was succcessful"
      }),
      restApiId: api.restApiId
    });

    const doc = new CfnDocumentationVersion(this, 'docVersion1', {
      documentationVersion: 'version1',
      restApiId: api.restApiId,
      description: 'this is a test of documentation'
    });
    // not sure if this is necessary but it made sense to me
    doc.addDependsOn(docpart);

    const deployment = api.latestDeployment ? api.latestDeployment: new Deployment(this,'newDeployment',{
      api: api,
      description: 'new deployment, API Gateway did not make one'
    });
    // create stage of api with documentation version
    const stage = new Stage(this, 'books-api-stage1', {
      deployment:  deployment,
      documentationVersion: doc.documentationVersion,
      stageName: 'somethingOtherThanProd'
    });
  }
}

OUTPUT:

APIGateway web console

Created a feature request for this option here.

Upvotes: 1

Related Questions