{"id":4334,"date":"2022-04-15T13:58:00","date_gmt":"2022-04-15T11:58:00","guid":{"rendered":"https:\/\/blog.besharp.it\/?p=4334"},"modified":"2022-04-15T17:17:44","modified_gmt":"2022-04-15T15:17:44","slug":"step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript","status":"publish","type":"post","link":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/","title":{"rendered":"Step Function with AWS CDK in action: our points of view about it using Typescript"},"content":{"rendered":"\n

Introduction<\/h2>\n\n\n\n

The infrastructure as Code (IaC) has been one of the most popular keywords in the cloud topic.<\/p>\n\n\n\n

This made cloud infrastructure implementation much more reusable, versionable, and maintainable.<\/p>\n\n\n\n

Several implementations and standards of this concept are recently born.<\/p>\n\n\n\n

Some of those are Ops<\/em>-oriented, which are usually based on declarative configuration files, like Terraform<\/strong> or AWS SAM<\/strong>.<\/p>\n\n\n\n

Other tools are more Dev<\/em>-oriented, using popular programming languages like Typescript<\/strong>, Java<\/strong>, or Python<\/strong>.<\/p>\n\n\n\n

The official tool to develop your IaC in your AWS account is the Cloud Development Kit (CDK), made by AWS.<\/p>\n\n\n\n

We have used it a lot, for different use cases, and today we are going to share our experiences to provide you with some useful tips. Let’s dive deep!<\/p>\n\n\n\n

Programming language: Typescript<\/h2>\n\n\n\n

AWS CDK is available for different programming languages, but we usually chose to use Typescript because it allows developers to use Javascript elasticity and Java type safety<\/strong>.<\/p>\n\n\n\n

Those characteristics are very useful to speed up development, keeping type safety’s advantages and enabling the usage of object-oriented design patterns.<\/p>\n\n\n\n

Project Structure<\/h2>\n\n\n\n

A very cool thing about AWS CDK is the modularized structure.<\/p>\n\n\n\n

Each AWS service it’s represented as a node module, so it’s a dependency of your node project.<\/p>\n\n\n\n

This permits you to create a lightweight project without useless imports.<\/p>\n\n\n\n

Usually, we design the CDK project structure by splitting the main stack into several nested stacks, using domain-driven criteria.<\/p>\n\n\n\n

Abstractions layers<\/h2>\n\n\n\n

AWS CDK provides you with several abstraction layers to create AWS -resources.<\/p>\n\n\n\n

We have identified 3 abstraction’s macro-categories:<\/p>\n\n\n\n

1 – CloudFormation Plain resources:<\/strong><\/h4>\n\n\n\n

Typically can be identified from their name’s root: “Cfn”. Those elements give the possibility to customize your resources the same way you’d do on a CloudFormation Template.<\/p>\n\n\n\n

(ie: CfnStateMachineProps<\/a>)<\/p>\n\n\n\n

2 – Attributes abstraction resources:<\/strong><\/h4>\n\n\n\n

These constructors are our favorites, they abstract and simplify some tricky resource configurations, without losing control of the created resources.<\/p>\n\n\n\n

(ie: StateMachine<\/a>)<\/p>\n\n\n\n

3 – Group of resource abstraction:<\/strong><\/h4>\n\n\n\n

These constructors can be very useful if you don’t want to customize every resource’s details.<\/p>\n\n\n\n

They provide a very abstract configuration that creates several cloud resources.<\/p>\n\n\n\n

We recommend reading them very carefully the documentation before implementing them to be sure that you need all resources that will be created.<\/p>\n\n\n\n

(ie: FargateService<\/a>)<\/p>\n\n\n\n

A success case with StepFunction<\/h2>\n\n\n\n

AWS StepFunction it’s a service that enables the creation of State machines on the cloud.<\/p>\n\n\n\n

We found it very handy and well implemented into AWS CDK.<\/p>\n\n\n\n

Each step of a state machine has one input and one output and can be of several types:<\/p>\n\n\n\n

  • Pass: <\/strong>This state passes its input to its output, without performing work. Pass states are useful when constructing and debugging state machines.<\/li>
  • Task: <\/strong>This represents an operation to execute, it’s integrable directly with a Lambda Invoke, or you can specify parameters that call a specific AWS service<\/li>
  • Choice: <\/strong>It is possible to configure a condition that permits the user to change execution flow based on the output of the previous state<\/li>
  • Wait: <\/strong>It’s possible to suspend the machine execution for a specific time<\/li>
  • Succeed: <\/strong>When a machine execution finishes with success<\/li>
  • Fail: <\/strong>When a machine execution finishes with some errors<\/li>
  • Parallel: <\/strong>This permits the execution of a state set that will be executed in parallel, using a single input value.<\/li>
  • Map: <\/strong>This permits the execution of a state set, using an array of input for each state.<\/li><\/ul>\n\n\n\n

    To create and make the state machine work there is a JSON-based language called Amazon States Language, it enables developers to create conditions and transitions that require manual interactions that connect steps to each other.<\/p>\n\n\n\n

    This language is a JSON object that contains these attributes:<\/p>\n\n\n\n

    Comment<\/strong><\/h4>\n\n\n\n

    This field is used as you use comments in a traditional programming language, it is used to describe State machine behaviors, is not required, so you can omit it if you want.<\/p>\n\n\n\n

    TimeoutSeconds<\/strong><\/h4>\n\n\n\n

    This field is not required and it is a number, it defines the maximum number of seconds an execution of the state machine can run. If it runs longer than the specified time, the execution fails with a state.<\/p>\n\n\n\n

    Version<\/strong><\/h4>\n\n\n\n

    This attribute is not required, it is the version of the Amazon States Language and is used in the state machine (default is “1.0”).<\/p>\n\n\n\n

    States<\/strong><\/h4>\n\n\n\n

    This field is mandatory and is a JSON object, used to describe the states that compose machine state.<\/p>\n\n\n\n

    Each key of that object is the step name.<\/p>\n\n\n\n

    Here is an example of it:<\/p>\n\n\n\n

    {\n    \"State1\" : {\n    },\n\n    \"State2\" : {\n    },\n    ...\n}<\/code><\/pre>\n\n\n\n

    StartAt<\/strong><\/h4>\n\n\n\n

    This attribute is required and defines the first state to invoke to start State Machine.<\/p>\n\n\n\n

    To create a Step Function using IaC technology with official AWS tools, you can use the CloudFormation service.<\/p>\n\n\n\n

    This is an example of a very simple flow implementation of a state machine.<\/p>\n\n\n\n

    <\/p>\n\n\n\n

    \"\"<\/figure><\/div>\n\n\n\n

    <\/p>\n\n\n\n

    As you can notice, a simple HelloWorld can be very verbose and tricky to configure with a standard Cloudformation template, because the JSON State language is not well integrated with the YAML template.<\/p>\n\n\n\n

    In one of our use cases, we needed to develop a flow for parallel operations and elaborate a report when all are completed.<\/p>\n\n\n\n

    With a standard template, this can be a little painful, so we tried to do it with CDK.<\/p>\n\n\n\n

    We found out that CDK other than providing a way to create and integrate cloud resources with your StepFunction easily also provides State Language abstractions.<\/p>\n\n\n\n

    Thanks to that writing a flow is more intuitive and maintainable.<\/p>\n\n\n\n

    <\/p>\n\n\n\n

    \"\"<\/figure><\/div>\n\n\n\n

    <\/p>\n\n\n\n

    Undoubtedly, AWS CDK is a very powerful tool. Anyway, it is important to keep in mind that it brings the need for a lot of programming knowledge. For this reason, it is necessary to master fundamental topics such as OOP (Object-oriented programming<\/strong>) before approaching CDK constructors.<\/p>\n\n\n\n

    This will allow you to create a scalable and maintainable code structure and to fully take advantage of IaC.<\/p>\n\n\n\n

    In conclusion<\/h2>\n\n\n\n

    In this article, we have shared a real use case where AWS CDK can be very useful and some best practices that we usually implement in projects.<\/p>\n\n\n\n

    I hope you enjoyed this overview of AWS CDK and StepFunction. All questions are welcome!<\/p>\n\n\n\n

    See you again in 14 days for a new article on Proud2beCloud<\/strong>!<\/p>\n","protected":false},"excerpt":{"rendered":"

    Introduction The infrastructure as Code (IaC) has been one of the most popular keywords in the cloud topic. This made […]<\/p>\n","protected":false},"author":10,"featured_media":4370,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[478],"tags":[487,254,392],"yoast_head":"\nStep Function with AWS CDK in action: our points of view about it using Typescript - Proud2beCloud Blog<\/title>\n<meta name=\"description\" content=\"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Step Function with AWS CDK in action with Typescript\" \/>\n<meta property=\"og:description\" content=\"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/\" \/>\n<meta property=\"og:site_name\" content=\"Proud2beCloud Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-15T11:58:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-15T15:17:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.besharp.it\/wp-content\/uploads\/2022\/04\/Copertina-blog-15-04-22_15-04-22-social-eng-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Paolo Di Ciaula\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Step Function with AWS CDK in action with Typescript\" \/>\n<meta name=\"twitter:description\" content=\"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/blog.besharp.it\/wp-content\/uploads\/2022\/04\/Copertina-blog-15-04-22_15-04-22-social-eng-1.png\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paolo Di Ciaula\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/\",\"url\":\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/\",\"name\":\"Step Function with AWS CDK in action: our points of view about it using Typescript - Proud2beCloud Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.besharp.it\/#website\"},\"datePublished\":\"2022-04-15T11:58:00+00:00\",\"dateModified\":\"2022-04-15T15:17:44+00:00\",\"author\":{\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c\"},\"description\":\"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.besharp.it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Step Function with AWS CDK in action: our points of view about it using Typescript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.besharp.it\/#website\",\"url\":\"https:\/\/blog.besharp.it\/\",\"name\":\"Proud2beCloud Blog\",\"description\":\"il blog di beSharp\",\"alternateName\":\"Proud2beCloud Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.besharp.it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c\",\"name\":\"Paolo Di Ciaula\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.besharp.it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g\",\"caption\":\"Paolo Di Ciaula\"},\"description\":\"DevOps Engineer, Frontend Developer, and Mobile App Developer @ beSharp. I divide my free time between good music, development, and... beer (sometimes mixed for better results ;D)\",\"url\":\"https:\/\/blog.besharp.it\/author\/paolo-di-ciaula\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Step Function with AWS CDK in action: our points of view about it using Typescript - Proud2beCloud Blog","description":"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/","og_locale":"en_US","og_type":"article","og_title":"Step Function with AWS CDK in action with Typescript","og_description":"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.","og_url":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/","og_site_name":"Proud2beCloud Blog","article_published_time":"2022-04-15T11:58:00+00:00","article_modified_time":"2022-04-15T15:17:44+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/blog.besharp.it\/wp-content\/uploads\/2022\/04\/Copertina-blog-15-04-22_15-04-22-social-eng-1.png","type":"image\/png"}],"author":"Paolo Di Ciaula","twitter_card":"summary_large_image","twitter_title":"Step Function with AWS CDK in action with Typescript","twitter_description":"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.","twitter_image":"https:\/\/blog.besharp.it\/wp-content\/uploads\/2022\/04\/Copertina-blog-15-04-22_15-04-22-social-eng-1.png","twitter_misc":{"Written by":"Paolo Di Ciaula","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/","url":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/","name":"Step Function with AWS CDK in action: our points of view about it using Typescript - Proud2beCloud Blog","isPartOf":{"@id":"https:\/\/blog.besharp.it\/#website"},"datePublished":"2022-04-15T11:58:00+00:00","dateModified":"2022-04-15T15:17:44+00:00","author":{"@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c"},"description":"In this article, we'll share an overview of AWS CDK and StepFunction in action using Typescript as a programming language.","breadcrumb":{"@id":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.besharp.it\/step-function-with-aws-cdk-in-action-our-points-of-view-about-it-using-typescript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.besharp.it\/"},{"@type":"ListItem","position":2,"name":"Step Function with AWS CDK in action: our points of view about it using Typescript"}]},{"@type":"WebSite","@id":"https:\/\/blog.besharp.it\/#website","url":"https:\/\/blog.besharp.it\/","name":"Proud2beCloud Blog","description":"il blog di beSharp","alternateName":"Proud2beCloud Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.besharp.it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/a984367e499bce5e12199e34d2c4181c","name":"Paolo Di Ciaula","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.besharp.it\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7a3395eed9ec6267a1c136c3cf0a3903?s=96&d=mm&r=g","caption":"Paolo Di Ciaula"},"description":"DevOps Engineer, Frontend Developer, and Mobile App Developer @ beSharp. I divide my free time between good music, development, and... beer (sometimes mixed for better results ;D)","url":"https:\/\/blog.besharp.it\/author\/paolo-di-ciaula\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts\/4334"}],"collection":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/comments?post=4334"}],"version-history":[{"count":0,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/posts\/4334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/media\/4370"}],"wp:attachment":[{"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/media?parent=4334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/categories?post=4334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.besharp.it\/wp-json\/wp\/v2\/tags?post=4334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}