# CustomResourceProvider

An AWS-Lambda backed custom resource provider, for CDK Construct Library constructs.

This is a provider for `CustomResource` constructs, backed by an AWS Lambda
Function. It only supports NodeJS runtimes.

> **Application builders do not need to use this provider type**. This is not
> a generic custom resource provider class. It is specifically
> intended to be used only by constructs in the AWS CDK Construct Library, and
> only exists here because of reverse dependency issues (for example, it cannot
> use `iam.PolicyStatement` objects, since the `iam` library already depends on
> the CDK `core` library and we cannot have cyclic dependencies).

If you are not writing constructs for the AWS Construct Library, you should
use the `Provider` class in the `custom-resources` module instead, which has
a better API and supports all Lambda runtimes, not just Node.

N.B.: When you are writing Custom Resource Providers, there are a number of
lifecycle events you have to pay attention to. These are documented in the
README of the `custom-resources` module. Be sure to give the documentation
in that module a read, regardless of whether you end up using the Provider
class in there or this one.

Extends: Construct › CustomResourceProviderBase › CustomResourceProvider
Implements: IConstruct, IDependable

- Source: [`core/lib/custom-resource-provider/custom-resource-provider.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/core/lib/custom-resource-provider/custom-resource-provider.ts#L98)
- Type last changed: v2.116.0 · 22 Dec 2023

## Example

```ts
const provider = CustomResourceProvider.getOrCreateProvider(this, 'Custom::MyCustomResourceType', {
  codeDirectory: `${__dirname}/my-handler`,
  runtime: CustomResourceProviderRuntime.NODEJS_22_X,
});
provider.addToRolePolicy({
  Effect: 'Allow',
  Action: 's3:GetObject',
  Resource: '*',
})
```

## Properties

- `codeHash`: string — The hash of the lambda code backing this provider.

  Can be used to trigger updates
  on code changes, even when the properties of a custom resource remain unchanged.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `roleArn`: string — The ARN of the provider's AWS Lambda function role.

- `serviceToken`: string — The ARN of the provider's AWS Lambda function which should be used as the `serviceToken` when defining a custom resource.

## Methods

- `addToRolePolicy(statement: any): void` — Add an IAM policy statement to the inline policy of the provider's lambda function's role.

  **Please note**: this is a direct IAM JSON policy blob, *not* a `iam.PolicyStatement`
  object like you will see in the rest of the CDK.

  ```ts
  declare const myProvider: CustomResourceProvider;

  myProvider.addToRolePolicy({
    Effect: 'Allow',
    Action: 's3:GetObject',
    Resource: '*',
  });
  ```

- `getOrCreate(scope: Construct, uniqueid: string, props: CustomResourceProviderProps): string` (static) — Returns a stack-level singleton ARN (service token) for the custom resource provider.

  **Parameters**

  - `scope` — Construct scope.
  - `uniqueid` — A globally unique id that will be used for the stack-level construct.
  - `props` — Provider properties which will only be applied when the provider is first created.

  **Returns:** the service token of the custom resource provider, which should be used when defining a `CustomResource`.

- `getOrCreateProvider(scope: Construct, uniqueid: string, props: CustomResourceProviderProps): CustomResourceProvider // chainable` (static) — Returns a stack-level singleton for the custom resource provider.

  **Parameters**

  - `scope` — Construct scope.
  - `uniqueid` — A globally unique id that will be used for the stack-level construct.
  - `props` — Provider properties which will only be applied when the provider is first created.

  **Returns:** the service token of the custom resource provider, which should be used when defining a `CustomResource`.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
