RdsDataSource

class aws_cdk.aws_appsync.RdsDataSource(scope, id, *, secret_store, serverless_cluster, database_name=None, service_role=None, api, description=None, name=None)

Bases: BackedDataSource

An AppSync datasource backed by RDS.

ExampleMetadata:

infused

Example:

# Build a data source for AppSync to access the database.
# api: appsync.GraphqlApi
# Create username and password secret for DB Cluster
secret = rds.DatabaseSecret(self, "AuroraSecret",
    username="clusteradmin"
)

# The VPC to place the cluster in
vpc = ec2.Vpc(self, "AuroraVpc")

# Create the serverless cluster, provide all values needed to customise the database.
cluster = rds.ServerlessCluster(self, "AuroraCluster",
    engine=rds.DatabaseClusterEngine.AURORA_MYSQL,
    vpc=vpc,
    credentials={"username": "clusteradmin"},
    cluster_identifier="db-endpoint-test",
    default_database_name="demos"
)
rds_dS = api.add_rds_data_source("rds", cluster, secret, "demos")

# Set up a resolver for an RDS query.
rds_dS.create_resolver("QueryGetDemosRdsResolver",
    type_name="Query",
    field_name="getDemosRds",
    request_mapping_template=appsync.MappingTemplate.from_string("""
          {
            "version": "2018-05-29",
            "statements": [
              "SELECT * FROM demos"
            ]
          }
          """),
    response_mapping_template=appsync.MappingTemplate.from_string("""
            $utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
          """)
)

# Set up a resolver for an RDS mutation.
rds_dS.create_resolver("MutationAddDemoRdsResolver",
    type_name="Mutation",
    field_name="addDemoRds",
    request_mapping_template=appsync.MappingTemplate.from_string("""
          {
            "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)
            }
          }
          """),
    response_mapping_template=appsync.MappingTemplate.from_string("""
            $utils.toJson($utils.rds.toJsonObject($ctx.result)[1][0])
          """)
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • secret_store (ISecret) – The secret containing the credentials for the database.

  • serverless_cluster (IServerlessCluster) – The serverless cluster to call to interact with this data source.

  • database_name (Optional[str]) – The name of the database to use within the cluster. Default: - None

  • service_role (Optional[IRole]) – The IAM service role to be assumed by AppSync to interact with the data source. Default: - Create a new role

  • api (IGraphqlApi) – The API to attach this data source to.

  • description (Optional[str]) – the description of the data source. Default: - None

  • name (Optional[str]) – The name of the data source. Default: - id of data source

Methods

create_function(id, *, name, code=None, description=None, max_batch_size=None, request_mapping_template=None, response_mapping_template=None, runtime=None)

creates a new appsync function for this datasource and API using the given properties.

Parameters:
  • id (str) –

  • name (str) – the name of the AppSync Function.

  • code (Optional[Code]) – The function code. Default: - no code is used

  • description (Optional[str]) – the description for this AppSync Function. Default: - no description

  • max_batch_size (Union[int, float, None]) – The maximum number of resolver request inputs that will be sent to a single AWS Lambda function in a BatchInvoke operation. Can only be set when using LambdaDataSource. Default: - No max batch size

  • request_mapping_template (Optional[MappingTemplate]) – the request mapping template for the AppSync Function. Default: - no request mapping template

  • response_mapping_template (Optional[MappingTemplate]) – the response mapping template for the AppSync Function. Default: - no response mapping template

  • runtime (Optional[FunctionRuntime]) – The functions runtime. Default: - no function runtime, VTL mapping templates used

Return type:

AppsyncFunction

create_resolver(id, *, field_name, type_name, caching_config=None, code=None, max_batch_size=None, pipeline_config=None, request_mapping_template=None, response_mapping_template=None, runtime=None)

creates a new resolver for this datasource and API using the given properties.

Parameters:
  • id (str) –

  • field_name (str) – name of the GraphQL field in the given type this resolver is attached to.

  • type_name (str) – name of the GraphQL type this resolver is attached to.

  • caching_config (Union[CachingConfig, Dict[str, Any], None]) – The caching configuration for this resolver. Default: - No caching configuration

  • code (Optional[Code]) – The function code. Default: - no code is used

  • max_batch_size (Union[int, float, None]) – The maximum number of elements per batch, when using batch invoke. Default: - No max batch size

  • pipeline_config (Optional[Sequence[IAppsyncFunction]]) – configuration of the pipeline resolver. Default: - no pipeline resolver configuration An empty array | undefined sets resolver to be of kind, unit

  • request_mapping_template (Optional[MappingTemplate]) – The request mapping template for this resolver. Default: - No mapping template

  • response_mapping_template (Optional[MappingTemplate]) – The response mapping template for this resolver. Default: - No mapping template

  • runtime (Optional[FunctionRuntime]) – The functions runtime. Default: - no function runtime, VTL mapping templates used

Return type:

Resolver

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

ds

the underlying CFN data source resource.

grant_principal

the principal of the data source to be IGrantable.

name

the name of the data source.

node

The tree node.

Static Methods

classmethod is_construct(x)

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) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.