AWS + Cloudformation + Elasticbeanstalk

时间:2018-08-20 17:53:09

标签: amazon-web-services java-8 elastic-beanstalk amazon-cloudformation tomcat8

当我使用输入参数EnvironmentType“ dev”使用以下cloudformation模板创建堆栈时,它将创建ebs应用程序,在应用程序内部创建环境,并从S3存储桶中部署sample-app.war文件。 / p>

然后,我使用具有输入参数EnvironmentType“ stage”的相同模板进行更新堆栈,这一次它删除了现有的dev环境并在应用程序内部创建了stage环境。

我还尝试使用示例模板再次创建堆栈,输入第一步中创建的示例应用程序名称,这一次它显示应用程序已存在。

我的要求是保留开发环境和舞台环境,以便使用cloudformation在示例应用程序内部创建

请提出任何建议。

---
AWSTemplateFormatVersion: 2010-09-09

Description: 'Create an ElasticBeanstalk Application, Environment and deploy the war file from S3 bucket'

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      -
        Label:
          default: 'EBS Application Configuration'
        Parameters:
          - ApplicationName
          - ApplicationDescription
          - ApplicationVersion
      -
        Label:
          default: 'EBS Environment Configuration'
        Parameters:
          - EnvironmentName
          - EnvironmentType
          - EnvironmentDescription
          - EnvironmentCName
          - MinInstances
          - MaxInstances

Mappings:
  PropertiesMap:
    IntanceType:
      dev: 'SingleInstance'
      qa: 'SingleInstance'
      stage: 'LoadBalanced'
      prod: 'LoadBalanced'

Parameters:
  ApplicationName:
    Type: String
    Description: 'Name of the ElasticBeanstalk Application'

  ApplicationDescription:
    Type: String
    Description: 'ElasticBeanstalk Application Description'

  ApplicationVersion:
    Type: String
    Description: 'Application version description'

  EnvironmentName:
    Type: String
    Description: 'Name of the Environment'
    AllowedPattern: '^([A-Za-z]|[0-9]|-)+$'

  EnvironmentType:
    Type: String
    Description: 'Type of the Environment (dev, qa, stage, prod)'
    AllowedValues:
      - 'dev'
      - 'qa'
      - 'stage'
      - 'prod'

  EnvironmentCName:
    Type: String
    Description: 'CName Prefix for the ElasticBeanstalk environment'
    AllowedPattern: '^([A-Za-z]|[0-9]|-)+$'

  EnvironmentDescription:
    Type: String
    Description: 'Description of the ElasticBeanstalk environment'

  MinInstances:
    Type: Number
    Description: 'Minimum load balanced instances (Mandatory for stage/prod)'
    Default: 2
    MinValue: 2
    MaxValue: 10

  MaxInstances:
    Type: Number
    Description: 'Maximum load balanced instances (Mandatory for stage/prod)'
    Default: 2
    MinValue: 2
    MaxValue: 10

Conditions:
  IsStageOrProdEnvironment:
    !Or [!Equals [stage, !Ref EnvironmentType], !Equals [prod, !Ref EnvironmentType]]

Resources:
  EBSApplication:
    Type: AWS::ElasticBeanstalk::Application
    Properties:
      ApplicationName: !Ref ApplicationName
      Description: !Ref ApplicationDescription

  EBSApplicationVersion:
    Type: AWS::ElasticBeanstalk::ApplicationVersion  
    Properties:
      ApplicationName: !Ref EBSApplication
      Description: !Ref ApplicationVersion

      SourceBundle:
        S3Bucket: deployable
        S3Key: artifacts/sample-app.war

  EBSApplicationConfigurationTemplate:
    Type: AWS::ElasticBeanstalk::ConfigurationTemplate
    Properties:
      ApplicationName: !Ref EBSApplication
      Description: 'ElasticBeanstalk Configuration Template'
      SolutionStackName: '64bit Amazon Linux 2018.03 v3.0.2 running Tomcat 8.5 Java 8'
      OptionSettings:
        - Namespace: aws:elasticbeanstalk:environment
          OptionName: EnvironmentType
          Value: !FindInMap [PropertiesMap, IntanceType, !Ref EnvironmentType]
        - Namespace: aws:autoscaling:asg
          OptionName: MinSize
          Value: !If [IsStageOrProdEnvironment, !Ref MinInstances, !Ref 'AWS::NoValue']
        - Namespace: aws:autoscaling:asg
          OptionName: MaxSize
          Value: !If [IsStageOrProdEnvironment, !Ref MaxInstances, !Ref 'AWS::NoValue']

  EBSEnvironment:
    Type: AWS::ElasticBeanstalk::Environment
    Properties:
      ApplicationName: !Ref EBSApplication
      CNAMEPrefix: !Ref EnvironmentCName
      Description: !Ref EnvironmentDescription
      EnvironmentName: !Ref EnvironmentName
      TemplateName: !Ref EBSApplicationConfigurationTemplate
      VersionLabel: !Ref EBSApplicationVersion

Outputs:
  ApplicationURL:
    Description: 'ElasticBeanstalk environment endpoint'
    Value: !Join 
      - ''
      - - 'http://'
        - !GetAtt EBSEnvironment.EndpointURL

0 个答案:

没有答案