/aws-cdk-infra-ElasticBeanstalk

In this learning project, I use AWS CDK to deploy a NodeJs poemApplication to AWS ElasticBeanStalk.

Primary LanguageTypeScript

Using AWS CDK infra to deploy a Node Application to AWS Elastic BeanStalk

In this small project we learn how to write AWS CDK infra code using the version 2.72.1 to deploy a Node.js Application to Aws Elastic Beanstalk with TypeScript.

Useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template
  • The cdk.json file tells the CDK Toolkit how to execute your app.

Process to build goes like this:

Install dependencies

    sudo npm install -g aws-cdk  ## Install aws cdk tool if not installed earlier
    cdk --version

Initialize our app

    mkdir  aws-cdk-infra-ElasticBeanstalk && cd aws-cdk-infra-ElasticBeanstalk 
    cdk init app --language typescript    

Writing the Infrastructure Code

  • All our code will be defined in the file aws-cdk-infra-ElasticBeanstalk/lib/aws-cdk-infra-elastic_beanstalk-stack.ts .

Checking Stack and Resources on AWS before deployment

  • We can see below that our ELb stak is not yet deployed.

image

  • Also we have an empty S3 bucket

image

  • Same for AWS Elastic Beanstalk

image

Exceuting commands for deployment

  • Bootstraping our AWS Account for correct configuration
    aws sts get-caller-identity
    aws configure get region
    aws cloudformation delete-stack --stack-name CDKToolkit ## to erase old stack if present
    cdk bootstrap aws://ACCOUNT-NUMBER-1/REGION-1 --force # to clean previous configuration
  • Checking the Differences changes
    cdk diff
  • Output of the CD:

image

  • After we have packaged our Node.js application, and bootstrapped our AWS account and Region, we are ready to build and deploy the CDK application.
  • The first step is to build the CDK application.
    npm run build
  • If there are no errors in our application, this will succeed. We can now deploy the CDK application in the cloud.
    cdk deploy

cdkDEPLOY-elb

  • Cleaning our environment by desstroying everything and removing
    cdk destroy

cdkDESTROY