<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: M Avdi</title>
    <description>The latest articles on DEV Community by M Avdi (@mavdi).</description>
    <link>https://dev.to/mavdi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F418998%2F81627a32-7996-456b-860d-d1dec8e50042.jpeg</url>
      <title>DEV Community: M Avdi</title>
      <link>https://dev.to/mavdi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mavdi"/>
    <language>en</language>
    <item>
      <title>Creating an AWS CodePipeline Stack and troubleshooting common issues</title>
      <dc:creator>M Avdi</dc:creator>
      <pubDate>Wed, 01 Jul 2020 05:06:57 +0000</pubDate>
      <link>https://dev.to/mavdi/creating-an-aws-codepipeline-stack-and-troubleshooting-common-issues-3kp5</link>
      <guid>https://dev.to/mavdi/creating-an-aws-codepipeline-stack-and-troubleshooting-common-issues-3kp5</guid>
      <description>&lt;h1&gt;
  
  
  Creating an AWS CodePipeline Stack and troubleshooting common issues
&lt;/h1&gt;

&lt;p&gt;AWS CDK is a relatively new framework that aims for faster development of AWS Cloud stacks when compared to AWS CloudFormation and AWS SAM. This article will present how to deploy a complete AWS CodePipeline using AWS CDK and troubleshoot all common issues that may occur in the process of creating the CDK application. For additional information about the framework, read the previous article named "&lt;a href=""&gt;How AWS CDK facilitates the development process of AWS Cloud Stacks&lt;/a&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS CDK setup
&lt;/h2&gt;

&lt;p&gt;To create any AWS CDK application, it is necessary to have the CDK installed in the machine and updated to its latest version. To do so, the following command can be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g aws-cdk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this command finishes its process, AWS CDK will be updated to its latest version. This process is important due to updates constantly being released, integrating new functionalities and solving issues from previous versions. To check out if the current CDK version installed is the updated, check the latest release on the &lt;a href="https://github.com/aws/aws-cdk/releases"&gt;CDK Github repository&lt;/a&gt; and compare with the output of the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk --version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick starting an AWS CDK application
&lt;/h2&gt;

&lt;p&gt;With the latest version of the AWS CDK installed, it is possible to start the CDK project that will generate the desired AWS cloud environment containing all the resources needed for a given application. The following command is necessary to start the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk init app --language=typescript
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The command above starts a CDK application using the app template, in the language typescript. Issues that may happen when using this command are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Unknown init template: it is important to notice that app does &lt;strong&gt;not&lt;/strong&gt; stand for the name of the application. It stands for the template that is going to be used to quick start the application. Three templates are available: &lt;code&gt;app&lt;/code&gt;, &lt;code&gt;lib&lt;/code&gt; and &lt;code&gt;sample-app&lt;/code&gt;, and each template has a list of languages supported. The current default template is &lt;code&gt;app&lt;/code&gt;;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unsupported language: as described in the previous item, some languages are not yet supported by all templates. The command below will specify all templates available and which languages are supported by them for the current CDK version installed.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk init --list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No language was selected: by the time of this article, AWS CDK does not have a standard language. One of the main goals of the framework is to let the developers decide which programming languages they want to use. Due to this, this event will fire when running the init command without specifying the language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;cdk init&lt;/code&gt; cannot be run in a non-empty directory: since it is only specified in this command the name of the template to be used, AWS CDK relies on the folder name to generate the name of the application, and it cannot be initialized in any folder that is not empty. To solve this issue, a new folder needs to be created, and the command should be used again inside this new folder.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  AWS CDK constructs and versioning
&lt;/h2&gt;

&lt;p&gt;The main goal when creating CDK applications is to easily deploy AWS Cloud resources. With that in mind, each resource that is going to be used in the application needs to be installed. Each language has its own way in how to install those packages, and documentation about it can be found in the&lt;a href="https://docs.aws.amazon.com/cdk/latest/guide/troubleshooting.html"&gt;troubleshooting section&lt;/a&gt;, and each construct module name can be found in its section in the &lt;a href="https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html"&gt;API Reference&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The same CDK application is usually used for long periods of time, as it defines the entire architecture of AWS projects. It is a common necessity to add more constructs to the stacks, as the project keeps growing, which can be achieved, using Amazon S3 as example, using the command below: &lt;/p&gt;

&lt;p&gt;npm i @aws-cdk/aws-s3&lt;/p&gt;

&lt;p&gt;By doing so, an issue might appear: dependencies which versions are mismatching, triggering the error &lt;code&gt;Argument of type 'this' is not assignable to parameter of type 'Construct'&lt;/code&gt;. It happens when constructs can't interact with each other, making the parameter this passed to be marked as invalid. To prevent this issue from happening, it is necessary to update the dependencies using the command &lt;code&gt;npm update&lt;/code&gt; every time a new construct is added to the project or a new version of the AWS CDK core is released.&lt;/p&gt;

