RestApiAttributes Struct
Attributes that can be specified when importing a RestApi.
| Source | aws-apigateway/lib/restapi.ts |
|---|---|
| Type last changed |
Example
ts
import type { Construct } from 'constructs';
import type { NestedStackProps } from '../../core';
import { App, CfnOutput, NestedStack, Stack } from '../../core';
import type { Method } from '../lib';
import { Deployment, MockIntegration, PassthroughBehavior, RestApi, Stage } from '../lib';
/**
* 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 extends Stack {
constructor(scope: Construct) {
super(scope, 'integ-restapi-import-RootStack');
const restApi = new RestApi(this, 'RestApi', {
cloudWatchRole: true,
deploy: false,
});
restApi.root.addMethod('ANY');
const petsStack = new PetsStack(this, {
restApiId: restApi.restApiId,
rootResourceId: restApi.restApiRootResourceId,
});
const booksStack = new BooksStack(this, {
restApiId: restApi.restApiId,
rootResourceId: restApi.restApiRootResourceId,
});
new DeployStack(this, {
restApiId: restApi.restApiId,
methods: petsStack.methods.concat(booksStack.methods),
});
new CfnOutput(this, 'PetsURL', {
value: `https://${restApi.restApiId}.execute-api.${this.region}.amazonaws.com/prod/pets`,
});
new CfnOutput(this, 'BooksURL', {
value: `https://${restApi.restApiId}.execute-api.${this.region}.amazonaws.com/prod/books`,
});
}
}
interface ResourceNestedStackProps extends NestedStackProps {
readonly restApiId: string;
readonly rootResourceId: string;
}
class PetsStack extends NestedStack {
public readonly methods: Method[] = [];
constructor(scope: Construct, props: ResourceNestedStackProps) {
super(scope, 'integ-restapi-import-PetsStack', props);
const api = RestApi.fromRestApiAttributes(this, 'RestApi', {
restApiId: props.restApiId,
rootResourceId: props.rootResourceId,
});
const method = api.root.addResource('pets').addMethod('GET', new MockIntegration({
integrationResponses: [{
statusCode: '200',
}],
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: {
'application/json': '{ "statusCode": 200 }',
},
}), {
methodResponses: [{ statusCode: '200' }],
});
this.methods.push(method);
}
}
class BooksStack extends NestedStack {
public readonly methods: Method[] = [];
constructor(scope: Construct, props: ResourceNestedStackProps) {
super(scope, 'integ-restapi-import-BooksStack', props);
const api = RestApi.fromRestApiAttributes(this, 'RestApi', {
restApiId: props.restApiId,
rootResourceId: props.rootResourceId,
});
const method = api.root.addResource('books').addMethod('GET', new MockIntegration({
integrationResponses: [{
statusCode: '200',
}],
passthroughBehavior: PassthroughBehavior.NEVER,
requestTemplates: {
'application/json': '{ "statusCode": 200 }',
},
}), {
methodResponses: [{ statusCode: '200' }],
});
this.methods.push(method);
}
}
interface DeployStackProps extends NestedStackProps {
readonly restApiId: string;
readonly methods?: Method[];
}
class DeployStack extends NestedStack {
constructor(scope: Construct, props: DeployStackProps) {
super(scope, 'integ-restapi-import-DeployStack', props);
const deployment = new Deployment(this, 'Deployment', {
api: RestApi.fromRestApiId(this, 'RestApi', props.restApiId),
});
if (props.methods) {
for (const method of props.methods) {
deployment.node.addDependency(method);
}
}
new Stage(this, 'Stage', { deployment });
}
}
new RootStack(new App());
python
from aws_cdk.aws_apigateway import IntegrationResponse, MethodResponse, IntegrationResponse, MethodResponse
from constructs import Construct
from aws_cdk import NestedStackProps
from aws_cdk import App, CfnOutput, NestedStack, Stack
from aws_cdk.aws_apigateway import Method
from aws_cdk.aws_apigateway import Deployment, MockIntegration, PassthroughBehavior, RestApi, Stage
#
# 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):
def __init__(self, scope):
super().__init__(scope, "integ-restapi-import-RootStack")
rest_api = RestApi(self, "RestApi",
cloud_watch_role=True,
deploy=False
)
rest_api.root.add_method("ANY")
pets_stack = PetsStack(self,
rest_api_id=rest_api.rest_api_id,
root_resource_id=rest_api.rest_api_root_resource_id
)
books_stack = BooksStack(self,
rest_api_id=rest_api.rest_api_id,
root_resource_id=rest_api.rest_api_root_resource_id
)
DeployStack(self,
rest_api_id=rest_api.rest_api_id,
methods=pets_stack.methods.concat(books_stack.methods)
)
CfnOutput(self, "PetsURL",
value=f"https://{restApi.restApiId}.execute-api.{this.region}.amazonaws.com/prod/pets"
)
CfnOutput(self, "BooksURL",
value=f"https://{restApi.restApiId}.execute-api.{this.region}.amazonaws.com/prod/books"
)
class PetsStack(NestedStack):
def __init__(self, scope, *, restApiId, rootResourceId, parameters=None, timeout=None, notificationArns=None, removalPolicy=None, description=None, suppressTemplateIndentation=None):
super().__init__(scope, "integ-restapi-import-PetsStack", restApiId=restApiId, rootResourceId=rootResourceId, parameters=parameters, timeout=timeout, notificationArns=notificationArns, removalPolicy=removalPolicy, description=description, suppressTemplateIndentation=suppressTemplateIndentation)
api = RestApi.from_rest_api_attributes(self, "RestApi",
rest_api_id=rest_api_id,
root_resource_id=root_resource_id
)
method = api.root.add_resource("pets").add_method("GET", MockIntegration(
integration_responses=[IntegrationResponse(
status_code="200"
)],
passthrough_behavior=PassthroughBehavior.NEVER,
request_templates={
"application/json": "{ \"statusCode\": 200 }"
}
),
method_responses=[MethodResponse(status_code="200")]
)
self.methods.push(method)
class BooksStack(NestedStack):
def __init__(self, scope, *, restApiId, rootResourceId, parameters=None, timeout=None, notificationArns=None, removalPolicy=None, description=None, suppressTemplateIndentation=None):
super().__init__(scope, "integ-restapi-import-BooksStack", restApiId=restApiId, rootResourceId=rootResourceId, parameters=parameters, timeout=timeout, notificationArns=notificationArns, removalPolicy=removalPolicy, description=description, suppressTemplateIndentation=suppressTemplateIndentation)
api = RestApi.from_rest_api_attributes(self, "RestApi",
rest_api_id=rest_api_id,
root_resource_id=root_resource_id
)
method = api.root.add_resource("books").add_method("GET", MockIntegration(
integration_responses=[IntegrationResponse(
status_code="200"
)],
passthrough_behavior=PassthroughBehavior.NEVER,
request_templates={
"application/json": "{ \"statusCode\": 200 }"
}
),
method_responses=[MethodResponse(status_code="200")]
)
self.methods.push(method)
class DeployStack(NestedStack):
def __init__(self, scope, *, restApiId, methods=None, parameters=None, timeout=None, notificationArns=None, removalPolicy=None, description=None, suppressTemplateIndentation=None):
super().__init__(scope, "integ-restapi-import-DeployStack", restApiId=restApiId, methods=methods, parameters=parameters, timeout=timeout, notificationArns=notificationArns, removalPolicy=removalPolicy, description=description, suppressTemplateIndentation=suppressTemplateIndentation)
deployment = Deployment(self, "Deployment",
api=RestApi.from_rest_api_id(self, "RestApi", rest_api_id)
)
if methods:
for method in methods:
deployment.node.add_dependency(method)
Stage(self, "Stage", deployment=deployment)
RootStack(App())
java
import software.constructs.Construct;
import software.amazon.awscdk.NestedStackProps;
import software.amazon.awscdk.App;
import software.amazon.awscdk.CfnOutput;
import software.amazon.awscdk.NestedStack;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.services.apigateway.Method;
import software.amazon.awscdk.services.apigateway.Deployment;
import software.amazon.awscdk.services.apigateway.MockIntegration;
import software.amazon.awscdk.services.apigateway.PassthroughBehavior;
import software.amazon.awscdk.services.apigateway.RestApi;
import software.amazon.awscdk.services.apigateway.Stage;
/**
* 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
*/
public class RootStack extends Stack {
public RootStack(Construct scope) {
super(scope, "integ-restapi-import-RootStack");
RestApi restApi = RestApi.Builder.create(this, "RestApi")
.cloudWatchRole(true)
.deploy(false)
.build();
restApi.root.addMethod("ANY");
PetsStack petsStack = new PetsStack(this, new ResourceNestedStackProps()
.restApiId(restApi.getRestApiId())
.rootResourceId(restApi.getRestApiRootResourceId())
);
BooksStack booksStack = new BooksStack(this, new ResourceNestedStackProps()
.restApiId(restApi.getRestApiId())
.rootResourceId(restApi.getRestApiRootResourceId())
);
new DeployStack(this, new DeployStackProps()
.restApiId(restApi.getRestApiId())
.methods(petsStack.methods.concat(booksStack.getMethods()))
);
CfnOutput.Builder.create(this, "PetsURL")
.value(String.format("https://%s.execute-api.%s.amazonaws.com/prod/pets", restApi.getRestApiId(), this.region))
.build();
CfnOutput.Builder.create(this, "BooksURL")
.value(String.format("https://%s.execute-api.%s.amazonaws.com/prod/books", restApi.getRestApiId(), this.region))
.build();
}
}
public class ResourceNestedStackProps extends NestedStackProps {
private String restApiId;
public String getRestApiId() {
return this.restApiId;
}
public ResourceNestedStackProps restApiId(String restApiId) {
this.restApiId = restApiId;
return this;
}
private String rootResourceId;
public String getRootResourceId() {
return this.rootResourceId;
}
public ResourceNestedStackProps rootResourceId(String rootResourceId) {
this.rootResourceId = rootResourceId;
return this;
}
}
public class PetsStack extends NestedStack {
public final Method[] methods;
public PetsStack(Construct scope, ResourceNestedStackProps props) {
super(scope, "integ-restapi-import-PetsStack", props);
IRestApi api = RestApi.fromRestApiAttributes(this, "RestApi", RestApiAttributes.builder()
.restApiId(props.getRestApiId())
.rootResourceId(props.getRootResourceId())
.build());
Method method = api.root.addResource("pets").addMethod("GET", MockIntegration.Builder.create()
.integrationResponses(List.of(IntegrationResponse.builder()
.statusCode("200")
.build()))
.passthroughBehavior(PassthroughBehavior.NEVER)
.requestTemplates(Map.of(
"application/json", "{ \"statusCode\": 200 }"))
.build(), MethodOptions.builder()
.methodResponses(List.of(MethodResponse.builder().statusCode("200").build()))
.build());
this.methods.push(method);
}
}
public class BooksStack extends NestedStack {
public final Method[] methods;
public BooksStack(Construct scope, ResourceNestedStackProps props) {
super(scope, "integ-restapi-import-BooksStack", props);
IRestApi api = RestApi.fromRestApiAttributes(this, "RestApi", RestApiAttributes.builder()
.restApiId(props.getRestApiId())
.rootResourceId(props.getRootResourceId())
.build());
Method method = api.root.addResource("books").addMethod("GET", MockIntegration.Builder.create()
.integrationResponses(List.of(IntegrationResponse.builder()
.statusCode("200")
.build()))
.passthroughBehavior(PassthroughBehavior.NEVER)
.requestTemplates(Map.of(
"application/json", "{ \"statusCode\": 200 }"))
.build(), MethodOptions.builder()
.methodResponses(List.of(MethodResponse.builder().statusCode("200").build()))
.build());
this.methods.push(method);
}
}
public class DeployStackProps extends NestedStackProps {
private String restApiId;
public String getRestApiId() {
return this.restApiId;
}
public DeployStackProps restApiId(String restApiId) {
this.restApiId = restApiId;
return this;
}
private Method[] methods;
public Method[] getMethods() {
return this.methods;
}
public DeployStackProps methods(Method[] methods) {
this.methods = methods;
return this;
}
}
public class DeployStack extends NestedStack {
public DeployStack(Construct scope, DeployStackProps props) {
super(scope, "integ-restapi-import-DeployStack", props);
Deployment deployment = Deployment.Builder.create(this, "Deployment")
.api(RestApi.fromRestApiId(this, "RestApi", props.getRestApiId()))
.build();
if (props.getMethods()) {
for (Object method : props.getMethods()) {
deployment.node.addDependency(method);
}
}
Stage.Builder.create(this, "Stage").deployment(deployment).build();
}
}
new RootStack(new App());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Constructs;
using Amazon.CDK;
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 {
CloudWatchRole = true,
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());
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/constructs-go/constructs"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
/**
* 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
*/
type rootStack struct {
Stack
}
func newRootStack(scope Construct) *rootStack {
this := &rootStack{}
newStack_Override(this, scope, jsii.String("integ-restapi-import-RootStack"))
restApi := awscdk.NewRestApi(this, jsii.String("RestApi"), &RestApiProps{
CloudWatchRole: jsii.Boolean(true),
Deploy: jsii.Boolean(false),
})
restApi.Root.AddMethod(jsii.String("ANY"))
petsStack := newPetsStack(this, &resourceNestedStackProps{
restApiId: restApi.RestApiId,
rootResourceId: restApi.RestApiRootResourceId,
})
booksStack := newBooksStack(this, &resourceNestedStackProps{
restApiId: restApi.*RestApiId,
rootResourceId: restApi.*RestApiRootResourceId,
})
newDeployStack(this, &deployStackProps{
restApiId: restApi.*RestApiId,
methods: petsStack.methods.concat(booksStack.methods),
})
awscdk.NewCfnOutput(this, jsii.String("PetsURL"), &CfnOutputProps{
Value: fmt.Sprintf("https://%v.execute-api.%v.amazonaws.com/prod/pets", restApi.*RestApiId, this.Region),
})
awscdk.NewCfnOutput(this, jsii.String("BooksURL"), &CfnOutputProps{
Value: fmt.Sprintf("https://%v.execute-api.%v.amazonaws.com/prod/books", restApi.*RestApiId, this.*Region),
})
return this
}
type resourceNestedStackProps struct {
NestedStackProps
restApiId *string
rootResourceId *string
}
type petsStack struct {
NestedStack
methods []Method
}
func newPetsStack(scope Construct, props resourceNestedStackProps) *petsStack {
this := &petsStack{}
newNestedStack_Override(this, scope, jsii.String("integ-restapi-import-PetsStack"), props)
api := awscdk.RestApi_FromRestApiAttributes(this, jsii.String("RestApi"), &RestApiAttributes{
RestApiId: props.restApiId,
RootResourceId: props.rootResourceId,
})
method := api.Root.AddResource(jsii.String("pets")).AddMethod(jsii.String("GET"), awscdk.NewMockIntegration(&IntegrationOptions{
IntegrationResponses: []IntegrationResponse{
&IntegrationResponse{
StatusCode: jsii.String("200"),
},
},
PassthroughBehavior: awscdk.PassthroughBehavior_NEVER,
RequestTemplates: map[string]*string{
"application/json": jsii.String("{ \"statusCode\": 200 }"),
},
}), &MethodOptions{
MethodResponses: []MethodResponse{
&MethodResponse{
StatusCode: jsii.String("200"),
},
},
})
this.methods.push(method)
return this
}
type booksStack struct {
NestedStack
methods []Method
}
func newBooksStack(scope Construct, props resourceNestedStackProps) *booksStack {
this := &booksStack{}
newNestedStack_Override(this, scope, jsii.String("integ-restapi-import-BooksStack"), props)
api := awscdk.RestApi_FromRestApiAttributes(this, jsii.String("RestApi"), &RestApiAttributes{
RestApiId: props.restApiId,
RootResourceId: props.rootResourceId,
})
method := api.Root.AddResource(jsii.String("books")).AddMethod(jsii.String("GET"), awscdk.NewMockIntegration(&IntegrationOptions{
IntegrationResponses: []IntegrationResponse{
&IntegrationResponse{
StatusCode: jsii.String("200"),
},
},
PassthroughBehavior: awscdk.PassthroughBehavior_NEVER,
RequestTemplates: map[string]*string{
"application/json": jsii.String("{ \"statusCode\": 200 }"),
},
}), &MethodOptions{
MethodResponses: []MethodResponse{
&MethodResponse{
StatusCode: jsii.String("200"),
},
},
})
this.methods.push(method)
return this
}
type deployStackProps struct {
NestedStackProps
restApiId *string
methods []Method
}
type deployStack struct {
NestedStack
}
func newDeployStack(scope Construct, props deployStackProps) *deployStack {
this := &deployStack{}
newNestedStack_Override(this, scope, jsii.String("integ-restapi-import-DeployStack"), props)
deployment := awscdk.NewDeployment(this, jsii.String("Deployment"), &DeploymentProps{
Api: awscdk.RestApi_FromRestApiId(this, jsii.String("RestApi"), props.restApiId),
})
if *props.methods {
for _, method := range *props.methods {
deployment.Node.AddDependency(method)
}
}
awscdk.NewStage(this, jsii.String("Stage"), &StageProps{
Deployment: Deployment,
})
return this
}
newRootStack(awscdk.NewApp())
Properties
The ID of the API Gateway RestApi.
restApiNameoptionalstringrest_api_nameoptionalstrrestApiNameoptionalStringRestApiNameoptionalstringRestApiNameoptional*string #The name of the API Gateway RestApi.
Default: ID of the RestApi construct.
rootResourceIdstringroot_resource_idstrrootResourceIdStringRootResourceIdstringRootResourceId*string required #The resource ID of the root resource.