# Code

The code the canary should execute.

Extends: Code
Superclass of: AssetCode, InlineCode, S3Code

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

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

## Initializer

```ts
new Code()
```

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