{{announcement.body}}
{{announcement.title}}

An Introduction to AWS Cloud Development Kit – CDK

DZone 's Guide to

An Introduction to AWS Cloud Development Kit – CDK

In this article, I'll provide a sneak peek of AWS' CDK and provide an example on how to create a static website on S3.

· Cloud Zone ·
Free Resource

The introduction of Cloud Development Kit (CDK) by AWS has been a valuable addition to its bouquet of services. CDK is an Infrastructure as Code (IAC) service from AWS. Yes, you could create cloudformation scripts in json or yaml and use it to provision your infrastructure. But who likes to write json files? I have always been averse to developing large json scripts to provision and maintain my infrastructure on AWS. I always wondered why isn’t there a simpler way of doing this – like developing deployment scripts on my favourite programming language.

AWS heard me alright and came up with CDK last year. CDK helps to define infrastructure in code and provision them through Cloudformation. With CDK it’s so much easier to build reusable deployment scripts. Moreover, it facilitates the use of the core features of a programming language in structuring and generating Cloudformation scripts. I decided to take it out for a spin to ascertain whether it really keeps up to its promises. Here, I’ll provide a sneak peek - an example to create a static website on S3.

This example uses Java programing language. The first step is to install and configure the CDK command line tool. The dependencies related to CDK API can be added to maven or gradle.  Once the code is in place and the build completed, executing cdk synth generates all required Cloudformation scripts. Running cdk deploy executes these Cloudformation scripts and provisions resources described within those scripts in the AWS account configured in CDK. The following diagram illustrates this workflow.

Below is the CDK code for creating a static website on S3.

 

Java
 




x


 
1
package cdk.example;
2
 
          
3
import java.util.Calendar;
4
 
          
5
import software.amazon.awscdk.core.Construct;
6
import software.amazon.awscdk.core.Stack;
7
import software.amazon.awscdk.core.StackProps;
8
import software.amazon.awscdk.services.iam.Effect;
9
import software.amazon.awscdk.services.iam.PolicyDocument;
10
import software.amazon.awscdk.services.iam.PolicyStatement;
11
import software.amazon.awscdk.services.s3.CfnBucket;
12
import software.amazon.awscdk.services.s3.CfnBucketPolicy;
13
import software.amazon.awscdk.services.s3.CfnBucketPolicyProps;
14
import software.amazon.awscdk.services.s3.CfnBucketProps;
15
 
          
16
public class StaticWebsiteStack extends Stack {
17
 
          
18
    public StaticWebsiteStack(final Construct parent, final String id) {
19
        this(parent, id, null);
20
    }
21
 
          
22
    public StaticWebsiteStack(final Construct parent, final String id, final StackProps props) {
23
        super(parent, id, props);
24
 
          
25
    }
26
 
          
27
    public void build() {
28
 
          
29
        String bucketName = "website-bucket-" + Calendar.getInstance().getTimeInMillis();
30
 
          
31
        CfnBucketProps.Builder bucketPropsBuilder = CfnBucketProps.builder();
32
        bucketPropsBuilder = bucketPropsBuilder.bucketName(bucketName);
33
 
          
34
        CfnBucket.WebsiteConfigurationProperty websiteConfigProp = CfnBucket.WebsiteConfigurationProperty.builder()
35
                .indexDocument("index.html").errorDocument("error.html").build();
36
        bucketPropsBuilder = bucketPropsBuilder.websiteConfiguration(websiteConfigProp);
37
 
          
38
        CfnBucketProps bucketProps = bucketPropsBuilder.build();
39
        CfnBucket bucket = new CfnBucket(this, "website-bucket", bucketProps);
40
        
41
        PolicyStatement  bucketPolicy = new PolicyStatement();
42
        bucketPolicy.setSid("PublicReadGetObject");
43
        bucketPolicy.setEffect(Effect.ALLOW);
44
        bucketPolicy.addAnyPrincipal();
45
        bucketPolicy.addActions("s3:GetObject");
46
        bucketPolicy.addResources(bucket.getAttrArn() + "/*");
47
        
48
        PolicyDocument policyDocument = new PolicyDocument();
49
        policyDocument.addStatements(bucketPolicy);
50
        
51
        new CfnBucketPolicy(this, "website-bucket-policy", CfnBucketPolicyProps.builder().bucket(bucket.getRef()).policyDocument(policyDocument).build()); 
52
        
53
    }
54
}
55
 
          



Running cdk synth on this generates the following Cloudformation script and a subsequent execution of cdk deploy will actually provision an S3 bucket for static website hosting. 

JSON
 




x


1
{
2
  "Resources": {
3
    "websitebucket": {
4
      "Type": "AWS::S3::Bucket",
5
      "Properties": {
6
        "AccessControl": "PublicRead",
7
        "BucketName": "website-bucket-xxxxxxxxx",
8
        "WebsiteConfiguration": {
9
          "ErrorDocument": "error.html",
10
          "IndexDocument": "index.html"
11
        }
12
      },
13
      "Metadata": {
14
        "aws:cdk:path": "websiteStack/website-bucket"
15
      }
16
    },
17
    "websitebucketpolicy": {
18
      "Type": "AWS::S3::BucketPolicy",
19
      "Properties": {
20
        "Bucket": {
21
          "Ref": "websitebucket"
22
        },
23
        "PolicyDocument": {
24
          "Statement": [
25
            {
26
              "Action": "s3:GetObject",
27
              "Effect": "Allow",
28
              "Principal": "*",
29
              "Resource": {
30
                "Fn::Join": [
31
                  "",
32
                  [
33
                    {
34
                      "Fn::GetAtt": [
35
                        "websitebucket",
36
                        "Arn"
37
                      ]
38
                    },
39
                    "/*"
40
                  ]
41
                ]
42
              },
43
              "Sid": "PublicReadGetObject"
44
            }
45
          ],
46
          "Version": "2012-10-17"
47
        }
48
      },
49
      "Metadata": {
50
        "aws:cdk:path": "websiteStack/website-bucket-policy"
51
      }
52
    }
53
  }
54
}



CDK comes with loads of promises and enormous possibilities. It definitely is a winner and a potential game changer in the field of IAC. I strongly believe that CDK will soon get popular among cloud developers and will eventually witness large scale adoption.

Topics:
aws, aws infrastructure, iac, infrastructure as code

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}