&lt;p&gt;It is also important to notice that the usage of some resources might require the addition of some dependencies, as the case of AWS CodePipeline, which may require features present in the &lt;code&gt;aws-cdk/aws-codepipeline-actions&lt;/code&gt;. Being aware of those cases might be helpful to prevent issues and delays in the stack development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Development of an AWS CodePipeline Stack with AWS CDK
&lt;/h2&gt;

&lt;p&gt;AWS CodePipeline is a cloud resource that provides CI/CD services in the AWS environment. In this section, a walkthrough on how to create a CodePipeline using AWS CDK in TypeScript is going to be provided, as well as solving some of the issues that may be presented while doing so. For this example, a pipeline that will automate the deployment of a ReactJS application present in a GitHub repository, which will be hosted on a Amazon S3 bucket is going to be used.&lt;/p&gt;

&lt;p&gt;CodePipelines consist of stages, which represent actions that are going to be performed on the artifacts used as source. The pipeline that is used as example for this article will have three stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Source: in this stage, the ReactJS application code will be gathered via webhook to be used as input for the pipeline;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build: this stage will be responsible to build the artifacts coming from the previous stage in a CodeBuild instance;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deploy: the last stage of this example pipeline, it will be responsible for deploying the S3 hosted website in the web.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Initializing the CodePipeline environment
&lt;/h3&gt;

&lt;p&gt;To define a standardization of all the resources existent in the cloud environment, it is a good practice to set the resources name with a prefix indicating the stack they belong to. To do so, a property named &lt;code&gt;environment&lt;/code&gt; is going to be set in the &lt;code&gt;cdk.json&lt;/code&gt; file and imported in a context file with other environment variables the application may require. This is how the JSON file should look like after setting the property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "app": "npx ts-node bin/aws-cdk-example.ts",
    "context": {
        "@aws-cdk/core:enableStackNameDuplicates": "true",
        "aws-cdk:enableDiffNoFail": "true",
        "environment": "aws-cdk-example"
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Besides the property just set, the entire file stays the same as it was created using the init command. An issue that may happen when modifying this file or the project structure is the &lt;code&gt;Cannot find module&lt;/code&gt; error, being triggered when the file set in the &lt;code&gt;app&lt;/code&gt; property is not present in the given path, usually happening after the file has been renamed or moved to better suit the development team's pattern. To solve it, change the value of the property with the proper path to the file where the stacks are being synthesized.&lt;/p&gt;

&lt;p&gt;To get those values, a context function is recommended, importing all the context variables used in the application. The following snippet is the function and its interface requiring the variable from the JSON file and returning it when called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Construct } from '@aws-cdk/core';

interface IContext {
    environment: string
};

function getContext(app: Construct): IContext {
    return {
        environment: app.node.tryGetContext('environment')
    }
}

export default getContext;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is important to point out that the string inside the &lt;code&gt;tryGetContext&lt;/code&gt; call must match the key from the JSON object, which otherwise will result in an undefined value and possible error generation, depending on the usage of the variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initializing the hosting bucket
&lt;/h3&gt;

&lt;p&gt;This bucket is going to serve as the host in which the ReactJS application is going to be stored and served in the web. For this example, a scenario where all users will be able to access the hosted website is going to be used, using CORS rules and public read access to allow its usage. The following snippet demonstrates how to define an S3 bucket to host a ReactJS application, using the &lt;code&gt;aws-s3&lt;/code&gt; module from AWS CDK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const corsRules: [CorsRule] = [{
    allowedOrigins: ['*'],
    allowedMethods: [HttpMethods.GET]
}];

