如何覆盖我的 ElasticBeanstalk 应用程序的负载均衡器和容量等配置

Posted

技术标签:

【中文标题】如何覆盖我的 ElasticBeanstalk 应用程序的负载均衡器和容量等配置【英文标题】:How can I override the configuration such as Load Balancer and Capacity for my ElasticBeanstalk application 【发布时间】:2020-11-01 04:59:58 【问题描述】:

我正在尝试使用 CloudFormation 在 ElasticBeanstalk 上部署示例 php 应用程序,以尝试学习 CloudFormation。

以下是我的模板。

AWSTemplateFormatVersion: '2010-09-09'
Description: AWS CloudFormation Sample PHP Application on ElasticBeanstalk Template
Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the AWS Elastic
      Beanstalk instance
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription: must be the name of an existing EC2 KeyPair.
Mappings:
  Region2Principal:
    us-east-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    us-west-2:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    us-west-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    eu-west-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    eu-west-2:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    eu-west-3:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ap-southeast-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ap-northeast-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ap-northeast-2:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ap-northeast-3:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ap-southeast-2:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ap-south-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    us-east-2:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    ca-central-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    sa-east-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    cn-north-1:
      EC2Principal: ec2.amazonaws.com.cn
      OpsWorksPrincipal: opsworks.amazonaws.com.cn
    cn-northwest-1:
      EC2Principal: ec2.amazonaws.com.cn
      OpsWorksPrincipal: opsworks.amazonaws.com.cn
    eu-central-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
    eu-north-1:
      EC2Principal: ec2.amazonaws.com
      OpsWorksPrincipal: opsworks.amazonaws.com
Resources:
  WebServerRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - Fn::FindInMap:
                    - Region2Principal
                    - Ref: AWS::Region
                    - EC2Principal
            Action:
              - sts:AssumeRole
      Path: /
  WebServerRolePolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: WebServerRole
      PolicyDocument:
        Statement:
          - Effect: Allow
            NotAction: iam:*
            Resource: '*'
      Roles:
        - Ref: WebServerRole
  WebServerInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Path: /
      Roles:
        - Ref: WebServerRole
  SampleApplication:
    Type: AWS::ElasticBeanstalk::Application
    Properties:
      Description: AWS Elastic Beanstalk Sample PHP Application
  SampleApplicationVersion:
    Type: AWS::ElasticBeanstalk::ApplicationVersion
    Properties:
      Description: Version 1.0
      ApplicationName:
        Ref: SampleApplication
      SourceBundle:
        S3Bucket:
          Fn::Join:
            - '-'
            - - elasticbeanstalk-samples
              - Ref: AWS::Region
        S3Key: php-sample.zip
  SampleConfigurationTemplate:
    Type: AWS::ElasticBeanstalk::ConfigurationTemplate
    Properties:
      ApplicationName:
        Ref: SampleApplication
      Description: SSH access to PHP Application
      SolutionStackName: 64bit Amazon Linux 2 v3.0.3 running PHP 7.3
      OptionSettings:
        - Namespace: aws:autoscaling:launchconfiguration
          OptionName: EC2KeyName
          Value:
            Ref: KeyName
        - Namespace: aws:autoscaling:launchconfiguration
          OptionName: IamInstanceProfile
          Value:
            Ref: WebServerInstanceProfile
  SampleEnvironment:
    Type: AWS::ElasticBeanstalk::Environment
    Properties:
      Description: AWS Elastic Beanstalk Environment running Sample PHP Application
      ApplicationName:
        Ref: SampleApplication
      EnvironmentName: LaravelAppTesting
      TemplateName:
        Ref: SampleConfigurationTemplate
      VersionLabel:
        Ref: SampleApplicationVersion
Outputs:
  URL:
    Description: URL of the AWS Elastic Beanstalk Environment
    Value:
      Fn::Join:
        - ''
        - - http://
          - Fn::GetAtt:
              - SampleEnvironment
              - EndpointURL

如下所示,ElasticBeanstalk 具有负载均衡器、容量等配置。现在,我正在尝试覆盖 Beanstalk 的负载均衡器设置。我该怎么做?

【问题讨论】:

【参考方案1】:

可以通过将OptionSettings 添加到 CloudFormation 中的 SampleEnvironment 资源来修改某些属性。这些选项的列表是可用的here。

对于其他情况,您应该在部署的应用程序中使用ebextensions 查看configuring the environment。

【讨论】:

嗨@Chris,再次感谢您的回答。但是我仍然不太清楚如何使用它们,OptionSettings。我检查了可用选项列表。例如,aws:autoscaling:launchconfiguration。该命名空间有什么作用?我们可以定义选项 IamInstanceProfile。那是什么,我在哪里可以看到文档?我们可以为这些选项设置什么值?你能解释一下并指出正确的方向吗? 文档仅限于在答案的链接中列出选项,这些选项中的大多数都需要进行一些研究,例如 IamInstanceProfile 是docs.aws.amazon.com/IAM/latest/UserGuide/…。这些值很可能与其对应的 CloudFormation 资源匹配,即docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/… 嗨@Chris Williams。非常感谢。文档应该在这方面更有条理。我想使用 CloudFormation 的原因是我希望基础架构更有条理并节省时间。这项研究还需要一位经验丰富的开发人员。似乎它增加了比预期更多的额外工作量来享受水果。再次感谢。

以上是关于如何覆盖我的 ElasticBeanstalk 应用程序的负载均衡器和容量等配置的主要内容,如果未能解决你的问题,请参考以下文章

使用 ALB 创建 ElasticBeanstalk 应用程序时覆盖 HealthCheckPort

gradle-aws-plugin elasticbeanstalk 如何部署上传的版本

如何将我的 DNS 指向 AWS ElasticBeanstalk 实例?

如何将 Visual Studio Flask 应用程序部署到 Elastic Beanstalk

如何自定义我的按钮?

如何在 AWS ElasticBeanstalk 中修改 apache VirtualHost