> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/use-continuous-delivery/provision-infrastructure/aws-cdk/aws-cdk-use-cases.md).

# AWS CDK use cases and examples

This topic explains AWS Cloud Development Kit (AWS CDK) provisioning modes in Harness and provides code examples for common deployment scenarios. AWS CDK allows you to provision infrastructure using familiar programming languages, either independently or as part of a deployment workflow.

***

### What will you learn in this topic? <a href="#what-will-you-learn-in-this-topic" id="what-will-you-learn-in-this-topic"></a>

* How to choose between [ad hoc provisioning](#ad-hoc-provisioning) and [dynamic provisioning](#dynamic-provisioning) for AWS CDK.
* How [dynamic provisioning maps CDK stack outputs](#dynamic-provisioning-by-deployment-type) for each supported deployment type.
* How to structure a CDK application and [reference its outputs in Harness](#example-ecs-infrastructure-provisioning) through an ECS example.

***

### Provisioning modes <a href="#provisioning-modes" id="provisioning-modes"></a>

Harness supports two AWS CDK provisioning modes:

* [Ad hoc provisioning](#ad-hoc-provisioning): Provision infrastructure as a standalone task without deploying an application in the same flow.
* [Dynamic provisioning](#dynamic-provisioning): Provision the target infrastructure and deploy your application to it in the same stage.

The pipeline steps are configured the same way for both modes. Choose the mode that matches your goal: use ad hoc provisioning to manage infrastructure on its own, and dynamic provisioning to provision and deploy in one stage. Multi-account deployments are supported, allowing you to deploy to different AWS accounts using a single connector by overriding the region and assuming a different IAM role. Go to [AWS CDK Provisioning](/continuous-delivery/use-continuous-delivery/provision-infrastructure/aws-cdk/aws-cdk-provisioning.md#aws-connector-configuration-optional) to configure multi-account deployments.

{% hint style="info" %}
**SERVICE INSTANCE LICENSING**

Harness does not consume Service Instances (SIs) when you use AWS CDK for infrastructure provisioning alone, so you can provision infrastructure at no additional licensing cost. SI licensing applies only when Harness deploys an application to the provisioned infrastructure in the same stage or pipeline.
{% endhint %}

Go to [Provisioning overview](/continuous-delivery/use-continuous-delivery/provision-infrastructure/provisioning-overview.md) to understand Harness provisioning concepts and use cases.

***

### Ad hoc provisioning <a href="#ad-hoc-provisioning" id="ad-hoc-provisioning"></a>

Ad hoc provisioning lets you provision infrastructure as a standalone workflow without deploying an application in the same flow. This mode is useful to create test environments, set up shared resources, or make infrastructure changes independently of application deployments.

For ad hoc provisioning, add the AWS CDK steps to the **Execution** section of a CD Deploy stage. The steps provision your resources when the stage runs.

```mermaid
flowchart TD
    subgraph stage["Execution section"]
        direction TB
        A["AWS CDK
application"] --> B["CDK steps
Bootstrap, Synth,
Diff, Deploy"]
        B --> C["Standalone
infrastructure
provisioned"]
    end
```

Example use cases:

* Provision a shared VPC or IAM resources that other pipelines consume.
* Stand up a temporary test environment for validation, then destroy it in a later step.
* Run a one-time infrastructure change defined in a CDK stack.

Go to [AWS CDK Provisioning](/continuous-delivery/use-continuous-delivery/provision-infrastructure/aws-cdk/aws-cdk-provisioning.md) to configure the CDK steps in your pipeline.

***

### Dynamic provisioning <a href="#dynamic-provisioning" id="dynamic-provisioning"></a>

Dynamic provisioning creates the target infrastructure as part of the application deployment. Harness provisions the infrastructure first and then deploys the application to the newly created resources. Typically, dynamic provisioning is for temporary pre-production environments such as development, test, and QA. Production environments are usually pre-existing.

For dynamic provisioning, add the AWS CDK steps to the **Environment** section of a CD Deploy stage and map the CDK stack outputs to the Infrastructure Definition. Harness then deploys your application to the provisioned infrastructure in the same stage.

```mermaid
flowchart TD
    subgraph stage["Environment section"]
        direction TB
        A["AWS CDK
application"] --> B["CDK steps
Bootstrap, Synth,
Diff, Deploy"]
        B --> C["Map CDK stack outputs
to the Infrastructure
Definition"]
        C --> D["Deploy application
to the provisioned
infrastructure"]
    end
```

Example use cases:

* Provision an ECS cluster and deploy a containerized application to it in a single pipeline.
* Create an ephemeral Kubernetes namespace per pull request, deploy to it, and tear it down afterward.
* Provision AWS Lambda infrastructure and deploy the function in the same stage.

#### Dynamic provisioning by deployment type <a href="#dynamic-provisioning-by-deployment-type" id="dynamic-provisioning-by-deployment-type"></a>

Each Harness deployment type, such as Kubernetes or AWS ECS, requires different CDK stack outputs to be mapped to its infrastructure settings. The following deployment types support dynamic provisioning with AWS CDK. Go to the topic for your deployment type to understand which CDK stack outputs are required.

* [Kubernetes infrastructure](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/kubernetes/define-your-kubernetes-target-infrastructure.md): Also used for Helm, Native Helm, and Kustomize deployment types.
* [AWS ECS](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/aws/ecs/ecs-deployment-tutorial.md): Elastic Container Service deployments.
* [AWS Lambda](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/aws/aws-lambda-deployments.md): Serverless function deployments.
* [Spot Elastigroup](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/aws/spot/spot-deployment.md): Spot instance group deployments.
* [Serverless.com framework for AWS Lambda](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/serverless/serverless-lambda-cd-quickstart.md): Serverless Framework deployments on AWS Lambda.
* [Tanzu Application Services](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/tanzu/tanzu-app-services-quickstart.md): Tanzu (Pivotal Cloud Foundry) deployments.
* [VM deployments using SSH](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/traditional/ssh-ng.md): Traditional virtual machine deployments over SSH.
* [Windows VM deployments using WinRM](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/traditional/win-rm-tutorial.md): Windows virtual machine deployments over WinRM.

To configure dynamic provisioning, go to the stage **Environment** tab in your Harness pipeline and select **AWS CDK** as the provisioner.

#### Example: ECS infrastructure provisioning <a href="#example-ecs-infrastructure-provisioning" id="example-ecs-infrastructure-provisioning"></a>

The following example shows an AWS CDK TypeScript application that provisions infrastructure for an ECS deployment and exposes the stack outputs required by the Harness Infrastructure Definition. Expand the section to review the full application.

<details>

<summary>ECS CDK application (TypeScript)</summary>

```typescript
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';

class EcsCdkStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Define a VPC (Virtual Private Cloud)
    const vpc = new ec2.Vpc(this, 'MyVpc', {
      maxAzs: 2, // Specify the number of availability zones
    });

    // Create an ECS cluster
    const cluster = new ecs.Cluster(this, 'MyCluster', {
      vpc,
    });

    // Define an ECS Fargate service using a sample container image
    new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'MyFargateService', {
      cluster,
      memoryLimitMiB: 512,
      cpu: 256,
      taskImageOptions: {
        image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
      },
    });

    // Define an output for the AWS region (CfnOutput exports stack values)
    new cdk.CfnOutput(this, 'RegionOutput', {
      value: cdk.Aws.REGION,
      description: 'AWS region of the stack',
    });

    // Define an output for the ECS cluster name
    new cdk.CfnOutput(this, 'ClusterNameOutput', {
      value: cluster.clusterName,
      description: 'Name of the ECS cluster',
    });
  }
}

const app = new cdk.App();
new EcsCdkStack(app, 'EcsCdkStack');
```

</details>

#### Reference CDK outputs in Harness <a href="#reference-cdk-outputs-in-harness" id="reference-cdk-outputs-in-harness"></a>

In the Harness Infrastructure Definition, you map CDK stack outputs to their corresponding settings using expressions in the format `<+provisioner.STACK_NAME.OUTPUT_NAME>`. For example:

* `<+provisioner.EcsCdkStack.RegionOutput>` references the AWS region output.
* `<+provisioner.EcsCdkStack.ClusterNameOutput>` references the ECS cluster name output.

The `CfnOutput` construct in your CDK application exports stack values that Harness can reference. Each output you define becomes available as an expression variable after the CDK Deploy step completes successfully.

{% hint style="info" %}
**TWO WAYS TO REFERENCE STACK OUTPUTS**

Use the short `<+provisioner.STACK_NAME.OUTPUT_NAME>` form to map an output to an Infrastructure Definition setting during dynamic provisioning. To reference a Deploy step output elsewhere in the pipeline, use the full step path instead, as described in [Reference CDK Deploy output variables](/continuous-delivery/use-continuous-delivery/provision-infrastructure/aws-cdk/aws-cdk-provisioning.md#reference-cdk-deploy-output-variables).
{% endhint %}

<figure><img src="/files/RuP0yFlwjfmBp185IU5y" alt="Cluster Details settings with Map Dynamically Provisioned Infrastructure enabled and the Region and Cluster fields mapped to the EcsCdkStack.RegionOutput and EcsCdkStack.ClusterNameOutput expressions"><figcaption></figcaption></figure>

Go to [Use Harness expressions](/harness-ai/use-harness-platform/variables-and-expressions/harness-variables.md) to understand Harness expression syntax.

***

### Next steps <a href="#next-steps" id="next-steps"></a>

* [AWS CDK Provisioning](/continuous-delivery/use-continuous-delivery/provision-infrastructure/aws-cdk/aws-cdk-provisioning.md): Configure the CDK steps in your pipeline.
* [Build your own CDK image](/continuous-delivery/use-continuous-delivery/provision-infrastructure/aws-cdk/cdk-image-build.md): Customize CDK plugin images with specific versions and dependencies.
* [AWS ECS deployment tutorial](/continuous-delivery/use-continuous-delivery/deploy-services-on-different-platforms/aws/ecs/ecs-deployment-tutorial.md): Provision ECS infrastructure with CDK and deploy containerized applications.
