Skip to content
v2.277.0

Code Class ​

Represents source code for an AppSync Function or Resolver.

Example ​

ts
declare const api: appsync.EventApi;
declare const lambdaDataSource: appsync.AppSyncLambdaDataSource;

// Lambda data source for publish handler
api.addChannelNamespace('lambda-ns', {
  code: appsync.Code.fromInline('/* event handler code here.*/'),
  publishHandlerConfig: {
    dataSource: lambdaDataSource,
  },
});

// Direct Lambda data source for publish handler
api.addChannelNamespace('lambda-direct-ns', {
  publishHandlerConfig: {
    dataSource: lambdaDataSource,
    direct: true,
  },
});

api.addChannelNamespace('lambda-direct-async-ns', {
  publishHandlerConfig: {
    dataSource: lambdaDataSource,
    direct: true,
    lambdaInvokeType: appsync.LambdaInvokeType.EVENT,
  },
});
python
# api: appsync.EventApi
# lambda_data_source: appsync.AppSyncLambdaDataSource


# Lambda data source for publish handler
api.add_channel_namespace("lambda-ns",
    code=appsync.Code.from_inline("/* event handler code here.*/"),
    publish_handler_config=appsync.HandlerConfig(
        data_source=lambda_data_source
    )
)

# Direct Lambda data source for publish handler
api.add_channel_namespace("lambda-direct-ns",
    publish_handler_config=appsync.HandlerConfig(
        data_source=lambda_data_source,
        direct=True
    )
)

api.add_channel_namespace("lambda-direct-async-ns",
    publish_handler_config=appsync.HandlerConfig(
        data_source=lambda_data_source,
        direct=True,
        lambda_invoke_type=appsync.LambdaInvokeType.EVENT
    )
)
java
EventApi api;
AppSyncLambdaDataSource lambdaDataSource;


// Lambda data source for publish handler
api.addChannelNamespace("lambda-ns", ChannelNamespaceOptions.builder()
        .code(Code.fromInline("/* event handler code here.*/"))
        .publishHandlerConfig(HandlerConfig.builder()
                .dataSource(lambdaDataSource)
                .build())
        .build());

// Direct Lambda data source for publish handler
api.addChannelNamespace("lambda-direct-ns", ChannelNamespaceOptions.builder()
        .publishHandlerConfig(HandlerConfig.builder()
                .dataSource(lambdaDataSource)
                .direct(true)
                .build())
        .build());

api.addChannelNamespace("lambda-direct-async-ns", ChannelNamespaceOptions.builder()
        .publishHandlerConfig(HandlerConfig.builder()
                .dataSource(lambdaDataSource)
                .direct(true)
                .lambdaInvokeType(LambdaInvokeType.EVENT)
                .build())
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
EventApi api;
AppSyncLambdaDataSource lambdaDataSource;


// Lambda data source for publish handler
api.AddChannelNamespace("lambda-ns", new ChannelNamespaceOptions {
    Code = Code.FromInline("/* event handler code here.*/"),
    PublishHandlerConfig = new HandlerConfig {
        DataSource = lambdaDataSource
    }
});

// Direct Lambda data source for publish handler
api.AddChannelNamespace("lambda-direct-ns", new ChannelNamespaceOptions {
    PublishHandlerConfig = new HandlerConfig {
        DataSource = lambdaDataSource,
        Direct = true
    }
});

api.AddChannelNamespace("lambda-direct-async-ns", new ChannelNamespaceOptions {
    PublishHandlerConfig = new HandlerConfig {
        DataSource = lambdaDataSource,
        Direct = true,
        LambdaInvokeType = LambdaInvokeType.EVENT
    }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var api EventApi
var lambdaDataSource AppSyncLambdaDataSource


// Lambda data source for publish handler
api.AddChannelNamespace(jsii.String("lambda-ns"), &ChannelNamespaceOptions{
	Code: appsync.Code_FromInline(jsii.String("/* event handler code here.*/")),
	PublishHandlerConfig: &HandlerConfig{
		DataSource: lambdaDataSource,
	},
})

// Direct Lambda data source for publish handler
api.AddChannelNamespace(jsii.String("lambda-direct-ns"), &ChannelNamespaceOptions{
	PublishHandlerConfig: &HandlerConfig{
		DataSource: lambdaDataSource,
		Direct: jsii.Boolean(true),
	},
})

api.AddChannelNamespace(jsii.String("lambda-direct-async-ns"), &ChannelNamespaceOptions{
	PublishHandlerConfig: &HandlerConfig{
		DataSource: lambdaDataSource,
		Direct: jsii.Boolean(true),
		LambdaInvokeType: appsync.LambdaInvokeType_EVENT,
	},
})

Initializer ​

ts
new Code()
python
Code()
java
new Code()
csharp
new Code()
go
NewCode()

Methods ​

bind(scope: Construct): CodeConfigdef bind(self, scope: Construct) -> CodeConfigpublic CodeConfig bind(Construct scope)public CodeConfig Bind(Construct scope)func Bind(Scope Construct) CodeConfig #
Bind source code to an AppSync Function or resolver.
fromAsset(path: string, options?: AssetOptions): AssetCode@staticmethod def from_asset(path: str, options: typing.Optional[AssetOptions] = None) -> AssetCodepublic static AssetCode fromAsset(String path, AssetOptions options)public static AssetCode FromAsset(string path, AssetOptions? options = null)func FromAsset(Path *string, Options AssetOptions) AssetCode static #
Loads the function code from a local disk path.
1 parameter

Parameters

  • pathpathpathpathPath — The path to the source code file.
fromInline(code: string): InlineCode@staticmethod def from_inline(code: str) -> InlineCodepublic static InlineCode fromInline(String code)public static InlineCode FromInline(string code)func FromInline(Code *string) InlineCode static #
Inline code for AppSync function.
1 parameter · returns

Parameters

  • codecodecodecodeCode — The actual handler code (the resulting zip file cannot exceed 4MB).

Returns: InlineCode with inline code.