I have a CDK app with a Stack class that creates an AWS Elastic Beanstalk App.
When there is a new app version I want to create, I want the Stack to create a new AppVersion on the Beanstalk Environment without deleting the already existing AppVersion(s).
With my current setup, everytime I run cdk deploy
, the Stack creates the new AppVersion resource and Deletes the existing AppVersion.
Here's my Stack code:
class ProdStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
// Construct an S3 asset from the ZIP located from directory up.
const webAppZipArchive = new s3assets.Asset(
this,
`${process.env.STACK_NAME}`,
getBuildZipConfig()
);
// EBS IAM Roles
const EbInstanceRole = new iam.Role(this, `my-aws-elasticbeanstalk-ec2-role`, {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});
const managedPolicy = iam.ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier');
EbInstanceRole.addManagedPolicy(managedPolicy);
const profileName = `${id}-InstanceProfile`;
const instanceProfile = new iam.CfnInstanceProfile(this, profileName, {
instanceProfileName: profileName,
roles: [EbInstanceRole.roleName],
});
const node = this.node;
const platform = node.tryGetContext('platform');
const APPLICATION_NAME = id;
const ENVIRONMENT_NAME = `${id}-env`;
const APP_VERSION = process.env.COMMIT_HASH;
// Create the EB application
const app = new elasticbeanstalk.CfnApplication(this, 'Application', {
applicationName: APPLICATION_NAME,
});
// Create an app version from the S3 asset defined earlier
const appVersionProps = new elasticbeanstalk.CfnApplicationVersion(this, APP_VERSION, {
applicationName: APPLICATION_NAME,
sourceBundle: {
s3Bucket: webAppZipArchive.s3BucketName,
s3Key: webAppZipArchive.s3ObjectKey,
},
description: APP_VERSION,
});
appVersionProps.addDependsOn(app);
// Create EB environment
let optionSettingProperties = [
{
namespace: 'aws:ec2:instances',
optionName: 'InstanceTypes',
value: 'c5.large',
},
{
namespace: 'aws:autoscaling:launchconfiguration',
optionName: 'IamInstanceProfile',
value: profileName,
},
{
namespace: 'aws:elasticbeanstalk:command',
optionName: 'DeploymentPolicy',
value: 'Immutable',
},
];
optionSettingProperties = [...optionSettingProperties, ...this.getEnvConfig()];
const env = new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
environmentName: ENVIRONMENT_NAME,
applicationName: APPLICATION_NAME,
platformArn: platform,
solutionStackName: '64bit Amazon Linux 2 v5.4.9 running Node.js 14',
optionSettings: optionSettingProperties,
cnamePrefix: process.env.CNAME_PREFIX,
});
env.addDependsOn(appVersionProps);
}
Any help will be greatly appreciated.
Thank you!