AWS CDK use cases and examples
This topic explains AWS CDK provisioning patterns 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.
Provisioning patterns
Harness supports two AWS CDK provisioning patterns:
-
Ad hoc provisioning: Temporary and on-demand provisioning of resources for specific tasks or purposes. Use this pattern to provision infrastructure independently, without deploying applications. This is useful for creating test environments, setting up shared resources, or managing infrastructure as a standalone workflow.
-
Dynamic infrastructure provisioning: Provision the target deployment environment as part of the same deployment process. Harness provisions the infrastructure first, then deploys your application to the newly created resources. Typically, dynamic infrastructure provisioning is for temporary pre-production environments such as dev, test, and qa. Production environments are usually pre-existing.
The provisioning pipeline steps are configured the same way for both patterns. 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 to configure multi-account deployments.
Go to Provisioning overview to understand Harness provisioning concepts and use cases.
Flexible infrastructure provisioning
Harness AWS CDK provisioning gives you the flexibility to provision infrastructure independently or as part of a deployment workflow. You can use AWS CDK provisioning on its own to manage infrastructure without deploying artifacts, or combine it with Harness deployment services to provision infrastructure and deploy applications in a single pipeline.
Service Instances (SIs) consumption
Harness optimizes licensing by not consuming Service Instances (SIs) when using AWS CDK for infrastructure provisioning alone. This allows you to provision infrastructure at no additional licensing cost. SI licensing applies only when Harness deploys artifacts to the provisioned infrastructure in the same stage or pipeline.
Dynamic provisioning by deployment type
Each deployment type Harness supports (Kubernetes, AWS ECS, and others) requires that you map different CDK stack outputs to the Harness infrastructure settings in the pipeline stage. In your Harness pipeline, navigate to the stage Environment tab and select AWS CDK as the provisioner to configure dynamic provisioning.
The following deployment types support dynamic provisioning with AWS CDK:
- Kubernetes infrastructure
- The Kubernetes infrastructure is also used for Helm, Native Helm, and Kustomize deployment types.
- AWS ECS
- AWS Lambda
- Spot Elastigroup
- Serverless.com framework for AWS Lambda
- Tanzu Application Services
- VM deployments using SSH
- Windows VM deployments using WinRM
Go to the documentation for your deployment type to understand which CDK stack outputs are required for dynamic provisioning.
Example: ECS infrastructure provisioning
This example shows an AWS CDK TypeScript application that provisions the infrastructure for an ECS deployment and includes the required output of AWS region.
import * as cdk from 'aws-cdk-lib';
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: cdk.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');
Using CDK outputs in Harness
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
Go to Use Harness expressions to learn about Harness expression syntax.
Mapping CDK outputs to Harness infrastructure settings
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.
Next steps
You understand AWS CDK provisioning patterns and how to structure CDK applications for different deployment types. You can now configure CDK provisioning in your Harness pipelines.
- Go to AWS CDK Provisioning to configure CDK steps in your pipeline.
- Go to Build your own CDK image to customize CDK plugin images with specific versions and dependencies.
- Go to AWS ECS deployment tutorial to provision ECS infrastructure with CDK and deploy containerized applications.