Table of Contents

Class RestApiAttributes

Namespace
Amazon.CDK.AWS.APIGateway
Assembly
Amazon.CDK.AWS.APIGateway.dll

Attributes that can be specified when importing a RestApi.

public class RestApiAttributes : IRestApiAttributes
Inheritance
RestApiAttributes
Implements
Inherited Members

Examples

using Amazon.CDK;
            using Constructs;
            using Amazon.CDK.AWS.APIGateway;

            /**
             * This file showcases how to split up a RestApi's Resources and Methods across nested stacks.
             *
             * The root stack 'RootStack' first defines a RestApi.
             * Two nested stacks BooksStack and PetsStack, create corresponding Resources '/books' and '/pets'.
             * They are then deployed to a 'prod' Stage via a third nested stack - DeployStack.
             *
             * To verify this worked, go to the APIGateway
             */

            class RootStack : Stack
            {
                public RootStack(Construct scope) : base(scope, "integ-restapi-import-RootStack")
                {

                    var restApi = new RestApi(this, "RestApi", new RestApiProps {
                        Deploy = false
                    });
                    restApi.Root.AddMethod("ANY");

                    var petsStack = new PetsStack(this, new ResourceNestedStackProps {
                        RestApiId = restApi.RestApiId,
                        RootResourceId = restApi.RestApiRootResourceId
                    });
                    var booksStack = new BooksStack(this, new ResourceNestedStackProps {
                        RestApiId = restApi.RestApiId,
                        RootResourceId = restApi.RestApiRootResourceId
                    });
                    new DeployStack(this, new DeployStackProps {
                        RestApiId = restApi.RestApiId,
                        Methods = petsStack.Methods.Concat(booksStack.Methods)
                    });

                    new CfnOutput(this, "PetsURL", new CfnOutputProps {
                        Value = $"https://{restApi.restApiId}.execute-api.{this.region}.amazonaws.com/prod/pets"
                    });

                    new CfnOutput(this, "BooksURL", new CfnOutputProps {
                        Value = $"https://{restApi.restApiId}.execute-api.{this.region}.amazonaws.com/prod/books"
                    });
                }
            }

            class ResourceNestedStackProps : NestedStackProps
            {
                public string RestApiId { get; set; }

                public string RootResourceId { get; set; }
            }

            class PetsStack : NestedStack
            {
                public readonly Method[] Methods = new [] {  };

                public PetsStack(Construct scope, ResourceNestedStackProps props) : base(scope, "integ-restapi-import-PetsStack", props)
                {

                    var api = RestApi.FromRestApiAttributes(this, "RestApi", new RestApiAttributes {
                        RestApiId = props.RestApiId,
                        RootResourceId = props.RootResourceId
                    });

                    var method = api.Root.AddResource("pets").AddMethod("GET", new MockIntegration(new IntegrationOptions {
                        IntegrationResponses = new [] { new IntegrationResponse {
                            StatusCode = "200"
                        } },
                        PassthroughBehavior = PassthroughBehavior.NEVER,
                        RequestTemplates = new Dictionary<string, string> {
                            { "application/json", "{ \"statusCode\": 200 }" }
                        }
                    }), new MethodOptions {
                        MethodResponses = new [] { new MethodResponse { StatusCode = "200" } }
                    });

                    Methods.Push(method);
                }
            }

            class BooksStack : NestedStack
            {
                public readonly Method[] Methods = new [] {  };

                public BooksStack(Construct scope, ResourceNestedStackProps props) : base(scope, "integ-restapi-import-BooksStack", props)
                {

                    var api = RestApi.FromRestApiAttributes(this, "RestApi", new RestApiAttributes {
                        RestApiId = props.RestApiId,
                        RootResourceId = props.RootResourceId
                    });

                    var method = api.Root.AddResource("books").AddMethod("GET", new MockIntegration(new IntegrationOptions {
                        IntegrationResponses = new [] { new IntegrationResponse {
                            StatusCode = "200"
                        } },
                        PassthroughBehavior = PassthroughBehavior.NEVER,
                        RequestTemplates = new Dictionary<string, string> {
                            { "application/json", "{ \"statusCode\": 200 }" }
                        }
                    }), new MethodOptions {
                        MethodResponses = new [] { new MethodResponse { StatusCode = "200" } }
                    });

                    Methods.Push(method);
                }
            }

            class DeployStackProps : NestedStackProps
            {
                public string RestApiId { get; set; }

                public Method[]? Methods { get; set; }
            }

            class DeployStack : NestedStack
            {
                public DeployStack(Construct scope, DeployStackProps props) : base(scope, "integ-restapi-import-DeployStack", props)
                {

                    var deployment = new Deployment(this, "Deployment", new DeploymentProps {
                        Api = RestApi.FromRestApiId(this, "RestApi", props.RestApiId)
                    });
                    if (props.Methods)
                    {
                        for (var method in props.Methods)
                        {
                            deployment.Node.AddDependency(method);
                        }
                    }
                    new Stage(this, "Stage", new StageProps { Deployment = deployment });
                }
            }

            new RootStack(new App());

Remarks

ExampleMetadata: lit=test/integ.restapi-import.lit.ts infused

Constructors

RestApiAttributes()

public RestApiAttributes()

Properties

RestApiId

The ID of the API Gateway RestApi.

public string RestApiId { get; set; }

Property Value

string

RootResourceId

The resource ID of the root resource.

public string RootResourceId { get; set; }

Property Value

string