Next, we will create the Elastic Beanstalk application, application version, and environment so that we can deploy the web app that we just uploaded to S3 using S3 Assets.
import * as elasticbeanstalk from 'aws-cdk-lib/aws-elasticbeanstalk';
const appName = 'MyWebApp';
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
applicationName: appName,
});
Now we need to create an application version from the S3 asset that we created earlier.
// Create an app version from the S3 asset defined earlier
const appVersionProps = new elasticbeanstalk.CfnApplicationVersion(this, 'AppVersion', {
applicationName: appName,
sourceBundle: {
s3Bucket: webAppZipArchive.s3BucketName,
s3Key: webAppZipArchive.s3ObjectKey,
},
});
// Make sure that Elastic Beanstalk app exists before creating an app version
appVersionProps.addDependency(app);
To create the Elastic Beanstalk environment, we will need to provide an existing instance profile name to pass role information to an Amazon EC2 instance when the instance starts.
import * as iam from 'aws-cdk-lib/aws-iam';
// Create role and instance profile
const myRole = new iam.Role(this, `${appName}-aws-elasticbeanstalk-ec2-role`, {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});
const managedPolicy = iam.ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier')
myRole.addManagedPolicy(managedPolicy);
const myProfileName = `${appName}-InstanceProfile`
const instanceProfile = new iam.CfnInstanceProfile(this, myProfileName, {
instanceProfileName: myProfileName,
roles: [
myRole.roleName
]
});
The last part we need to create is the Elastic Beanstalk environment. The environment is a collection of AWS resources running an application version. For the environment, we will need to give some information about the infrastructure.
// Example of some options which can be configured
const optionSettingProperties: elasticbeanstalk.CfnEnvironment.OptionSettingProperty[] = [
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'IamInstanceProfile',
value: myProfileName,
},
{
namespace: 'aws:autoscaling:asg',
optionName: 'MinSize',
value: props?.maxSize ?? '1',
},
{
namespace: 'aws:autoscaling:asg',
optionName: 'MaxSize',
value: props?.maxSize ?? '1',
},
{
namespace: 'aws:ec2:instances',
optionName: 'InstanceTypes',
value: props?.instanceTypes ?? 't2.micro',
},
];
// Create an Elastic Beanstalk environment to run the application
const elbEnv = new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
environmentName: props?.envName ?? "MyWebAppEnvironment",
applicationName: app.applicationName || appName,
solutionStackName: 'SOLUTION-STACK-NAME',
optionSettings: optionSettingProperties,
versionLabel: appVersionProps.ref,
});
64bit Amazon Linux 2023 v6.0.2 running Node.js 18
.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
// Add import statements here
import * as s3assets from 'aws-cdk-lib/aws-s3-assets';
import * as elasticbeanstalk from 'aws-cdk-lib/aws-elasticbeanstalk';
import * as iam from 'aws-cdk-lib/aws-iam';
export interface EBEnvProps extends cdk.StackProps {
// Autoscaling group configuration
minSize?: string;
maxSize?: string;
instanceTypes?: string;
envName?: string;
}
export class EBApplnStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: EBEnvProps) {
super(scope, id, props);
// The code that defines your stack goes here
// Construct an S3 asset Zip from directory up.
const webAppZipArchive = new s3assets.Asset(this, 'WebAppZip', {
path: `${__dirname}/../src`,
});
const appName = 'MyWebApp';
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
applicationName: appName,
});
// Create an app version from the S3 asset defined earlier
const appVersionProps = new elasticbeanstalk.CfnApplicationVersion(this, 'AppVersion', {
applicationName: appName,
sourceBundle: {
s3Bucket: webAppZipArchive.s3BucketName,
s3Key: webAppZipArchive.s3ObjectKey,
},
});
// Make sure that Elastic Beanstalk app exists before creating an app version
appVersionProps.addDependency(app);
// Create role and instance profile
const myRole = new iam.Role(this, `${appName}-aws-elasticbeanstalk-ec2-role`, {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});
const managedPolicy = iam.ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier')
myRole.addManagedPolicy(managedPolicy);
const myProfileName = `${appName}-InstanceProfile`
const instanceProfile = new iam.CfnInstanceProfile(this, myProfileName, {
instanceProfileName: myProfileName,
roles: [
myRole.roleName
]
});
// Example of some options which can be configured
const optionSettingProperties: elasticbeanstalk.CfnEnvironment.OptionSettingProperty[] = [
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'IamInstanceProfile',
value: myProfileName,
},
{
namespace: 'aws:autoscaling:asg',
optionName: 'MinSize',
value: props?.maxSize ?? '1',
},
{
namespace: 'aws:autoscaling:asg',
optionName: 'MaxSize',
value: props?.maxSize ?? '1',
},
{
namespace: 'aws:ec2:instances',
optionName: 'InstanceTypes',
value: props?.instanceTypes ?? 't2.micro',
},
];
// Create an Elastic Beanstalk environment to run the application
const elbEnv = new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
environmentName: props?.envName ?? "MyWebAppEnvironment",
applicationName: app.applicationName || appName,
solutionStackName: '64bit Amazon Linux 2023 v6.0.2 running Node.js 18',
optionSettings: optionSettingProperties,
versionLabel: appVersionProps.ref,
});
}
}