Moove It is now Qubika. For more information, visit our new website
Visit Qubika

At Moove It, we work with a variety of exciting technologies every day. Today we’re going to explore the AWS CDK, a software development framework.

The AWS Cloud Development Kit allows you to programmatically generate Cloud Formation templates to provision resources on AWS cloud infrastructure. 

It supports the following languages:

  • Java
  • TypeScript
  • Python
  • Javascript
  • C#

This enables you to write your CDK application in your preferred language, using the tools you’re already familiar with. In this article, we’ll focus on Java. 

Installation

Prerequisites

AWS CDK command-line tools

You must provide your credentials and an AWS Region to use the AWS CDK CLI. The CDK looks for identification and region in the following order:

  • Using the –profile option to CDK commands.
  • Using environment variables.
    • AWS_ACCESS_KEY_ID – Specifies your access key.
    • AWS_SECRET_ACCESS_KEY – Specifies your secret access key.
    • AWS_DEFAULT_REGION – Specifies your default Region.
  • Using the default profile as set by the AWS Command Line Interface (AWS CLI) (AWS configure).

Installing AWS CDK

npm install -g aws-cdk

Concepts

AWS CDK

Construct: Represents a cloud component. It can be a low-level component (S3 bucket) or a higher-level one like an ECS application that includes other services like ELB, Escalation Rules, etc.

Stack: Deployment unit. Each one relates to an AWS CloudFormation stack. All resources on the same stack are provisioned as a single deploy unit.

Environment:  An environment is the target AWS account and AWS Region into which the stack is intended to be deployed.

Tagging: Allows you to tag all resources related to a construct (Tag.add(myConstruct, ‘key’, ‘value’);)

Assets: Files or directories in your CDK application to be referenced by supported resources. Ej: ECS container images could use a docket file embedded in your app. A lambda handler could reference an asset to specify its code.

Main commands

Create a new folder for your CDK application. Inside that folder, open terminal and run:

cdk init -- language java

This will create a new maven project. When your application is ready, you will need to run: 

cdk synth

That command will generate the Cloud Formation template that will be used on AWS to provision all resources.

To deploy your application to AWS and effectively provision all resources, you need to run: 

cdk deploy 

Another useful command is cdk diff, which will show you the difference between the application currently deployed and your local version of it.

Example ECS

App app = new App(); //Instantiates a new CDK app, which is a construct in itself

Stack stack =  new Stack(app, "hello-cdk"); //Registers a new Stack to the construct

Vpc vpc = new Vpc(stack, "VPC"); //Creates a new VPN associated to a stack

Cluster cluster = new Cluster(stack, "My Cluster", ClusterProps.builder().vpc(vpc).build()); //Creates a new cluster associated with the vpn

FargateService service = new FargateService(stack,"My Service",

       FargateServiceProps.builder().cluster(cluster).desiredCount(5)

               .taskDefinition(new TaskDefinition(stack,"My Task", TaskDefinitionProps.builder().cpu("512").memoryMiB("1024").build()))

               .build()); //Creates a Fargate Service and task associated with this cluster

app.synth();

After running cdk deploy for this application, an ECS cluster will be created associated with a VPC and a Fargate task using 512 CPU units and 1024 MB of memory.

Specifying Region/Account

Stack stackUs =  new Stack(app, "hello-cdk", StackProps.builder().env(Environment.builder().account("123").region("us-west-2").build()).build()); //Registers a new Stack to the construct

Stack stackUe =  new Stack(app, "hello-cdk", StackProps.builder().env(Environment.builder().account("123").region("us-west-2").build()).build()); //Registers a new Stack to the construct

Doing this from a single CDK app, you can provision resources on multiple AWS accounts and/or regions.

More info

API Reference: https://docs.aws.amazon.com/cdk/latest/guide/reference.html

Construct Library: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html

Javadocs: https://docs.aws.amazon.com/cdk/api/latest/java/index.html

Get our stories delivered from us to your inbox weekly.