

# Connecting AWS Lambda functions to AWS Systems Manager Cloud Extensions

Connect your AWS Lambda functions to a Cloud Extensions profile, and verify, update, and remove the connection.

You connect a Lambda function to Cloud Extensions in two parts. First, you add the Cloud Extensions runtime to the function as a layer. Second, you point the function to a profile. The profile stores the extensions that the function loads, and the settings of the extensions. You can point a function to a profile with the Lambda console or the AWS CLI.

**Topics**
+ [How Lambda integration works](#cloud-extensions-lambda-how-lambda-integration-works)
+ [Connecting a function on the console](#cloud-extensions-lambda-connecting-a-function-on-the-console)
+ [Connecting a function with the AWS CLI](#cloud-extensions-lambda-connecting-a-function-with-the-aws-cli)
+ [Verifying the connection](#cloud-extensions-lambda-verifying-the-connection)
+ [Updating and removing the connection](#cloud-extensions-lambda-updating-and-removing-the-connection)
+ [Considerations](#cloud-extensions-lambda-considerations)

## How Lambda integration works
<a name="cloud-extensions-lambda-how-lambda-integration-works"></a>

You point a function to a profile by setting the `AWS_CLOUDEXTENSIONS_PROFILE` environment variable of the function to the name or ID of the profile. The runtime loads that profile.

When the function runs, the Cloud Extensions runtime starts in the execution environment. The runtime retrieves the profile, compares the SHA-256 hash of each extension with the hash in the profile state, and loads the extensions. Each execution environment runs its own runtime with its own cache.

Because the environment variable holds the profile name or ID, you can change which extensions a function uses by pointing it to a different profile. You don't change your function code.

## Connecting a function on the console
<a name="cloud-extensions-lambda-connecting-a-function-on-the-console"></a>

**To connect a function to a profile on the Lambda console**

1. Open the Lambda console, and then choose your function.

1. Choose the **Configuration** tab, and then choose **Environment variables**.

1. Choose **Edit**, and then choose **Add environment variable**.

1. For **Key**, enter `AWS_CLOUDEXTENSIONS_PROFILE`. For **Value**, enter the name or ID of your profile.

1. Choose **Save**.

The role of the function needs the `cloudextensions:SyncState` action. For the policy, see [Permissions for the runtime](cloud-extensions-security.md#cloud-extensions-security-permissions-for-the-runtime).

## Connecting a function with the AWS CLI
<a name="cloud-extensions-lambda-connecting-a-function-with-the-aws-cli"></a>

To connect a function to a profile with the AWS CLI, run the `update-function-configuration` command. Replace `{{function-name}}` with the name of your function, and replace `{{profile-name}}` with the name of your profile:

```bash
aws lambda update-function-configuration \
  --function-name {{function-name}} \
  --environment "Variables={AWS_CLOUDEXTENSIONS_PROFILE={{profile-name}}}"
```

The `--environment` option of the `update-function-configuration` command replaces the whole map of environment variables. To keep your existing variables, include them in the command, together with `AWS_CLOUDEXTENSIONS_PROFILE`.

## Verifying the connection
<a name="cloud-extensions-lambda-verifying-the-connection"></a>

To verify the connection, invoke the function and check that the extensions are active. The runtime writes its startup and extension messages to the logs of the function. For the messages, see [Runtime logs](cloud-extensions-monitoring.md#cloud-extensions-monitoring-runtime-logs).

If the extensions don't load as expected, see [Troubleshooting AWS Systems Manager Cloud Extensions](cloud-extensions-troubleshooting.md).

## Updating and removing the connection
<a name="cloud-extensions-lambda-updating-and-removing-the-connection"></a>

To point a function to a different profile, change the value of the `AWS_CLOUDEXTENSIONS_PROFILE` environment variable to the name or ID of the new profile. To remove Cloud Extensions from a function, remove the layer and delete the environment variable. For more information, see [Stopping the use of Cloud Extensions](cloud-extensions-profiles.md#cloud-extensions-profiles-stopping-the-use-of-cloud-extensions).

When you update a profile, the runtime applies the change in the first invocation after the 45 seconds pass. A new execution environment loads the current profile when it starts. You don't need to change the function. For more information, see [Working with AWS Systems Manager Cloud Extensions profiles](cloud-extensions-profiles.md).

## Considerations
<a name="cloud-extensions-lambda-considerations"></a>

Review the following considerations when you connect functions to Cloud Extensions:
+ Functions that already use other Lambda layers or extensions can also use Cloud Extensions.
+ When you combine many extensions in a profile, consider the memory and the startup time of the execution environment.
+ The time that an execution environment spends frozen counts toward the cache time to live of an extension.