从 CodeStar 项目自定义 Prod 部署

Posted

技术标签:

【中文标题】从 CodeStar 项目自定义 Prod 部署【英文标题】:Customizing a Prod deployment from a CodeStar project 【发布时间】:2020-05-23 01:35:40 【问题描述】:

我指的是这个文档:https://docs.aws.amazon.com/codestar/latest/userguide/customize-ec2-multi-endpoints.html#customize-ec2-multi-endpoints-newstage

我们目前有一个从“Java Spring,Web Service”类型的 Beanstalk 模板创建的 CodeStar 项目。然而,使用 CloudFormation 生成的项目仅包含一个用于部署我们的 API 的环境。

问题:如何正确修改此模板以使两个不同的环境同时运行?

就 Beanstalk 而言:我们的 dev 实例需要一个 SingleInstance 类型,我们希望 prod 实例有一个 LoadBalanced 类型(使用两个 EC2 实例来实现冗余)。

引用的教程提到 awscodestar-<project_name>-infrastructure-prod 被用作 CloudFormation 的集合的堆栈名称,但是如果没有创建要部署的实际实例,这将如何工作?我很困惑。

为什么还没有一个模板文件来提供类似于所描述的两种环境的 CodePipeline/Beanstalk 设置?这似乎是一种相当主流的 CI/CD 方法。


这是我们的template.yml 文件:

AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::CodeStar

Conditions:
  UseSubnet: !Not [!Equals [!Ref 'SubnetId', subnet-none]]

Parameters:
  ProjectId:
    Type: String
    Description: AWS CodeStar project ID used to name project resources and create roles.
  InstanceType:
    Type: String
    Description: The type of Amazon EC2 Linux instances that will be launched for this project.
  KeyPairName:
    Type: String
    Description: The name of an existing Amazon EC2 key pair in the region where the project is created, which you can use to SSH into the new Amazon EC2 Linux instances.
  VpcId:
    Type: String
    Description: The ID of the Amazon Virtual Private Cloud (VPC) used for the new Amazon EC2 Linux instances.
  SubnetId:
    Type: String
    Description: The name of the VPC subnet used for the new Amazon EC2 Linux instances launched for this project.
  SolutionStackName:
    Type: String
    Description: The software stack used to launch environments and configure instances in AWS Elastic Beanstalk.
  EBTrustRole:
    Type: String
    Description: The service role in IAM for AWS Elastic Beanstalk to be created for this project.
  EBInstanceProfile:
    Type: String
    Description: The IAM role that will be created for the Amazon EC2 Linux instances.
  Stage:
    Type: String
    Description: The name for a project pipeline stage, such as Staging or Prod, for which resources are provisioned and deployed.
    Default: ''
Resources:
  EBApplication:
    Description: The AWS Elastic Beanstalk application, which is a container used to deploy the correct application configuration.
    Type: AWS::ElasticBeanstalk::Application
    Properties:
      ApplicationName: !Sub '$ProjectIdapp$Stage'
      Description: The name of the AWS Elastic Beanstalk application to be created for this project.
  EBApplicationVersion:
    Description: The version of the AWS Elastic Beanstalk application to be created for this project.
    Type: AWS::ElasticBeanstalk::ApplicationVersion
    Properties:
      ApplicationName: !Ref 'EBApplication'
      Description: The application version number.
      SourceBundle: 'target/ROOT'
  EBConfigurationTemplate:
    Description: The AWS Elastic Beanstalk configuration template to be created for this project, which defines configuration settings used to deploy different versions of an application.
    Type: AWS::ElasticBeanstalk::ConfigurationTemplate
    Properties:
      ApplicationName: !Ref 'EBApplication'
      Description: The name of the sample configuration template.
      OptionSettings:
      - Namespace: aws:elasticbeanstalk:environment
        OptionName: EnvironmentType
        Value: SingleInstance
      - Namespace: aws:elasticbeanstalk:environment
        OptionName: ServiceRole
        Value: !Ref 'EBTrustRole'
      - Namespace: aws:elasticbeanstalk:healthreporting:system
        OptionName: SystemType
        Value: enhanced
      SolutionStackName: !Ref 'SolutionStackName'
  EBEnvironment:
    Description: The AWS Elastic Beanstalk deployment group where the application is deployed, which is made up of the Amazon EC2 Linux instances launched for this project.
    Type: AWS::ElasticBeanstalk::Environment
    Properties:
      ApplicationName: !Ref 'EBApplication'
      EnvironmentName: !Ref 'EBApplication'
      Description: The application to be deployed to the environment.
      TemplateName: !Ref 'EBConfigurationTemplate'
      VersionLabel: !Ref 'EBApplicationVersion'
      OptionSettings:
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: IamInstanceProfile
        Value: !Ref 'EBInstanceProfile'
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: InstanceType
        Value: !Ref 'InstanceType'
      - Namespace: aws:autoscaling:launchconfiguration
        OptionName: EC2KeyName
        Value: !Ref 'KeyPairName'
      - Namespace: aws:ec2:vpc
        OptionName: VPCId
        Value: !Ref 'VpcId'
      - !If
        - UseSubnet
        - Namespace: 'aws:ec2:vpc'
          OptionName: Subnets
          Value: !Ref 'SubnetId'
        - !Ref "AWS::NoValue"

【问题讨论】:

【参考方案1】:

您可以创建一个主模板并将生产和开发模板称为嵌套堆栈。在这种情况下,您只需将 template.yaml 文件复制两次,将副本重命名为适当的名称,然后将它们上传到 S3 存储桶。然后您的 template.yaml 文件将引用这两个模板。像这样的:

AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::CodeStar

Description: Master stack which creates all required nested stacks

Resources:
  ProdStack:
    Type: "AWS::CloudFormation::Stack"
    Properties:
      TemplateURL: https://your-bucket/templates/production-stack.yml"
      Parameters:
        InstanceType: t2.micro
        EBInstanceProfile: eg
        KeyPairName: eg
        VpcId: eg
        ProjectId: eg
        SubnetId: eg
        SolutionStackName: eg
        EBTrustRole: eg
      Tags:
        - Key: Environment
          Value: Production
  DevStack:
    Type: "AWS::CloudFormation::Stack"
    Properties:
      TemplateURL: https://your-bucket/templates/development-stack.yml"
      Parameters:
        InstanceType: t2.nano
        EBInstanceProfile: eg
        KeyPairName: eg
        VpcId: eg
        ProjectId: eg
        SubnetId: eg
        SolutionStackName: eg
        EBTrustRole: eg
      Tags:
        - Key: Environment
          Value: Dev

这将创建两个堆栈并允许您为每个堆栈输入适当的参数。看这里:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stack.html

您的管道可能会保留在主文件中,以及两种环境所需的任何其他参数,具体取决于您如何构建项目。

【讨论】:

例如,这最终会生成两个 CodeCommit 存储库和两个 CodePipeline 管道吗? 您可能不希望针对不同环境使用单独的存储库。您可以从存储库中的不同分支触发不同的管道。此示例仅对 Instance 类型进行更改。 有趣。当我有时间时,我会研究那个解决方案。那时,我们曾试图将某些东西“粘合”在一起,但我们仍然遇到问题。也请随意查看:***.com/q/60460005/9768291

以上是关于从 CodeStar 项目自定义 Prod 部署的主要内容,如果未能解决你的问题,请参考以下文章

AWS CodeStar

springboot入门

部署项目 - 自定义操作更新

Bamboo 自定义部署-项目变量

使用 QtCreator 部署自定义构建项目

.NET 部署项目:取消卸载程序的自定义操作