Skip to content
v2.273.0

AssetApiDefinition Class ​

OpenAPI specification from a local file.

Example ​

ts
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);
python
my_api_definition = apigateway.ApiDefinition.from_asset("path-to-file.json")
spec_rest_api = apigateway.SpecRestApi(self, "my-specrest-api",
    deploy=False,
    api_definition=my_api_definition
)

# Use `stageName` to deploy to an existing stage
deployment = apigateway.Deployment(self, "my-deployment",
    api=spec_rest_api,
    stage_name="dev",
    retain_deployments=True
)

# Trigger a new deployment on OpenAPI definition updates
deployment.add_to_logical_id(my_api_definition)
java
AssetApiDefinition myApiDefinition = ApiDefinition.fromAsset("path-to-file.json");
SpecRestApi specRestApi = SpecRestApi.Builder.create(this, "my-specrest-api")
        .deploy(false)
        .apiDefinition(myApiDefinition)
        .build();

// Use `stageName` to deploy to an existing stage
Deployment deployment = Deployment.Builder.create(this, "my-deployment")
        .api(specRestApi)
        .stageName("dev")
        .retainDeployments(true)
        .build();

// Trigger a new deployment on OpenAPI definition updates
deployment.addToLogicalId(myApiDefinition);
csharp
// Install: dotnet add package Amazon.CDK.Lib
var myApiDefinition = ApiDefinition.FromAsset("path-to-file.json");
var specRestApi = new SpecRestApi(this, "my-specrest-api", new SpecRestApiProps {
    Deploy = false,
    ApiDefinition = myApiDefinition
});

// Use `stageName` to deploy to an existing stage
var deployment = new Deployment(this, "my-deployment", new DeploymentProps {
    Api = specRestApi,
    StageName = "dev",
    RetainDeployments = true
});

// Trigger a new deployment on OpenAPI definition updates
deployment.AddToLogicalId(myApiDefinition);
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
myApiDefinition := apigateway.ApiDefinition_FromAsset(jsii.String("path-to-file.json"))
specRestApi := apigateway.NewSpecRestApi(this, jsii.String("my-specrest-api"), &SpecRestApiProps{
	Deploy: jsii.Boolean(false),
	ApiDefinition: myApiDefinition,
})

// Use `stageName` to deploy to an existing stage
deployment := apigateway.NewDeployment(this, jsii.String("my-deployment"), &DeploymentProps{
	Api: specRestApi,
	StageName: jsii.String("dev"),
	RetainDeployments: jsii.Boolean(true),
})

// Trigger a new deployment on OpenAPI definition updates
deployment.AddToLogicalId(myApiDefinition)

Initializer ​

ts
new AssetApiDefinition(path: string, options?: AssetOptions)
python
AssetApiDefinition(path: str, options: typing.Optional[AssetOptions] = None)
java
new AssetApiDefinition(String path, AssetOptions options)
csharp
new AssetApiDefinition(string path, AssetOptions? options = null)
go
NewAssetApiDefinition(Path *string, Options AssetOptions)

Methods ​

bind(scope: Construct): ApiDefinitionConfigdef bind(self, scope: Construct) -> ApiDefinitionConfigpublic ApiDefinitionConfig bind(Construct scope)public ApiDefinitionConfig Bind(Construct scope)func Bind(Scope Construct) ApiDefinitionConfig#
Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.
bindAfterCreate(scope: Construct, restApi: IRestApiRef): voiddef bind_after_create(self, scope: Construct, rest_api: IRestApiRef) -> Nonepublic void bindAfterCreate(Construct scope, IRestApiRef restApi)public void BindAfterCreate(Construct scope, IRestApiRef restApi)func BindAfterCreate(Scope Construct, RestApi IRestApiRef)#
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.

fromAsset(file: string, options?: AssetOptions): AssetApiDefinition // chainable@staticmethod def from_asset(file: str, options: typing.Optional[AssetOptions] = None) -> AssetApiDefinitionpublic static AssetApiDefinition fromAsset(String file, AssetOptions options)public static AssetApiDefinition FromAsset(string file, AssetOptions? options = null)func FromAsset(File *string, Options AssetOptions) AssetApiDefinitionstatic#
Loads the API specification from a local disk asset.
fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3ApiDefinition@staticmethod def from_bucket(bucket: IBucket, key: str, object_version: typing.Optional[str] = None) -> S3ApiDefinitionpublic static S3ApiDefinition fromBucket(IBucket bucket, String key, String objectVersion)public static S3ApiDefinition FromBucket(IBucket bucket, string key, string? objectVersion = null)func FromBucket(Bucket IBucket, Key *string, ObjectVersion *string) S3ApiDefinitionstatic#
Creates an API definition from a specification file in an S3 bucket.
fromInline(definition: any): InlineApiDefinition@staticmethod def from_inline(definition: Any) -> InlineApiDefinitionpublic static InlineApiDefinition fromInline(Object definition)public static InlineApiDefinition FromInline(object definition)func FromInline(Definition interface{}) InlineApiDefinitionstatic#
Create an API definition from an inline object.

The inline object must follow the schema of OpenAPI 2.0 or OpenAPI 3.0

example
ts
  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',
        },
      },
    },
  });