# Code

The code the canary should execute.

Superclass of: [AssetCode](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_synthetics.AssetCode.html), [InlineCode](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_synthetics.InlineCode.html), [S3Code](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_synthetics.S3Code.html)

- Source: [`aws-synthetics/lib/code.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-synthetics/lib/code.ts#L14)
- Type last changed: v2.224.0 · 13 Nov 2025

## Example

TypeScript:

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

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

```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();
```

C# / .NET:

```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)
});
```

Golang:

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

TypeScript:

```ts
new Code()
```

Python:

```python
Code()
```

Java:

```java
new Code()
```

C# / .NET:

```csharp
new Code()
```

Golang:

```go
NewCode()
```

## Methods

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

  **Parameters**

  - `scope` — The binding scope.

  **Returns:** a bound `CodeConfig`.

- `fromAsset(assetPath: string, options?: AssetOptions): AssetCode` (static) — Specify code from a local path.

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

  **Parameters**

  - `assetPath` — Either a directory or a .zip file.

  **Returns:** `AssetCode` associated with the specified path.

- `fromBucket(bucket: IBucketRef, key: string, objectVersion?: string): S3Code` (static) — 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`.

  **Parameters**

  - `bucket` — The S3 bucket.
  - `key` — The object key.
  - `objectVersion` — Optional S3 object version.

  **Returns:** `S3Code` associated with the specified S3 object.

- `fromInline(code: string): InlineCode` (static) — Specify code inline.

  **Parameters**

  - `code` — The actual handler code (limited to 5MB).

  **Returns:** `InlineCode` with inline code.
