0

我指的是这个文档: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 而言:我们的实例需要一个SingleInstance类型dev,并且我们希望LoadBalanced我们的实例有一个类型prod(使用两个 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 '${ProjectId}app${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"
4

1 回答 1

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

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

于 2020-02-07T15:41:50.327 回答