Bobcares

AWS CDK Elastic Beanstalk | Deployment Steps

by | Nov 5, 2022

Using AWS CDK, an Elastic Beanstalk app can be deployed easily. Bobcares, as a part of our AWS Support Services, offers solutions to every query that comes our way.

Elastic Beanstalk Deployment Using AWS CDK

Elastic Beanstalk is a simple web app deployment tool. It supports many web app frameworks. We can use the EB CLI to deploy NodeJS, Django, and many more apps.

 

aws cdk elastic beanstalk

 

Prerequisites: AWS CDK, EB CLI, and AWS CLI.

The deployment of AWS EB using AWS CDK includes the below steps:

CDK Project Initialization

We must run the below code in a new directory:

cdk init --app myApp --language typescript

CDK has folders such as lib, bin, and test. During the deployment, we’ll create all of the stacks in the lib folder and write the tests in the test folder. Then initialize the stacks for deployment in the bin folder.

CDK Libraries Installation

In AWS CDK, we can just include only what’s needed for our project. Here, we are using the IAM and EB libraries. We can install it using the below code:

npm install @aws-cdk/aws-elasticbeanstalk @aws-cdk/aws-iam

Stack Creation

A stack is a set of DBs, servers, containers, etc. The stack does not contain any resources; rather, it is a collection of resource settings. When we deploy the stack, AWS creates the resources based on the settings.

We require three resources to start the app, namely, an IAM role and an Instance Profile to give EB the permissions to manage our server, an EB Environment, and an EB App.

We can use the below commands for the IAM permissions:

// EBS IAM Roles
const EbInstanceRole = new iam.Role(this, `${appName}-aws-elasticbeanstalk-ec2-role`, {
  assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});

const managedPolicy = iam.ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier')
EbInstanceRole.addManagedPolicy(managedPolicy);

const profileName = `${appName}-InstanceProfile`
const instanceProfile = new iam.CfnInstanceProfile(this, profileName, {
  instanceProfileName: profileName,
  roles: [
    EbInstanceRole.roleName
  ]
});

This builds an IAM role with the AWSElasticBeanstalkWebTier managed IAM policy attached. Then we make the instance profile and apply for the newly formed role. This grants the server the necessary rights to function as an EB server.

We can use the below commands to create an EB app and environment:

const node = this.node;
const platform = node.tryGetContext("platform");

const optionSettingProperties: elasticbeanstalk.CfnEnvironment.OptionSettingProperty[] = [
  {
    namespace: 'aws:autoscaling:launchconfiguration',
    optionName: 'InstanceType',
    value: 't3.small',
  },
  {
    namespace: 'aws:autoscaling:launchconfiguration',
    optionName: 'IamInstanceProfile',
    value: profileName
  }
];

// EBS Application and Environment
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
  applicationName: `${appName}-EB-App`
});

const env = new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
  environmentName: `${appName}-EB-Env`,
  applicationName: `${appName}-EB-App`,
  platformArn: platform,
  solutionStackName: '64bit Amazon Linux 2 v5.3.0 running Node.js 14',
  optionSettings: optionSettingProperties
});

env.addDependsOn(app);

Stack Deployment

  • Firstly, we must execute the synth code to ensure that there are no issues translating our typescript code into CloudFormation templates.
  • Then call the bootstrap code to get the CDK to start our stack in AWS. Now we see it is created in CloudFormation called “CDK Toolkit”.
  • Finally, deploy the stack.
    cdk synth
    cdk bootstrap
    cdk deploy

     

  • When the stack finishes, we should be able to see the EB Environment and Application in the EB console. We can also see the server status as “healthy.”

Server Deployment

We must first start our project with EB before we can deploy our code to the newly built server.

eb init

If we use the same profile that we used to deploy the CDK stack, the EB environment should appear as a selectable option in the console. We must then choose an environment and wait for the project to start. After that, it should be as simple as:

eb deploy

Debugging

If we’re having problems with the deployment, we can examine the most current logs or SSH into the server using the below codes:

eb logs
eb ssh

Testing

In the AWS interface, we should now find the URL for our app. From here, we may expand the stack by adding databases, S3 buckets, or networks. We may keep adding resources to the stack or split it up into several stacks for modularity.

[Need help with another AWS issue? We’re available 24/7.]

Conclusion

To conclude, we have included detailed steps from our Support team for the deployment of Elastic Beanstalk using AWS CDK in this article.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.