# RdsDataSource

An AppSync datasource backed by RDS.

Extends: Construct › BaseDataSource › BackedDataSource › RdsDataSource
Implements: IGrantable, IConstruct, IDependable

- Source: [`aws-appsync/lib/data-source.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-appsync/lib/data-source.ts#L455)
- Type last changed: v2.196.0 · 16 May 2025

## Example

```ts
// Create username and password secret for DB Cluster
const secret = new rds.DatabaseSecret(this, 'AuroraSecret', {
  username: 'clusteradmin',
});

// The VPC to place the cluster in
const vpc = new ec2.Vpc(this, 'AuroraVpc');

// Create the serverless cluster, provide all values needed to customise the database.
const cluster = new rds.ServerlessCluster(this, 'AuroraCluster', {
  engine: rds.DatabaseClusterEngine.AURORA_MYSQL,
  vpc,
  credentials: { username: 'clusteradmin' },
  clusterIdentifier: 'db-endpoint-test',
  defaultDatabaseName: 'demos',
});

// Build a data source for AppSync to access the database.
declare const api: appsync.GraphqlApi;
const rdsDS = api.addRdsDataSource('rds', cluster, secret, 'demos');

// Set up a resolver for an RDS query.
rdsDS.createResolver('QueryGetDemosRdsResolver', {
  typeName: 'Query',
  fieldName: 'getDemosRds',
  requestMappingTemplate: appsync.MappingTemplate.fromString(`
  {
    "version": "2018-05-29",
    "statements": [
      "SELECT * FROM demos"
    ]
  }
  `),
  responseMappingTemplate: appsync.MappingTemplate.fromString(`
    $utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
  `),
});

// Set up a resolver for an RDS mutation.
rdsDS.createResolver('MutationAddDemoRdsResolver', {
  typeName: 'Mutation',
  fieldName: 'addDemoRds',
  requestMappingTemplate: appsync.MappingTemplate.fromString(`
  {
    "version": "2018-05-29",
    "statements": [
      "INSERT INTO demos VALUES (:id, :version)",
      "SELECT * WHERE id = :id"
    ],
    "variableMap": {
      ":id": $util.toJson($util.autoId()),
      ":version": $util.toJson($ctx.args.version)
    }
  }
  `),
  responseMappingTemplate: appsync.MappingTemplate.fromString(`
    $utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0])
  `),
});
```

## Initializer

```ts
new RdsDataSource(scope: Construct, id: string, props: RdsDataSourceProps)
```

## Construct Props

- `api`: [IGraphQLApiRef](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_appsync.IGraphQLApiRef.html) (required) — The API to attach this data source to.

- `databaseName`: string = None — The name of the database to use within the cluster.

- `description`: string = None — the description of the data source.

- `metricsConfig`: [DataSourceMetricsConfig](/cdk/api/v2/docs/aws-cdk-lib.aws_appsync.DataSourceMetricsConfig.html) — Whether to enable enhanced metrics of the data source Value will be ignored, if `enhancedMetricsConfig.dataSourceLevelMetricsBehavior` on AppSync GraphqlApi construct is set to `FULL_REQUEST_DATA_SOURCE_METRICS`.

  **Default:** no metrics configuration

- `name`: string — The name of the data source.

  **Default:** id of data source

- `secretStore`: [ISecret](/cdk/api/v2/docs/aws-cdk-lib.aws_secretsmanager.ISecret.html) (required) — The secret containing the credentials for the database.

- `serverlessCluster`: [IServerlessCluster](/cdk/api/v2/docs/aws-cdk-lib.aws_rds.IServerlessCluster.html) (required) — The serverless cluster to call to interact with this data source.

- `serviceRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The IAM service role to be assumed by AppSync to interact with the data source.

  **Default:** Create a new role

## Properties

- `ds`: [CfnDataSource](/cdk/api/v2/docs/aws-cdk-lib.aws_appsync.CfnDataSource.html) — the underlying CFN data source resource.

- `grantPrincipal`: [IPrincipal](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IPrincipal.html) — the principal of the data source to be IGrantable.

- `name`: string — the name of the data source.

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

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

## Methods

- `createFunction(id: string, props: BaseAppsyncFunctionProps): AppsyncFunction` — creates a new appsync function for this datasource and API using the given properties.

- `createResolver(id: string, props: BaseResolverProps): Resolver` — creates a new resolver for this datasource and API using the given properties.

- `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
