繁体   English   中英

为什么 Elastic Beanstalk 的 CloudFormation 堆栈 (aws-cdk) 会删除以前的 AppVersion?

[英]Why does CloudFormation stack (aws-cdk) for Elastic Beanstalk delete previous AppVersion?

我有一个带有 Stack class 的 CDK 应用程序,它创建了一个 AWS Elastic Beanstalk 应用程序。

当我想创建一个新的应用程序版本时,我希望 Stack 在 Beanstalk 环境上创建一个新的 AppVersion,而不删除已经存在的 AppVersion。

使用我当前的设置,每次我运行cdk deploy时,Stack 都会创建新的 AppVersion 资源并删除现有的 AppVersion。

这是我的堆栈代码:

 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);
}

任何帮助将不胜感激。

谢谢!

您的堆栈有一个CfnApplicationVersion构造。 如果您想要更明确定义的版本实例,可以 (a) 添加更多CfnApplicationVersion构造到您的 Stack 或 (b) 添加更多ProdStack实例到您的 App。

Stack 是 CDK/CloudFormation 部署单元。 在环境(= 区域 + 帐户)中第一次运行cdk deploy会创建 Stack 的资源。 随后的cdk deploy命令更新或替换现有资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM