Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services CloudBeanstalk为共享负载平衡器指定目标组_Amazon Web Services_Amazon Elastic Beanstalk_Amazon Cloudformation_Aws Application Load Balancer - Fatal编程技术网

Amazon web services CloudBeanstalk为共享负载平衡器指定目标组

Amazon web services CloudBeanstalk为共享负载平衡器指定目标组,amazon-web-services,amazon-elastic-beanstalk,amazon-cloudformation,aws-application-load-balancer,Amazon Web Services,Amazon Elastic Beanstalk,Amazon Cloudformation,Aws Application Load Balancer,我有两个模板 LambdaBasicExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:Assume

我有两个模板

  LambdaBasicExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSCloudFormationReadOnlyAccess
        - arn:aws:iam::aws:policy/AWSElasticBeanstalkReadOnly
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  GetEBLBTargetGroupLambda:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Description: 'Get ARN of EB Load balancer'
      Timeout: 30
      Role: !GetAtt 'LambdaBasicExecutionRole.Arn'
      Runtime: nodejs12.x
      Code:
        ZipFile: |
          ... code ...
  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Priority: 1
      ListenerArn:
        Fn::ImportValue: !Sub '${NetworkStackName}-HTTPS-Listener'
      Actions:
        - Type: forward
          TargetGroupArn:
            Fn::GetAtt: ['GetEBLBTargetGroupResource', 'TargetGroupArn']
      Conditions:
        - Field: host-header
          HostHeaderConfig:
            Values:
              - mydomain.com
  • 创建VPC、ALB和任何其他共享资源等的人
  • 创建一个弹性beanstalk环境和相关侦听器规则,以使用导入的共享负载平衡器将流量定向到此环境(称此模板为
    环境
我面临的问题是
Environment
模板创建了一个
AWS::ElasticBeanstalk::Environment
,该模板随后创建了一个新的CFN堆栈,其中包含ASG和目标组(或elastic beanstalk已知的流程)。这些资源不是用于创建环境的AWS拥有的CFN模板的输出

设置时

- Namespace: aws:elasticbeanstalk:environment
  OptionName: LoadBalancerIsShared
  Value: true
在我的elastic beanstalk环境的选项设置中,没有创建负载平衡器,这很好。然后,我尝试将侦听器规则附加到负载平衡器侦听器

  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      Priority: 1
      ListenerArn:
        Fn::ImportValue: !Sub '${NetworkStackName}-HTTPS-Listener'
      Actions:
        - Type: forward
          TargetGroupArn: WHAT_GOES_HERE
      Conditions:
        - Field: host-header
          HostHeaderConfig:
            Values:
              - mywebsite.com
    DependsOn:
      - Environment
这里的问题是,据我所知,我无法访问由elastic beanstalk环境资源创建的目标群体的ARN。如果我创建了一个目标组,那么它就不会链接到elastic beanstalk,也不存在任何实例

我找到了哪个州的地图

Elastic Beanstalk为您的环境创建的资源具有名称。您可以使用这些名称通过函数获取有关资源的信息,或者修改资源的属性以自定义其行为

但是因为它们位于不同的堆栈中(我事先不知道其名称),而不是模板的输出,所以我不知道如何获得它们

--

编辑:

Marcin在回答中为我指出了定制资源的方向。我稍微扩展了一下,让它工作起来了。实现在两个方面略有不同

  • 它位于节点中,而不是Python中
  • 在提供的示例中,api调用
    descripe\u environment\u resources
    返回一个资源列表,但似乎不是所有资源。在我的实现中,我获取自动缩放组,并使用物理资源ID使用Cloudformation API查找它所属堆栈中的其他资源
  • 云形成模板

      LambdaBasicExecutionRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Statement:
              - Effect: Allow
                Principal:
                  Service: lambda.amazonaws.com
                Action: sts:AssumeRole
          Path: /
          ManagedPolicyArns:
            - arn:aws:iam::aws:policy/AWSCloudFormationReadOnlyAccess
            - arn:aws:iam::aws:policy/AWSElasticBeanstalkReadOnly
            - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
    
      GetEBLBTargetGroupLambda:
        Type: AWS::Lambda::Function
        Properties:
          Handler: index.handler
          Description: 'Get ARN of EB Load balancer'
          Timeout: 30
          Role: !GetAtt 'LambdaBasicExecutionRole.Arn'
          Runtime: nodejs12.x
          Code:
            ZipFile: |
              ... code ...
      ListenerRule:
        Type: AWS::ElasticLoadBalancingV2::ListenerRule
        Properties:
          Priority: 1
          ListenerArn:
            Fn::ImportValue: !Sub '${NetworkStackName}-HTTPS-Listener'
          Actions:
            - Type: forward
              TargetGroupArn:
                Fn::GetAtt: ['GetEBLBTargetGroupResource', 'TargetGroupArn']
          Conditions:
            - Field: host-header
              HostHeaderConfig:
                Values:
                  - mydomain.com
    
    我在做这件事时学到的东西,希望能帮助别人

  • 在节点中使用
    async
    处理程序很困难,因为默认的cfn响应库不是异步的,这会导致Cloudformation创建(和删除)过程在回滚之前挂起数小时
  • 如果使用
    ZipFile
    ,则cloudformation会自动包括
    cfn响应库。如果您非常倾向于手动包含该代码,则可以在上使用该代码(您也可以将其包装在promise中,然后使用异步lambda处理程序)。npm上也有实现相同效果的包
  • 节点14.x无法运行,Cloudformation抛出了一个错误。不幸的是,我没有记下那是什么
  • 所提供示例中使用的策略
    AWSElasticBeanstalkFullAccess
    已不存在,并已替换为
    AdministratorAccess AWSElasticBeanstalk
  • 我上面的例子需要附加更少的许可策略,但我还没有在测试中解决这个问题。最好只能读取特定的弹性豆茎环境等
  • 据我所知,我无法访问由elastic beanstalk环境资源创建的目标群体的ARN


    没错。克服这一问题的方法是通过。事实上,我为我的一个团队开发了完全有效的、非常类似的资源,因此您可以查看它并采用您的模板。资源返回EB负载平衡器的ARN,但您可以修改它以获得EB目标组的ARN。

    非常感谢,这是一个很好的起点。我在现在的问题中谈到了一些分歧。我本来打算把它写进你的答案,而不是这个问题,但我想我最好先问一下。再次感谢您查看我的代码,我想您可能不知道为什么我的自定义资源不能正确删除?据我所知,lambda没有适当地响应cloudformation,然后它最终超时(许多小时),然后堆栈的其余部分删除并离开定制资源。