

# Using dependencies with CloudWatch Synthetics canaries
<a name="CloudWatch_Synthetics_Canaries_dependencies"></a>

This section explains how to use `Dependencies` in CloudWatch Synthetics canaries. The `Dependencies` field allows you to specify dependencies for your canaries, enabling you to include additional libraries or custom code that your canary scripts can use.

## Overview
<a name="overview"></a>

CloudWatch Synthetics canaries support specifying Lambda layers as dependencies. This feature allows you to:
+ Share common code across multiple canaries
+ Manage dependencies separately from your canary script code
+ Reduce the size of your canary script by moving dependencies to a Lambda layer

## Supported APIs
<a name="supported-apis"></a>

The `Dependencies` field is supported in the following APIs:
+  [CreateCanary](https://docs.aws.amazon.com/AmazonSynthetics/latest/APIReference/API_CreateCanary.html) 
+  [UpdateCanary](https://docs.aws.amazon.com/AmazonSynthetics/latest/APIReference/API_UpdateCanary.html) 
+  [ StartCanaryDryRun](https://docs.aws.amazon.com/AmazonSynthetics/latest/APIReference/API_StartCanaryDryRun.html) 

## Syntax
<a name="syntax"></a>

The `Dependencies` field is part of the code structure in the request syntax:

```
"Code": { 
  "Handler": "string",
  "S3Bucket": "string",
  "S3Key": "string",
  "S3Version": "string",
  "ZipFile": blob,
  "Dependencies": [
    {
      "Type": "LambdaLayer",
      "Reference": "string"
    }
  ]
}
```

## Using dependencies
<a name="usage"></a>

Here are some examples and instructions for using the `Dependencies` field in different scenarios.

### Creating a Canary with dependencies
<a name="creating-canary"></a>

When creating a canary, you can specify a Lambda layer as a dependency:

```
{
  "Name": "my-canary",
  "Code": {
    "Handler": "pageLoadBlueprint.handler",
    "S3Bucket": "my-bucket",
    "S3Key": "my-canary-script.zip",
    "Dependencies": [
      {
        "Type": "LambdaLayer",
        "Reference": "arn:aws:lambda:us-west-2:123456789012:layer:my-custom-layer:1"
      }
    ]
  },
  "ArtifactS3Location": "s3://my-bucket/artifacts/",
  "ExecutionRoleArn": "arn:aws:iam::123456789012:role/my-canary-role",
  "Schedule": {
    "Expression": "rate(5 minutes)"
  },
  "RuntimeVersion": "syn-nodejs-puppeteer-3.9"
}
```

### Updating a Canary's dependencies
<a name="updating-canary"></a>

You can update a canary's dependencies using the UpdateCanary API:

```
{
  "Name": "my-canary",
  "Code": {
    "Dependencies": [
      {
        "Type": "LambdaLayer",
        "Reference": "arn:aws:lambda:us-west-2:123456789012:layer:my-updated-layer:2"
      }
    ]
  }
}
```

### Removing dependencies
<a name="removing-dependencies"></a>

To remove dependencies from a canary, provide an empty array for the Dependencies field:

```
{
  "Name": "my-canary",
  "Code": {
    "Dependencies": []
  }
}
```

### Testing dependencies with StartCanaryDryRun
<a name="testing-dependencies"></a>

Before updating a canary with new dependencies, you can test them using the StartCanaryDryRun API:

```
{
  "Name": "my-canary",
  "Code": {
    "Dependencies": [
      {
        "Type": "LambdaLayer",
        "Reference": "arn:aws:lambda:us-west-2:123456789012:layer:my-test-layer:3"
      }
    ]
  }
}
```

## Limitations and considerations
<a name="limitations"></a>
+ Only one Lambda layer can be specified as a dependency
+ The role being used to create a canary with dependencies should have ` lambda:GetLayerVersion` access to the dependency layer in addition to the [necessary roles and permissions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_Roles.html)

## Creating compatible Lambda layers
<a name="creating-compatible-layers"></a>

For information on how to create and package layers, see [Managing Lambda dependencies with layers ](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html) and to understand the packaging structure of a canary check based on the canary packing structure, see [Writing a canary script](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Canaries_WritingCanary.html).