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 jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk 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.
- Also we have an empty S3 bucket
- Same for AWS Elastic Beanstalk
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:
- 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
- Cleaning our environment by desstroying everything and removing
cdk destroy