> ## Documentation Index
> Fetch the complete documentation index at: https://cdktn.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Lambda functions with TypeScript and CDK Terrain

> Deploy two AWS Lambda stacks from a single CDKTN app, package Lambda artifacts with TerraformAsset, and manage each stack independently.

<Note>
  This guide was recovered from the [Wayback Machine snapshot of “Deploy Lambda functions with TypeScript and CDK for Terraform”](https://web.archive.org/web/20251112223320https://developer.hashicorp.com/terraform/tutorials/cdktf/cdktf-assets-stacks-lambda).
</Note>

CDK Terrain (CDKTN) stacks let you manage multiple Terraform configurations within a single application. Each stack synthesizes to its own Terraform configuration and state file, which lets you deploy and destroy stacks independently.

In this tutorial, you deploy a CDKTN application (TypeScript-only) with two stacks. Each stack provisions a small AWS Lambda function and uses `TerraformAsset` to package the Lambda deployment artifact.

## Prerequisites

This tutorial assumes you are already familiar with the basic CDKTN workflow. If you are new to CDKTN, start with the [install tutorial](/tutorials/install).

* Terraform v1.2+ (or [OpenTofu](https://opentofu.org/))
* CDKTN v0.15+
* AWS account
* AWS credentials configured for Terraform
* Node.js + npm

<Warning>
  Some of the infrastructure in this tutorial does not qualify for the AWS free tier. Destroy the infrastructure at the end of the guide to avoid unnecessary charges.
</Warning>

<Tip>CDKTN works with both Terraform and OpenTofu. To run OpenTofu, set `TERRAFORM_BINARY_NAME=tofu` before using the CLI. Refer to [Environment Variables](/create-and-deploy/environment-variables) for details.</Tip>

## Explore CDKTN application

Clone the sample repository and move into the CDKTN project directory.

```shell Shell theme={null}
git clone https://github.com/hashicorp-education/learn-cdktf-assets-stacks-lambda
cd learn-cdktf-assets-stacks-lambda
cd cdktf
```

## View application stacks

Install dependencies.

```shell-session Shell theme={null}
npm install
added 305 packages, and audited 357 packages in 5s
found 0 vulnerabilities
```

Add the AWS and Random providers.

```shell-session Shell theme={null}
cdktn provider add "aws@~>4.0" random
Found pre-built provider.
Installing package @cdktn/provider-aws ...
Package installed.
Found pre-built provider.
Installing package @cdktn/provider-random ...
Package installed.
```

List the stacks in the application.

```shell-session Shell theme={null}
cdktn list

Stack name           Path
lambda-hello-world   cdktf.out/stacks/lambda-hello-world
lambda-hello-name    cdktf.out/stacks/lambda-hello-name
```

## Deploy Hello World function

Deploy the `lambda-hello-world` stack.

```shell-session Shell theme={null}
cdktn deploy lambda-hello-world
...
Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Outputs:
lambda-hello-world url = "https://bur6dfzia5.execute-api.us-west-2.amazonaws.com"
```

Open the `url` output in your browser to confirm the API gateway returns a greeting.

## Deploy Hello Name function

Deploy the `lambda-hello-name` stack.

```shell-session Shell theme={null}
cdktn deploy lambda-hello-name
...
Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Outputs:
lambda-hello-name url = "https://oogzjki3cb.execute-api.us-west-2.amazonaws.com"
```

Open the `url` output in your browser.

Append `?name=Terry` to the URL to confirm the API gateway responds with a personalized greeting.

## Inspect synthesized stacks

CDKTN generates a `cdktf.out` directory when it synthesizes the Terraform configuration (during `cdktn synth` or `cdktn deploy`). Each stack has its own folder under `cdktf.out/stacks`.

```text cdktf.out/ theme={null}
cdktf.out/
|-- manifest.json
`-- stacks
    |-- lambda-hello-name
    |   |-- assets
    |   |   `-- lambda-asset
    |   |       `-- ACDAAB9A40AD1E0EC3B40C4B53527E85
    |   |           `-- archive.zip
    |   |-- cdk.tf.json
    |   `-- plan
    `-- lambda-hello-world
        |-- assets
        |   `-- lambda-asset
        |       `-- BD42BA06A24B006B4907A4F399734ACC
        |           `-- archive.zip
        |-- cdk.tf.json
        `-- plan
```

Each stack also has its own state file (for example, `terraform.lambda-hello-world.tfstate`). Like any Terraform state file, do not commit state files into source control.

## Clean up resources

Destroy both stacks to remove AWS resources.

```shell-session Shell theme={null}
cdktn destroy lambda-hello-world
...
Destroy complete! Resources: 8 destroyed.
```

```shell-session Shell theme={null}
cdktn destroy lambda-hello-name
...
Destroy complete! Resources: 8 destroyed.
```

## Next steps

* Continue with [Deploy applications](/tutorials/deploy-applications) for an end-to-end, multi-step deployment workflow.
* Review how stacks work in CDKTN and Terraform state in [Stacks](/concepts/stacks).
