class ApiDefinition
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.APIGateway.ApiDefinition |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway#ApiDefinition |
Java | software.amazon.awscdk.services.apigateway.ApiDefinition |
Python | aws_cdk.aws_apigateway.ApiDefinition |
TypeScript (source) | aws-cdk-lib » aws_apigateway » ApiDefinition |
Implemented by
Asset
, Inline
, S3
Represents an OpenAPI definition asset.
Example
const myApiDefinition = apigateway.ApiDefinition.fromAsset('path-to-file.json');
const specRestApi = new apigateway.SpecRestApi(this, 'my-specrest-api', {
deploy: false,
apiDefinition: myApiDefinition
});
// Use `stageName` to deploy to an existing stage
const deployment = new apigateway.Deployment(this, 'my-deployment', {
api: specRestApi,
stageName: 'dev',
retainDeployments: true, // keep old deployments
});
// Trigger a new deployment on OpenAPI definition updates
deployment.addToLogicalId(myApiDefinition);
Initializer
new ApiDefinition()
Methods
Name | Description |
---|---|
bind(scope) | Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun. |
bind | Called after the CFN RestApi resource has been created to allow the Api Definition to bind to it. |
static from | Loads the API specification from a local disk asset. |
static from | Creates an API definition from a specification file in an S3 bucket. |
static from | Create an API definition from an inline object. |
bind(scope)
public bind(scope: Construct): ApiDefinitionConfig
Parameters
- scope
Construct
— The binding scope.
Returns
Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.
AfterCreate(_scope, _restApi)
bindpublic bindAfterCreate(_scope: Construct, _restApi: IRestApi): void
Parameters
Called after the CFN RestApi resource has been created to allow the Api Definition to bind to it.
Specifically it's required to allow assets to add metadata for tooling like SAM CLI to be able to find their origins.
Asset(file, options?)
static frompublic static fromAsset(file: string, options?: AssetOptions): AssetApiDefinition
Parameters
- file
string
- options
Asset
Options
Returns
Loads the API specification from a local disk asset.
Bucket(bucket, key, objectVersion?)
static frompublic static fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3ApiDefinition
Parameters
- bucket
IBucket
- key
string
- objectVersion
string
Returns
Creates an API definition from a specification file in an S3 bucket.
Inline(definition)
static frompublic static fromInline(definition: any): InlineApiDefinition
Parameters
- definition
any
Returns
Create an API definition from an inline object.
The inline object must follow the schema of OpenAPI 2.0 or OpenAPI 3.0 Example
apigateway.ApiDefinition.fromInline({
openapi: '3.0.2',
paths: {
'/pets': {
get: {
'responses': {
200: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Empty',
},
},
},
},
},
'x-amazon-apigateway-integration': {
responses: {
default: {
statusCode: '200',
},
},
requestTemplates: {
'application/json': '{"statusCode": 200}',
},
passthroughBehavior: 'when_no_match',
type: 'mock',
},
},
},
},
components: {
schemas: {
Empty: {
title: 'Empty Schema',
type: 'object',
},
},
},
});