# AppSyncRdsDataSource

An AppSync datasource backed by RDS.

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › [AppSyncBaseDataSource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_appsync.AppSyncBaseDataSource.html) › [AppSyncBackedDataSource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_appsync.AppSyncBackedDataSource.html) › AppSyncRdsDataSource
Implements: [IGrantable](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IGrantable.html), [IConstruct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IConstruct.html), [IDependable](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IDependable.html)

- Source: [`aws-appsync/lib/data-source-common.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-appsync/lib/data-source-common.ts#L451)
- Type last changed: v2.193.0 · 30 Apr 2025

## Example

TypeScript:

```ts
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';

declare const vpc: ec2.Vpc;
const databaseName = 'mydb';
const cluster = new rds.DatabaseCluster(this, 'Cluster', {
  engine: rds.DatabaseClusterEngine.auroraPostgres({ version: rds.AuroraPostgresEngineVersion.VER_16_6 }),
  writer: rds.ClusterInstance.serverlessV2('writer'),
  vpc: vpc,
  credentials: { username: 'clusteradmin' },
  defaultDatabaseName: databaseName,
  enableDataApi: true,
});

const secret = secretsmanager.Secret.fromSecretNameV2(this, 'Secret', 'db-secretName');

const api = new appsync.EventApi(this, 'EventApiRds', {
  apiName: 'RdsEventApi',
});

const dataSource = api.addRdsDataSource('rdsds', cluster, secret, databaseName);
```

Python:

```python
import aws_cdk.aws_secretsmanager as secretsmanager

# vpc: ec2.Vpc

database_name = "mydb"
cluster = rds.DatabaseCluster(self, "Cluster",
    engine=rds.DatabaseClusterEngine.aurora_postgres(version=rds.AuroraPostgresEngineVersion.VER_16_6),
    writer=rds.ClusterInstance.serverless_v2("writer"),
    vpc=vpc,
    credentials={"username": "clusteradmin"},
    default_database_name=database_name,
    enable_data_api=True
)

secret = secretsmanager.Secret.from_secret_name_v2(self, "Secret", "db-secretName")

api = appsync.EventApi(self, "EventApiRds",
    api_name="RdsEventApi"
)

data_source = api.add_rds_data_source("rdsds", cluster, secret, database_name)
```

Java:

```java
import software.amazon.awscdk.services.secretsmanager.*;

Vpc vpc;

String databaseName = "mydb";
DatabaseCluster cluster = DatabaseCluster.Builder.create(this, "Cluster")
        .engine(DatabaseClusterEngine.auroraPostgres(AuroraPostgresClusterEngineProps.builder().version(AuroraPostgresEngineVersion.VER_16_6).build()))
        .writer(ClusterInstance.serverlessV2("writer"))
        .vpc(vpc)
        .credentials(Map.of("username", "clusteradmin"))
        .defaultDatabaseName(databaseName)
        .enableDataApi(true)
        .build();

ISecret secret = Secret.fromSecretNameV2(this, "Secret", "db-secretName");

EventApi api = EventApi.Builder.create(this, "EventApiRds")
        .apiName("RdsEventApi")
        .build();

AppSyncRdsDataSource dataSource = api.addRdsDataSource("rdsds", cluster, secret, databaseName);
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.SecretsManager;

Vpc vpc;

var databaseName = "mydb";
var cluster = new DatabaseCluster(this, "Cluster", new DatabaseClusterProps {
    Engine = DatabaseClusterEngine.AuroraPostgres(new AuroraPostgresClusterEngineProps { Version = AuroraPostgresEngineVersion.VER_16_6 }),
    Writer = ClusterInstance.ServerlessV2("writer"),
    Vpc = vpc,
    Credentials = new Dictionary<string, string> { { "username", "clusteradmin" } },
    DefaultDatabaseName = databaseName,
    EnableDataApi = true
});

var secret = Secret.FromSecretNameV2(this, "Secret", "db-secretName");

var api = new EventApi(this, "EventApiRds", new EventApiProps {
    ApiName = "RdsEventApi"
});

var dataSource = api.AddRdsDataSource("rdsds", cluster, secret, databaseName);
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import secretsmanager "github.com/aws/aws-cdk-go/awscdk"

var vpc Vpc

databaseName := "mydb"
cluster := rds.NewDatabaseCluster(this, jsii.String("Cluster"), &DatabaseClusterProps{
	Engine: rds.DatabaseClusterEngine_AuroraPostgres(&AuroraPostgresClusterEngineProps{
		Version: rds.AuroraPostgresEngineVersion_VER_16_6(),
	}),
	Writer: rds.ClusterInstance_ServerlessV2(jsii.String("writer")),
	Vpc: vpc,
	Credentials: map[string]*string{
		"username": jsii.String("clusteradmin"),
	},
	DefaultDatabaseName: databaseName,
	EnableDataApi: jsii.Boolean(true),
})

secret := secretsmanager.Secret_FromSecretNameV2(this, jsii.String("Secret"), jsii.String("db-secretName"))

api := appsync.NewEventApi(this, jsii.String("EventApiRds"), &EventApiProps{
	ApiName: jsii.String("RdsEventApi"),
})

dataSource := api.AddRdsDataSource(jsii.String("rdsds"), cluster, secret, databaseName)
```

## Initializer

TypeScript:

```ts
new AppSyncRdsDataSource(scope: Construct, id: string, props: AppSyncRdsDataSourceProps)
```

Python:

```python
AppSyncRdsDataSource(scope: Construct, id: str, props: AppSyncRdsDataSourceProps)
```

Java:

```java
new AppSyncRdsDataSource(Construct scope, String id, AppSyncRdsDataSourceProps props)
```

C# / .NET:

```csharp
new AppSyncRdsDataSource(Construct scope, string id, AppSyncRdsDataSourceProps props)
```

Golang:

```go
NewAppSyncRdsDataSource(Scope Construct, Id *string, Props AppSyncRdsDataSourceProps)
```

## Construct Props

- `api`: [IApiRef](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_appsync.IApiRef.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.

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

  **Default:** id of data source

  The only allowed pattern is: {[_A-Za-z][_0-9A-Za-z]*}.
  Any invalid characters will be automatically removed.

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

- `serverlessCluster`: [IServerlessCluster](https://docs.aws.amazon.com/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](https://docs.aws.amazon.com/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

- `grantPrincipal`: [IPrincipal](https://docs.aws.amazon.com/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](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

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

## Methods

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