class Resource (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.APIGateway.Resource |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway#Resource |
Java | software.amazon.awscdk.services.apigateway.Resource |
Python | aws_cdk.aws_apigateway.Resource |
TypeScript (source) | aws-cdk-lib » aws_apigateway » Resource |
Implements
IConstruct
, IDependable
, IResource
, IResource
Example
declare const booksBackend: apigateway.LambdaIntegration;
const api = new apigateway.RestApi(this, 'books', {
defaultIntegration: booksBackend
});
const books = api.root.addResource('books');
books.addMethod('GET'); // integrated with `booksBackend`
books.addMethod('POST'); // integrated with `booksBackend`
const book = books.addResource('{book_id}');
book.addMethod('GET'); // integrated with `booksBackend`
Initializer
new Resource(scope: Construct, id: string, props: ResourceProps)
Parameters
- scope
Construct
- id
string
- props
Resource
Props
Construct Props
Name | Type | Description |
---|---|---|
parent | IResource | The parent resource of this resource. |
path | string | A path name for the resource. |
default | Cors | Adds a CORS preflight OPTIONS method to this resource and all child resources. |
default | Integration | An integration to use as a default for all methods created within this API unless an integration is specified. |
default | Method | Method options to use as a default for all methods created within this API unless custom options are specified. |
parent
Type:
IResource
The parent resource of this resource.
You can either pass another
Resource
object or a RestApi
object here.
pathPart
Type:
string
A path name for the resource.
defaultCorsPreflightOptions?
Type:
Cors
(optional, default: CORS is disabled)
Adds a CORS preflight OPTIONS method to this resource and all child resources.
You can add CORS at the resource-level using addCorsPreflight
.
defaultIntegration?
Type:
Integration
(optional, default: Inherited from parent.)
An integration to use as a default for all methods created within this API unless an integration is specified.
defaultMethodOptions?
Type:
Method
(optional, default: Inherited from parent.)
Method options to use as a default for all methods created within this API unless custom options are specified.
Properties
Name | Type | Description |
---|---|---|
api | IRest | The rest API that this resource is part of. |
env | Resource | The environment this resource belongs to. |
node | Node | The tree node. |
path | string | The full path of this resource. |
resource | string | The ID of the resource. |
stack | Stack | The stack in which this resource is defined. |
default | Cors | Default options for CORS preflight OPTIONS method. |
default | Integration | An integration to use as a default for all methods created within this API unless an integration is specified. |
default | Method | Method options to use as a default for all methods created within this API unless custom options are specified. |
parent | IResource | The parent of this resource or undefined for the root resource. |
api
Type:
IRest
The rest API that this resource is part of.
The reason we need the RestApi object itself and not just the ID is because the model is being tracked by the top-level RestApi object for the purpose of calculating it's hash to determine the ID of the deployment. This allows us to automatically update the deployment when the model of the REST API changes.
env
Type:
Resource
The environment this resource belongs to.
For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.
node
Type:
Node
The tree node.
path
Type:
string
The full path of this resource.
resourceId
Type:
string
The ID of the resource.
stack
Type:
Stack
The stack in which this resource is defined.
defaultCorsPreflightOptions?
Type:
Cors
(optional)
Default options for CORS preflight OPTIONS method.
defaultIntegration?
Type:
Integration
(optional)
An integration to use as a default for all methods created within this API unless an integration is specified.
defaultMethodOptions?
Type:
Method
(optional)
Method options to use as a default for all methods created within this API unless custom options are specified.
parentResource?
Type:
IResource
(optional)
The parent of this resource or undefined for the root resource.
Methods
Name | Description |
---|---|
add | Adds an OPTIONS method to this resource which responds to Cross-Origin Resource Sharing (CORS) preflight requests. |
add | Defines a new method for this resource. |
add | Adds a greedy proxy resource ("{proxy+}") and an ANY method to this route. |
add | Defines a new child resource where this resource is the parent. |
apply | Apply the given removal policy to this resource. |
get | Retrieves a child resource by path part. |
resource | Gets or create all resources leading up to the specified path. |
to | Returns a string representation of this construct. |
static from | Import an existing resource. |
CorsPreflight(options)
addpublic addCorsPreflight(options: CorsOptions): Method
Parameters
- options
Cors
Options
Returns
Adds an OPTIONS method to this resource which responds to Cross-Origin Resource Sharing (CORS) preflight requests.
Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.
Method(httpMethod, integration?, options?)
addpublic addMethod(httpMethod: string, integration?: Integration, options?: MethodOptions): Method
Parameters
- httpMethod
string
- integration
Integration
- options
Method
Options
Returns
Defines a new method for this resource.
Proxy(options?)
addpublic addProxy(options?: ProxyResourceOptions): ProxyResource
Parameters
- options
Proxy
Resource Options
Returns
Adds a greedy proxy resource ("{proxy+}") and an ANY method to this route.
Resource(pathPart, options?)
addpublic addResource(pathPart: string, options?: ResourceOptions): Resource
Parameters
- pathPart
string
- options
Resource
Options
Returns
Defines a new child resource where this resource is the parent.
RemovalPolicy(policy)
applypublic applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
Removal
Policy
Apply the given removal policy to this resource.
The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.
The resource can be deleted (RemovalPolicy.DESTROY
), or left in your AWS
account for data recovery and cleanup later (RemovalPolicy.RETAIN
).
Resource(pathPart)
getpublic getResource(pathPart: string): IResource
Parameters
- pathPart
string
Returns
Retrieves a child resource by path part.
ForPath(path)
resourcepublic resourceForPath(path: string): Resource
Parameters
- path
string
Returns
Gets or create all resources leading up to the specified path.
- Path may only start with "/" if this method is called on the root resource.
- All resources are created using default options.
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
ResourceAttributes(scope, id, attrs)
static frompublic static fromResourceAttributes(scope: Construct, id: string, attrs: ResourceAttributes): IResource
Parameters
- scope
Construct
- id
string
- attrs
Resource
Attributes
Returns
Import an existing resource.