const bucketName = `${environment}-bucket`
const bucket = new Bucket(this, bucketName, {
    bucketName: bucketName,
    publicReadAccess: true,
    cors: corsRules,
    websiteIndexDocument: "index.html",
    websiteErrorDocument: "index.html",
    removalPolicy: RemovalPolicy.DESTROY
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;environment&lt;/code&gt; present in the code represents the context variable previously set in the &lt;code&gt;cdk.json&lt;/code&gt; file, allowing the cloud resources to have names following the same pattern. Issues that may be presented when creating such S3 buckets are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Forbidden status when entering the website: This issue happens when not setting the &lt;code&gt;publicReadAccess&lt;/code&gt; property to the S3 bucket, as for safety purposes, the default value for this field is set as &lt;code&gt;false&lt;/code&gt;. To solve this issue, the &lt;code&gt;publicReadAccess&lt;/code&gt; property must be set as &lt;code&gt;true&lt;/code&gt;, granting read access for everyone in the web accessing the bucket URL;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Page not found: This issue, usually generated when creating websites with ReactJS and similar frameworks, is generated due to the fact the &lt;code&gt;websiteErrorDocument&lt;/code&gt; is pointing to a nonexistent file. since ReactJS builds do not generate an &lt;code&gt;error.html&lt;/code&gt; file to act as a redirect page when a route is not found. To solve this issue, both &lt;code&gt;websiteErrorDocument&lt;/code&gt; and &lt;code&gt;websiteIndexDocument&lt;/code&gt; must be set as &lt;code&gt;index.html&lt;/code&gt; as parameters when creating the resource Construct;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bucket name already taken: Amazon S3 buckets have one rule for their naming: they must be unique globally. This means that the bucket name created must be well chosen and very specific, preventing the error &lt;code&gt;BucketAlreadyExists&lt;/code&gt; to be generated when deploying the CDK.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As for the &lt;code&gt;removalPolicy&lt;/code&gt; property in the bucket creation, it is set to destroy to allow the removal of this bucket when running the &lt;code&gt;cdk destroy&lt;/code&gt; command, overriding the default &lt;code&gt;RETAIN&lt;/code&gt; value. This allows the deletion of the given bucket if the same is empty when the command is used, although it will still need manual clean up and removal if the bucket contains any kind of data on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building the website with AWS CodeBuild
&lt;/h3&gt;

&lt;p&gt;The next step after creating the bucket where the website is going to be hosted is creating the CodeBuild project that will build the website with the code coming from the GitHub repository. The first step to achieve this goal is creating the AWS IAM Role the CodeBuild project will have, granting it permissions to perform actions on other AWS cloud resources, such as the website bucket. The following code snippet shows how to create this role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const codebuildRoleName = `${environment}-codebuild-role`;

const codebuildRole: Role = new Role(this, codebuildRoleName, {
    roleName: codebuildRoleName,
    assumedBy: new ServicePrincipal('codebuild.amazonaws.com')
});

codebuildRole.addToPolicy(new PolicyStatement({
    effect: Effect.ALLOW,
    actions: [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "s3:*"
    ],
    resources: [artifactsBucket.bucketArn]
}));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The definition of the role has one important property: &lt;code&gt;assumedBy&lt;/code&gt;. This property is responsible to tell the AWS environment which service this role is going to work for, being here assigned to &lt;code&gt;codebuild.amazonaws.com&lt;/code&gt;, which represents AWS CodeBuild. It is important to assure that the proper service principal name is specified, as it is going to create the Trusted Entity AWS IAM will use to give the resource the basic permissions to operate. If the value is not properly set, an error like this will be given when running the pipeline: &lt;code&gt;Error calling startBuild: CodeBuild is not authorized to perform: sts:AssumeRole&lt;/code&gt;. To solve it, simply set the &lt;code&gt;ServicePrincipal&lt;/code&gt; to the proper value of the desired AWS cloud resource.&lt;/p&gt;

&lt;p&gt;As for the next code section, it is handling the policies that are granted to the previously created role. IAM policies allow AWS cloud resources to perform actions on other resources in the environment. It is a &lt;code&gt;PolicyStatement&lt;/code&gt; that usually contains &lt;code&gt;effect&lt;/code&gt;, &lt;code&gt;actions&lt;/code&gt;, &lt;code&gt;resources&lt;/code&gt; and &lt;code&gt;conditions&lt;/code&gt;, although this CodeBuild project needs no conditions to be set, therefore, this property is not being used. It is very common to have issues in this section of CDK applications, as there is no easy method to define which properties a resource will need to have to run. With time and experience, architecting AWS projects will facilitate the definition of what permissions each resource will require, reducing the time spent on trial and error methods to create resources.&lt;/p&gt;

&lt;p&gt;To solve policy issues, tailing the logs when the stack is being created and when the resource runs is the best method, as the console will specify which permission is missing in the policy set and which resource it needs to be accessed.&lt;/p&gt;

&lt;p&gt;The next step on creating a CodeBuild resource is to create the resource itself, using the &lt;code&gt;aws-codebuild&lt;/code&gt; module from AWS CDK. The following snippet demonstrates how to declare the CodeBuild construct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const codebuildProjectName = `${environment}-codebuild`;

const codebuildProject = new PipelineProject(this, codebuildProjectName, {
    environment: {
        buildImage: LinuxBuildImage.STANDARD_2_0,
        computeType: ComputeType.SMALL
    },
    role: codebuildRole,
    projectName: codebuildProjectName
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The CodeBuild method used to instantiate this resource is by using the &lt;code&gt;PipelineProject&lt;/code&gt; construct, which facilitates the usage of the resource when it is used inside a CodePipeline project, as used in this example. This construct is composed of a large number of properties, but just some of those are needed to be set, and for this project &lt;code&gt;environment&lt;/code&gt;, &lt;code&gt;role&lt;/code&gt; and &lt;code&gt;projectName&lt;/code&gt; are used. The environment property sets the specifications of the CodeBuild instance used to build the given project, and it needs to support in its environment the language that is used in the project that is going to be built. This &lt;a href="https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html"&gt;CodeBuild reference&lt;/a&gt; tells about all images and its specificities, which will prevent CodeBuild to generate an error informing that the runtime version is not supported by the selected build image.&lt;/p&gt;

&lt;p&gt;Another property, not used in this project due to the fact its default value is correct, is the &lt;code&gt;buildSpec&lt;/code&gt;. The buildspec file contains the information that is going to be used by the resource to build the source code, with step-by-step instructions to CodeBuild on how the compilation should proceed. The lack of a file named &lt;code&gt;buildspec.yml&lt;/code&gt; in the root of the project to be built, or the custom if a value is set to the property, will result in an error labeled as &lt;code&gt;YAML_FILE_ERROR Message: YAML file does not exist&lt;/code&gt;. To solve it, this file must be created, containing the steps to build the application, following the pattern present in the &lt;a href="https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html"&gt;aws docs&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the AWS CodePipeline
&lt;/h3&gt;

&lt;p&gt;The CodePipeline is where everything that has been created until now comes together. As CodeBuild also required, the pipeline will also need to have its own IAM &lt;code&gt;Role&lt;/code&gt; to have the necessary permissions to operate successfully, and its definition, which will use the &lt;code&gt;Pipeline&lt;/code&gt; construct from the &lt;code&gt;aws-codepipeline&lt;/code&gt; module present on AWS CDK. The definition of the pipeline will follow the pattern as the snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const codepipelineName = `${environment}-pipeline`;
const codepipeline: Pipeline = new Pipeline(this, codepipelineName, {
    artifactBucket: artifactsBucket,
    pipelineName: codepipelineName,
    role: pipelineRole,
});

const outputSource = new Artifact();
const outputBuild = new Artifact();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That short snippet is necessary to declare the pipeline. The only parameter that wasn't stated yet in this article is the &lt;code&gt;artifactBucket&lt;/code&gt;, which needs to receive a S3 bucket that is going to be used to store the artifacts that are going to be passed from stage to stage. It is an optional parameter, that was set with the goal to standardize the bucket names, when not setting it would result in the creation of a bucket with a default name. Also, the &lt;code&gt;Artifact&lt;/code&gt; items created are going to serve as the artifacts holding the files that are going to be passed on the pipeline from a stage to another.&lt;/p&gt;

&lt;p&gt;After everything is set up, all that is left to do is declare the stages themselves to the pipeline, where all the action will happen. As previously stated, three stages are going to be created: Source, Build and Deploy, and the following sections will state how to create those stages, using the &lt;code&gt;aws-codepipeline-actions&lt;/code&gt; module from AWS CDK.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Adding stages: Source&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This stage is responsible for gathering the code from the GitHub repository that is going to be used as source for the build using CodeBuild. This stage can be added using the snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { repo, owner, oauthToken } = github;
codepipeline.addStage({
    stageName: 'Source',
    actions: [
        new GitHubSourceAction({
            actionName: 'Source',
            oauthToken: new SecretValue(oauthToken),
            output: outputSource,
            owner,
            repo,
        })
    ]
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;github&lt;/code&gt; object will be set in the &lt;code&gt;cdk.json&lt;/code&gt; file, and is selected the same way as the &lt;code&gt;environment&lt;/code&gt; was previously done. It is important to note that the object containing this information should be identical as the object defined in the &lt;code&gt;context.ts&lt;/code&gt; interface, allowing &lt;code&gt;tryGetContext&lt;/code&gt; to get its value without issues. If the pattern is not followed, an error will be triggered as one or more properties will have their values as &lt;code&gt;undefined&lt;/code&gt; due to the fact the values will not be present in the JSON file.&lt;/p&gt;

&lt;p&gt;Basically, each stage will contain a &lt;code&gt;stageName&lt;/code&gt; and the &lt;code&gt;actions&lt;/code&gt; to be performed. In this case, its name is going to be defined as &lt;code&gt;Source&lt;/code&gt; and it will perform a &lt;code&gt;GitHubSourceAction&lt;/code&gt;, which will collect the code in the GitHub repository specified in its parameters. Some issues may be generated when creating this stage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Authentication fails with GitHub oauthToken: GitHub provides to developers a token that needs to be passed to the pipeline to grant access to GitHub repositories. To solve this issue, an oauthToken must be generated in the repository owner's profile containing the following permissions: &lt;code&gt;admin:repo_hook&lt;/code&gt; and &lt;code&gt;repo&lt;/code&gt;. This generated token needs to be passed to the pipeline as a new &lt;code&gt;SecretValue&lt;/code&gt;, which represents a secure way to store the Secret value created in the AWS SecretsManager, in the &lt;code&gt;oathToken&lt;/code&gt; property;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Invalid repository/owner: This issue usually happens when the repository name and/or the owner are mistyped in the context file, therefore generating a incorrect link that CodePipeline can't find, or if the provided oathToken does not have the necessary permissions to visualize the repository. This issue is labeled on CodePipeline as &lt;code&gt;Either the GitHub repository "repo-name" does not exist, or the GitHub access token provided has insufficient permissions to access the repository&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Adding stages: Build&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This stage is responsible for using the previously defined AWS CodeBuild resource to build the code coming from the previous stage. To define this stage, the example snippet below can be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;codepipeline.addStage({
    stageName: 'Build',
    actions: [
        new CodeBuildAction({
            actionName: 'Build',
            project: codebuildProject,
            outputs: [outputBuild],
            input: outputSource
        })
    ]
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This stage is far simpler than the &lt;code&gt;Source&lt;/code&gt; stage, as the CodeBuild resource was already created in this application. It is going to use the &lt;code&gt;outputSource&lt;/code&gt; artifact, which contains the source code coming from the repository, as shown in the &lt;code&gt;Source&lt;/code&gt; stage snippet, to create the build of the application. In this stage, all the CodeBuild issues will appear, as this is when the resource is being put to use, so to debug those possible issues, it is needed to open the CodeBuild project in the AWS Console and check the logs of the building operation. This building process, if successful, will then generate the compiled application that will be sent to the &lt;code&gt;outputBuild&lt;/code&gt; artifact to be used in the next stage.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Adding stages: Deploy&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This stage is responsible for delivering the website via Amazon S3, using the bucket previously created to do so. It will require the usage of the &lt;code&gt;S3DeployAction&lt;/code&gt; from the pipeline actions module, as shown in the snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;codepipeline.addStage({
    stageName: 'Deploy',
    actions: [
        new S3DeployAction({
            actionName: 'Deploy',
            bucket: bucket,
            input: outputBuild
        })
    ]
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This last stage will require just a few parameters, being the only one not described yet, the &lt;code&gt;bucket&lt;/code&gt;, which refers to the bucket the application is going to be deployed, to serve hosting the built website. Most of the common issues that may happen in this stage are being covered in the definition of the bucket, so at this point the pipeline should work just fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Instantiating the artifactsBucket and the CodePipeline
&lt;/h3&gt;

&lt;p&gt;Now, working in a direct class that extends a CDK Stack, the pipeline and its artifacts bucket are going to be created, as the last step before deploying the pipeline. The following code snippet represents this creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const artifactsBucketName = `${environment}-artifacts-bucket`;

const artifactsBucket: Bucket = new Bucket(this, artifactsBucketName, {
    removalPolicy: RemovalPolicy.DESTROY,
    bucketName: artifactsBucketName
});

new CDKExamplePipeline(this, `${environment}-stack`, {
    artifactsBucket
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a quite simple step, considering a S3 bucket has already been created, and the &lt;code&gt;artifactsBucket&lt;/code&gt; is a much less complex resource, as it does not need to host a website. As previously mentioned, the usage of this bucket described in this snippet is optional, since the CodePipeline already creates a bucket by default, but be mindful that this default bucket will most certainly not follow the patterns used in the stack. As for the pipeline itself, the bucket is being passed as a parameter, taking in consideration that the CodePipeline class just created has an interface like the snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface ICDKExamplePipelineProps {
    artifactsBucket: Bucket
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This interface is used as the type of the &lt;code&gt;props&lt;/code&gt; value in the constructor of the class, allowing the developers to create custom interfaces, passing all the parameters that they may want to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Post-development issues
&lt;/h2&gt;

&lt;p&gt;The development of the application might be the trickiest part, but issues might happen even after the code is completely right. Issues that happen following the development of the application may include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Bootstrap required: When the issue &lt;code&gt;Template too large to deploy ("cdk bootstrap" is required)&lt;/code&gt; appears, it means your CDK application generated a CloudFormation template larger than the maximum accepted, which is 50 KiB. This issue is quite simple to solve, since all that is needed to do is run the bootstrap command given in the terminal, containing the &lt;code&gt;bucket ARN&lt;/code&gt; in which the CDK application is going to be uploaded and imported in AWS CloudFormation once the deploy command is used again;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exceeding resource limit: Since AWS CDK applications are converted to CloudFormation templates, they have the same limitations. One of those limitations is the hard cap on the resource quantity present in a single stack: 200. While this may seem a lot, CDK constructs usually generate more than one resource in the stack to get created. To solve this problem, separating the application in multiple stacks is the recommended option, allowing the developers to organize the cloud environment in multiple parts that are connected when deployed;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resources not being deleted after &lt;code&gt;cdk destroy&lt;/code&gt;: The &lt;code&gt;cdk destroy&lt;/code&gt; command has the objective of deleting a given stack and all of its resources. Although, AWS blocks the removal of some resources that may contain data, such as Amazon DynamoDB and Amazon S3, leaving the given resource in the cloud environment even after the entire stack has been destroyed. Specifying &lt;code&gt;RemovalPolicy.DESTROY&lt;/code&gt; in the Construct definition will allow CDK to destroy all empty resources created. For resources that are not empty, running &lt;code&gt;cdk destroy&lt;/code&gt; will generate an exception for the non-empty resources, and those resources will then have to be cleared and deleted manually via AWS console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Conclusion&lt;br&gt;
This article pointed out the most common issues that may happen while developing cloud environments using AWS CDK and how to easily fix them, is a complimentary article for “&lt;a href="https://medium.com/swlh/how-aws-cdk-facilitates-the-development-process-of-aws-cloud-stacks-994e259592ed"&gt;How AWS CDK facilitates the development process of AWS Cloud Stacks&lt;/a&gt;”, which introduces the tool and gives an overview in how it can be utilized. As the framework evolves, those issues will be addressed by AWS and the community, improving the facilitating tool and the lives of every AWS developer.&lt;/p&gt;

&lt;p&gt;Mark Avdi&lt;/p&gt;

&lt;p&gt;CTO | &lt;a href="https://westpoint.io"&gt;WestPoint.io&lt;/a&gt; | Lover of all things Serverless&lt;/p&gt;

</description>
      <category>aws</category>
      <category>react</category>
      <category>devops</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How AWS CDK facilitates the development process of AWS Cloud Stacks
</title>
      <dc:creator>M Avdi</dc:creator>
      <pubDate>Sun, 28 Jun 2020 17:03:21 +0000</pubDate>
      <link>https://dev.to/mavdi/how-aws-cdk-facilitates-the-development-process-of-aws-cloud-stacks-1kai</link>
      <guid>https://dev.to/mavdi/how-aws-cdk-facilitates-the-development-process-of-aws-cloud-stacks-1kai</guid>
      <description>&lt;p&gt;Working with AWS can be quite a complex task at times, due to the fact that well composed architectures and solid infrastructures might be troublesome to deploy and maintain in the long run. A main software development goal is to always optimize the development flow by making processes as simple as possible, from the programming language to each and every framework to be used in the project. In AWS it is no different. Even though CloudFormation was already a hugely beneficial tool, the launch of SAM helped even more developers across the world to architect their infrastructures with a smooth efficiency, reducing the time to plan infrastructures and increase productive development time.&lt;/p&gt;

&lt;p&gt;Last year, AWS took Cloudformation one step further and released CDK (Cloud Development Kit). AWS CDK is one of the most recent releases from Amazon AWS to improve the capability of creating and keeping control of infrastructures with your preferred programming language. Although this technology is quite recent, and may present issues while compared to SAM or even raw CloudFormation, it is undeniable that it eases the whole process of creating and setting up AWS resources. This article will present the advantages as well as some issues you might encounter while using this tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining AWS CDK
&lt;/h2&gt;

&lt;p&gt;In a nutshell, the AWS Cloud Development Kit is an open-source framework that aims to make an easier and faster way to create AWS Cloud resources that will be transformed into AWS CloudFormation templates. With CDK, the cloud infrastructure development can use all the perks provided by the desired programming languages, such as TypeScript, JavaScript, Python, Java and C#. This can drastically reduce the time spent on architecture, along with solid API reference and a very useful CLI that allows the interaction with CDK applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  CDK integrations and known issues
&lt;/h2&gt;

&lt;p&gt;One of the benefits that makes using AWS CDK so tempting is that although the tool itself has some complexities, an integration with a SAM project can be easily achieved. But, due to being a new framework, some of the cloud components are not very stable yet, which can be the case for APIs with Lambda functions because it can negatively affect the infrastructure scalability. In those cases, SAM templates, which are rock stable by now, can do just the trick, and those can be homogeneously incorporated into CDK applications.&lt;/p&gt;

&lt;p&gt;Another common issue is that as the framework is relatively new, updates are constantly being made on the packages, which may result in compatibility issues when installing CDK modules.&lt;/p&gt;

&lt;p&gt;The ability to overcome issues like those above described, as well as the fact that there are AWS resources that currently lack Constructs, such as Pinpoint and Athena, makes this framework a solid option for development teams facilitating the development of solid cloud architectures. As the tool is improved, it is expected that those issues will be solved, allowing an even faster and consistent development process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small learning curve
&lt;/h2&gt;

&lt;p&gt;Besides the programming language that is going to be used to create the cloud resources, which most of the time is already background knowledge, there are a few factors that make the learning curve relatively small. Not having to know all the basics and background information of each construct may be one of the biggest factors. When instantiating a resource, its basic roles and sets are already predefined, allowing the developer to focus only on elements they may need to customize. This allows developers that are starting in the cloud environment to acquire knowledge about the resources while still being productive and delivering projects. There may be times where the basics won’t be enough, but by the time the development process is complete, all the time previously saved by using the framework will be available to research in how to architect solutions for those problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Faster development process
&lt;/h2&gt;

&lt;p&gt;The development process is a balance of using the frameworks that will make the development process faster while maintaining its quality. Spending time creating enormous CloudFormation templates can be quite hard and time consuming, but it offers solid results and the cloud stacks created with it are solid. When SAM was launched, it made the developer’s life much easier, as its templates are quite smaller, while not losing its capabilities as it is transformed into CloudFormation templates.&lt;/p&gt;

&lt;p&gt;The same can be said for CDK: it brings the comfort of defining components programmatically while still having a compilation that allows the stack to have all the perks provided by CloudFormation, such as rollback in case of failures, safe deployment and drift detection.&lt;/p&gt;

&lt;p&gt;The usage of CDK constructs allows an acceleration of the development process, as the constructs come with predefined configurations for each AWS cloud resource they create, including its basic roles and properties.&lt;/p&gt;

&lt;p&gt;As an example of how the creation of cloud resources using CDK can speed up the development of cloud architectures, a definition of a bucket that allows get requests from specific domains is going to be demonstrated in both an AWS CloudFormation template and a AWS CDK application. This policy in a CloudFormation template should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"westpointBucket": {
  "Type": "AWS::S3::Bucket",
  "Properties": {
    "BucketName": "westpoint-bucket",
    "CorsConfiguration": {
      "CorsRules": [
        {
          "AllowedMethods": [
            "GET"
          ],
          "AllowedOrigins": [
            "http://westpoint.io/*"
          ]
        }
      ]
    },
    "WebsiteConfiguration": {
      "ErrorDocument": "index.html",
      "IndexDocument": "index.html"
    }
  },
  "UpdateReplacePolicy": "Delete",
  "DeletionPolicy": "Delete"
},
"westpointBucketPolicy": {
  "Type": "AWS::S3::BucketPolicy",
  "Properties": {
    "Bucket": {
      "Ref": "westpointBucket"
    },
    "PolicyDocument": {
      "Statement": [
        {
          "Action": "s3:GetObject",
          "Effect": "Allow",
          "Principal": "*",
          "Resource": {
            "Fn::Join": [
              "",
              [
                {
                  "Fn::GetAtt": [
                    "westpointBucket",
                    "Arn"
                  ]
                },
                "/*"
              ]
            ]
          }
        }
      ],
      "Version": "2012-10-17"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';const cors_rule: [s3.CorsRule] = [{
    allowedOrigins: ['http://westpoint.io/*'],
    allowedMethods: [s3.HttpMethods.GET]
}];

const westpointBucket = new s3.Bucket(this, 'westpoint-bucket', {
    bucketName: 'westpoint-bucket',
    cors: cors_rule,
    publicReadAccess: true,
    websiteIndexDocument: "index.html",
    websiteErrorDocument: "index.html",
    removalPolicy: cdk.RemovalPolicy.DESTROY
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As demonstrated above, defining this kind of resource on CDK applications is much faster and more compact compared to the same definition in CloudFormation. Although some issues may exist, as pointed out before, it is undeniable that this tool improves the productivity of the development teams, allowing them to focus more on what is important: the product itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constructs: the base for each application
&lt;/h2&gt;

&lt;p&gt;Constructs are the pieces that will come together when creating applications with AWS CDK. They are the representation of cloud components, such as a simple S3 bucket, or a DynamoDB table, as well as composite components, such as an AWS CodePipeline, which will be composed of multiple resources to accomplish its purpose.&lt;/p&gt;

&lt;p&gt;Defining resources using constructs in a CDK application is a quite simple task compared to what needs to be done on a CloudFormation template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as dynamodb from '@aws-cdk/aws-dynamodb';

const westpointTable = new dynamodb.Table(
    this, 
    'westpoint-table', { 
        partitionKey: {
            name: 'id', 
            type: dynamodb.AttributeType.STRING
        }, 
        tableName: 'WestpointTable'
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code snippet above represents the definition of an Amazon DynamoDB table using TypeScript. Each AWS resource has its own dependency, allowing the developers to import only what is needed for the application, reducing the size of the application by not importing packages that are not going to be used. As of the table itself, it accepts three attributes: scope, id and props.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scope: defines which construct the new construct is present within. When creating CDK stacks, each construct should be inside another construct, creating a hierarchy of constructs starting from Stacks all the way to smaller resources, such as a DynamoDB table. In almost every case, &lt;code&gt;this&lt;/code&gt; is going to be passed as the parameter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Id: defines the id of the construct, which must be unique within the scope. It is important to notice that the id does not represent the resource name, its purpose is to separate each cloud resource within the scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Props: an object that defines all the parameters that are going to be passed to customize the cloud resource. They are divided in two types: required and optional. Required props are the sets of parameters that the developers must set to create the desired cloud resource, while the optional props are non-mandatory parameters that can be passed to better suit the projects needs, usually overwriting predefined settings.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the previously defined DynamoDB table, which the id ‘westpoint-table’ was set, the set of properties passed to it were the partition key, which is a required parameter to the creation of the resource, and the table name, which optionally nominates the table that is going to be created in the AWS infrastructure. Not defining the table name would result in the creation of the resource with the default table name, which is a concatenation of all the constructs present within the current stack.&lt;/p&gt;

&lt;p&gt;CDK constructs are divided in three types: those being low-level constructs, higher-level constructs, and patterns. They represent multiple complexities of constructs that can be used in the cloud stack definition.&lt;/p&gt;

&lt;p&gt;Low-level constructs represent the smaller constructs, also called Cfn resources. They are all the resources that are available on CloudFormation templates, and require explicit declaration of all its properties, without any kind of basic resource creation. For its usage, deep knowledge about the AWS service is required, as the developer needs to specify all the models of the resource.&lt;/p&gt;

&lt;p&gt;Higher-level components also represent AWS services, but as an API that handles most of the configurations. They allow the development of cloud resources without deep knowledge about the resources themselves, as it bootstraps an instance of the service, and only requires basic user configuration to suit their needs.&lt;/p&gt;

&lt;p&gt;The last type, patterns, represent constructs that allow the simple creation of common tasks that otherwise would require some effort to be put together. They often consist of multiple AWS resources, such as the LambdaRestApi construct, which consists of a simple API Gateway resource that is backed up by a Lambda function.&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS CDK CLI
&lt;/h2&gt;

&lt;p&gt;Along with what was previously mentioned, AWS CDK has a CLI that is the main tool to interact with CDK applications. It is responsible for commands such as verifying the defined stacks, generating the CloudFormation templates and deploying your application to the AWS infrastructure.&lt;/p&gt;

&lt;p&gt;These perks allow the developers to validate their stacks much faster than it would be to deploy CloudFormation stacks to the cloud, in which case you would have to wait for the entire process to finish to then realize there was some kind of issue in the templates. As some commands are vital for the usage of CDK, they are going to be presented here to show an overview of what the CLI is capable of.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The basic command to quick start CDK projects. It generates a folder with the necessary files and dependencies to create a CDK application. It comes with options to select the language, list available templates to start your stack and possibility to auto setup a git repository for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk synthesize
cdk synth
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command is responsible for generating the CloudFormation stack that will be created when deploying your CDK application. It can be used to verify if the current template definition in your code represents a valid AWS CloudFormation template, reducing the time needed to discover issues. It comes with an option to synthesize only the requested stacks, leaving behind all their dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk deploy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the most important command present in the CLI. It allows the deployment of your application into the AWS cloud environment. Running this command will result in the creation of the CloudFormation templates for the defined stacks present in the code, and then uploading those templates in the cloud. It comes with multiple options, such as passing parameters to the stack, selecting the output file path, tags that may be added to the stack, and more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk bootstrap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There might be situations where the template created by the deploy command can’t be directly uploaded to the cloud due to its size. When this happens, the bootstrap of your application is required. It consists of uploading the generated templates into AWS buckets for them to be deployed again using the previous command. This command comes with many options, such as selecting the bucket the templates will be uploaded to, selecting the AWS KMS master key for encryption, as well as adding tags to the stack, and more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk destroy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command is used to remove deployed CDK stacks. It removes from the cloud all the resources created by your CDK application, besides some special cases. One of those cases is S3 buckets: the destroy command will never delete a bucket that is not empty, which most of the time it will not be, requiring manual deletion after the destroy process is finished. It comes with options to not destroy dependencies and force exclusions, not requiring confirmations afterwards.&lt;/p&gt;

&lt;p&gt;As we can see, the AWS CDK CLI is one of the biggest friends of the developer when defining cloud resource stacks, having its multiple perks that facilitate the process of creating and deploying AWS infrastructures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As described in this article, AWS CDK and its CLI facilitate the development of AWS infrastructures, as it allows developers to use programming languages rather than using notation languages such as YAML or JSON. The creation and deployment of each and every stack is a lot easier when making usage of elements such as the synthesize command to validate stacks. Following this article I will write a complimentary article tackling the most common issues of using CDK and how to solve them, allowing a quick understanding of some of the issues that may occur and allowing an even easier and faster development. I hope you found this article useful.&lt;/p&gt;

&lt;p&gt;Mark Avdi&lt;/p&gt;

&lt;p&gt;CTO | &lt;a href="https://westpoint.io"&gt;WestPoint.io&lt;/a&gt; | Lover of all things Serverless&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
