IAC with the AWS Cloud Development Kit


Infrastructure as Code define and deploy IaC with the AWS Cloud Development Kit

In cloud architectures, many components interact with each other: applications want to be deployed and automatically scaled, databases and integration services like load balancer are managed. The AWS Cloud Development Kit helps to cope with the complex scenario.

Companies on the topic

With the Cloud Development Kit, developers can define required cloud resources with programming languages known to them.With the Cloud Development Kit, developers can define required cloud resources with programming languages known to them.

Over time, various approaches have emerged to make infrastructure management automatable. Above all, these processes should also be repeatable, so that not every team has to reset the components manually.

From initial bash scripts to playbooks (to commission individual virtual machines), IaC (infrastructure as Code) technologies have evolved. In the case of the AWS Cloud, YAML or JSON can be used to describe the corresponding infrastructure and then roll it out with the AWS CloudFormation Service.

CloudFormation then takes care of the consistent deployment of the infrastructure. Here is a YAML template for providing an Amazon Simple Storage Service Bucket:

AWSTemplateFormatVersion: '2010-09-09'Parameters:
Type: String
Default: my-default-bucket-name
Description: Name for your AWS S3 bucket
MinLength: 5
MaxLength: 30
Type: AWS::S3::Bucket
Ref: S3BucketNameParameter
DeletionPolicy: Retain
Description: Name of AWS S3 Bucket

Manage cloud architectures with the Cloud Development Kit

Based on CloudFormation, however, another way to model infrastructure has been established: the Cloud Development Kit (CDK). The CDK allows to define the cloud resources with the help of common programming languages.

In addition to TypeScript and Python, Java and .NET are also supported . This gives the CDK the freedom to use all common language-specific on-board tools, for example to define recurring constructs in the form of NPM modules or Maven artifacts and to import them into other team projects.

Developers can also use their experience with these languages to manage cloud resources. The CDK enjoys a growing active community, dedicated developer teams and a variety of solutions and reusable modules based on CDK. The CDK is open source and the corresponding CDK roadmap is publicly available.

Flowchart for the Cloud Devleopment Kit.Flowchart for the Cloud Devleopment Kit.

(Image: Amazon Web Services)

The CDK defines the required infrastructure in one of the supported programming languages. This consists of individual constructs. For deployment, the constructs are validated and then synthesized in CloudFormation templates. This template is then deployed using CloudFormation.

Code example for AWS CDK

A serverless application should serve as an example here. With the Serverless execution model, all infrastructure management tasks – such as deployment, operation, but also scaling of the application – are outsourced to the cloud provider. The customer only pays for the resources as long as they are needed to execute his code.

Overview of the AWS resources used.Overview of the AWS resources used.

(Image: Amazon Web Services)

Specifically, we want to use a serverless Lambda function that can be used for recurring administrative tasks, such as creating backups, status checks, or deleting resources that are no longer needed. This function is called every 5 minutes based on a cron job.

In the CDK code, the stack is defined first. The stack is the unit of all resources to be shared. A lambda function running our application is configured within the stack’s constructor.

Here we define the executable artifact and the JavaScript function to be executed at the call, the runtime configuration, ie Node.JS 14, and the RAM configuration of 128 MB, which is available to the application. Finally, the maximum runtime of the function of 30 seconds is specified. We also configure a rule that is called time-based (every 5 minutes) and in turn calls the lambda function.

After the class definition, we configure the corresponding stack. The desired AWS region Frankfurt (eu-central-1) is hard-coded in which this stack is to be executed. Below is the CDK code for providing a lambda function triggered based on a Cron job.

import { App, Duration, Stack, StackProps } from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as events from '@aws-cdk/aws-events';
import * as targets from '@aws-cdk/aws-events-targets'
import fs = require('fs');export class LambdaCronStack extends Stack {
constructor(app: App, id: string, props?: StackProps) {
super(app, id, props);
      // Erstellung der Lambda Funktion
const lambdaFn = new lambda.Function(this, 'Beispiel-Funktion', {
code: new lambda.InlineCode(
fs.readFileSync('lambda-handler.js', { encoding: 'utf-8' })),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
memorySize: 128,
timeout: Duration.seconds(30)
      // Erstellung des Cron Jobs
const rule = new events.Rule(this, 'Beispiel-Cron-Job', {
schedule: events.Schedule.rate(Duration.minutes(5))
      // Verbindung des Cron Jobs mit der Lambda Funktion
rule.addTarget(new targets.LambdaFunction(lambdaFn));
const app = new App();
new LambdaCronStack(app, 'LambdaCronExample', {
env: {
region: 'eu-central-1'

The following commands can be used to deploy the infrastructure:

$ npm install -g aws-cdk // Installiert das AWS CDK
$ npm install            // Installiert benötigte Abhängigkeiten
$ cdk bootstrap          // Setzt CDK in Region/Account auf
$ cdk deploy             // Deployed den CDK Stack

The following command is used to display the generated template.

$ cdk synth // Synthetisiert den CDK Code in ein CloudFormation template

Once the stack is no longer needed, all these resources can be cleaned up with a command to save costs:

$ cdk destroy

These and other examples can be found in the following CDK examples repository at GitHub. For detailed documentation on the use of individual services, please refer to the CDK documentation here.


In addition to the mentioned points, the Infrastructure as Code approach also offers the advantage that infrastructure descriptions can be managed with the existing code base. The same applies to automated unit tests: these can be used to ensure that the infrastructure definition remains valid and that no essential aspects are lost.

Luis Morales

Luis Morales (Image: Amazon Web Services)

As we have seen in our demo application, it is easy to get into infrastructure management with CDK. We can define, configure, and pass runtime information to AWS cloud services. The corresponding example can be adapted to corresponding administrative usecases with minimal effort. Alternatively, the code can be detached as a construct with only a few adjustments and thus be used for recurring administrative tasks.

* Luis Morales works as a Solutions Architect at AWS. He specializes in software engineering, test-driven development, distributed systems, everything as code and security.


Ready to see us in action:

More To Explore

Enable registration in settings - general
Have any project in mind?

Contact us: