Skip to content
v2.277.0

Code Class ​

The code the canary should execute.

Example ​

ts
import * as cdk from 'aws-cdk-lib';

const canary = new synthetics.Canary(this, 'MyCanary', {
  schedule: synthetics.Schedule.rate(Duration.minutes(5)),
  test: synthetics.Test.custom({
    code: synthetics.Code.fromAsset(path.join(__dirname, 'canary')),
    handler: 'index.handler',
  }),
  runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_6_2,
  memory: cdk.Size.mebibytes(1024), // 1024 MiB
});
python
import aws_cdk as cdk


canary = synthetics.Canary(self, "MyCanary",
    schedule=synthetics.Schedule.rate(Duration.minutes(5)),
    test=synthetics.Test.custom(
        code=synthetics.Code.from_asset(path.join(__dirname, "canary")),
        handler="index.handler"
    ),
    runtime=synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_6_2,
    memory=cdk.Size.mebibytes(1024)
)
java
import software.amazon.awscdk.*;


Canary canary = Canary.Builder.create(this, "MyCanary")
        .schedule(Schedule.rate(Duration.minutes(5)))
        .test(Test.custom(CustomTestOptions.builder()
                .code(Code.fromAsset(join(__dirname, "canary")))
                .handler("index.handler")
                .build()))
        .runtime(Runtime.SYNTHETICS_NODEJS_PUPPETEER_6_2)
        .memory(Size.mebibytes(1024))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK;


var canary = new Canary(this, "MyCanary", new CanaryProps {
    Schedule = Schedule.Rate(Duration.Minutes(5)),
    Test = Test.Custom(new CustomTestOptions {
        Code = Code.FromAsset(Join(__dirname, "canary")),
        Handler = "index.handler"
    }),
    Runtime = Runtime.SYNTHETICS_NODEJS_PUPPETEER_6_2,
    Memory = Size.Mebibytes(1024)
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import cdk "github.com/aws/aws-cdk-go/awscdk"


canary := synthetics.NewCanary(this, jsii.String("MyCanary"), &CanaryProps{
	Schedule: synthetics.Schedule_Rate(awscdk.Duration_Minutes(jsii.Number(5))),
	Test: synthetics.Test_Custom(&CustomTestOptions{
		Code: synthetics.Code_FromAsset(path.join(__dirname, jsii.String("canary"))),
		Handler: jsii.String("index.handler"),
	}),
	Runtime: synthetics.Runtime_SYNTHETICS_NODEJS_PUPPETEER_6_2(),
	Memory: cdk.Size_Mebibytes(jsii.Number(1024)),
})

Initializer ​

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

Methods ​

bind(scope: Construct, handler: string, family: RuntimeFamily, runtimeName?: string): CodeConfigdef bind(self, scope: Construct, handler: str, family: RuntimeFamily, runtime_name: typing.Optional[str] = None) -> CodeConfigpublic CodeConfig bind(Construct scope, String handler, RuntimeFamily family, String runtimeName)public CodeConfig Bind(Construct scope, string handler, RuntimeFamily family, string? runtimeName = null)func Bind(Scope Construct, Handler *string, Family RuntimeFamily, RuntimeName *string) CodeConfig#
Called when the canary is initialized to allow this object to bind to the stack, add resources and have fun.
1 parameter · returns

Parameters

  • scopescopescopescopeScope — The binding scope.

Returns: a bound CodeConfig.

fromAsset(assetPath: string, options?: AssetOptions): AssetCode@staticmethod def from_asset(asset_path: str, options: typing.Optional[AssetOptions] = None) -> AssetCodepublic static AssetCode fromAsset(String assetPath, AssetOptions options)public static AssetCode FromAsset(string assetPath, AssetOptions? options = null)func FromAsset(AssetPath *string, Options AssetOptions) AssetCodestatic#
Specify code from a local path.

Path must include the folder structure nodejs/node_modules/myCanaryFilename.js.

1 parameter · returns

Parameters

  • assetPathasset_pathassetPathassetPathAssetPath — Either a directory or a .zip file.

Returns: AssetCode associated with the specified path.

fromBucket(bucket: IBucketRef, key: string, objectVersion?: string): S3Code@staticmethod def from_bucket(bucket: IBucketRef, key: str, object_version: typing.Optional[str] = None) -> S3Codepublic static S3Code fromBucket(IBucketRef bucket, String key, String objectVersion)public static S3Code FromBucket(IBucketRef bucket, string key, string? objectVersion = null)func FromBucket(Bucket IBucketRef, Key *string, ObjectVersion *string) S3Codestatic#
Specify code from an s3 bucket.

The object in the s3 bucket must be a .zip file that contains the structure nodejs/node_modules/myCanaryFilename.js.

3 parameters · returns

Parameters

  • bucketbucketbucketbucketBucket — The S3 bucket.
  • keykeykeykeyKey — The object key.
  • objectVersionobject_versionobjectVersionobjectVersionObjectVersion — Optional S3 object version.

Returns: S3Code associated with the specified S3 object.

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) InlineCodestatic#
Specify code inline.
1 parameter · returns

Parameters

  • codecodecodecodeCode — The actual handler code (limited to 5MB).

Returns: InlineCode with inline code.