# LambdaDataSource

An AppSync datasource backed by a Lambda function.

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › [BaseDataSource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_appsync.BaseDataSource.html) › [BackedDataSource](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_appsync.BackedDataSource.html) › LambdaDataSource
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.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-appsync/lib/data-source.ts#L394)
- Type last changed: v2.196.0 · 16 May 2025

## Example

TypeScript:

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import { aws_appsync as appsync } from 'aws-cdk-lib';
import { aws_iam as iam } from 'aws-cdk-lib';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { aws_appsync as interfaces_appsync } from 'aws-cdk-lib/interfaces';

declare const function_: lambda.Function;
declare const graphQLApiRef: interfaces_appsync.IGraphQLApiRef;
declare const role: iam.Role;
const lambdaDataSource = new appsync.LambdaDataSource(this, 'MyLambdaDataSource', {
  api: graphQLApiRef,
  lambdaFunction: function_,

  // the properties below are optional
  description: 'description',
  metricsConfig: appsync.DataSourceMetricsConfig.ENABLED,
  name: 'name',
  serviceRole: role,
});
```

Python:

```python
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
from aws_cdk import aws_appsync as appsync
from aws_cdk import aws_iam as iam
from aws_cdk import aws_lambda as lambda_
from aws_cdk.interfaces import aws_appsync as interfaces_appsync

# function_: lambda.Function
# graph_ql_api_ref: interfaces_appsync.IGraphQLApiRef
# role: iam.Role

lambda_data_source = appsync.LambdaDataSource(self, "MyLambdaDataSource",
    api=graph_ql_api_ref,
    lambda_function=function_,

    # the properties below are optional
    description="description",
    metrics_config=appsync.DataSourceMetricsConfig.ENABLED,
    name="name",
    service_role=role
)
```

Java:

```java
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.services.appsync.*;
import software.amazon.awscdk.services.iam.*;
import software.amazon.awscdk.services.lambda.*;
import software.amazon.awscdk.interfaces.appsync.*;

Function function_;
IGraphQLApiRef graphQLApiRef;
Role role;

LambdaDataSource lambdaDataSource = LambdaDataSource.Builder.create(this, "MyLambdaDataSource")
        .api(graphQLApiRef)
        .lambdaFunction(function_)

        // the properties below are optional
        .description("description")
        .metricsConfig(DataSourceMetricsConfig.ENABLED)
        .name("name")
        .serviceRole(role)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK.AWS.AppSync;
using Amazon.CDK.AWS.IAM;
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK.Interfaces.AppSync;

Function function_;
IGraphQLApiRef graphQLApiRef;
Role role;

var lambdaDataSource = new LambdaDataSource(this, "MyLambdaDataSource", new LambdaDataSourceProps {
    Api = graphQLApiRef,
    LambdaFunction = function_,

    // the properties below are optional
    Description = "description",
    MetricsConfig = DataSourceMetricsConfig.ENABLED,
    Name = "name",
    ServiceRole = role
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var function_ Function
var graphQLApiRef IGraphQLApiRef
var role Role

lambdaDataSource := awscdk.Aws_appsync.NewLambdaDataSource(this, jsii.String("MyLambdaDataSource"), &LambdaDataSourceProps{
	Api: graphQLApiRef,
	LambdaFunction: function_,

	// the properties below are optional
	Description: jsii.String("description"),
	MetricsConfig: awscdk.*Aws_appsync.DataSourceMetricsConfig_ENABLED,
	Name: jsii.String("name"),
	ServiceRole: role,
})
```

## Initializer

TypeScript:

```ts
new LambdaDataSource(scope: Construct, id: string, props: LambdaDataSourceProps)
```

Python:

```python
LambdaDataSource(scope: Construct, id: str, props: LambdaDataSourceProps)
```

Java:

```java
new LambdaDataSource(Construct scope, String id, LambdaDataSourceProps props)
```

C# / .NET:

```csharp
new LambdaDataSource(Construct scope, string id, LambdaDataSourceProps props)
```

Golang:

```go
NewLambdaDataSource(Scope Construct, Id *string, Props LambdaDataSourceProps)
```

## Construct Props

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

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

- `lambdaFunction`: [IFunction](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.IFunction.html) (required) — The Lambda function to call to interact with this data source.

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

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

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

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

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