为通过 CDK 实例化的 Elastic Beanstalk 环境获取 Application Load Balancer 的句柄

Get a handle for the Application Load Balancer for an Elastic Beanstalk environment instantiated via CDK

我像这样创建了一个 Elastic Beanstalk 环境:

    const certificate = new certificatemanager.Certificate(this, 'Certificate', {
      domainName: props.domainName,
      subjectAlternativeNames: [],
      validationMethod: certificatemanager.ValidationMethod.EMAIL,
    });

    const optionSettings = {
      'aws:autoscaling:asg': {
        MinSize: '2',
        MaxSize: '2',
      },
      'aws:ec2:vpc': {
        VPCId: vpc.vpcId,
        Subnets: vpc.privateSubnets.map((subnet) => subnet.subnetId).join(','),
        ElbSubnets: vpc.publicSubnets.map((subnet) => subnet.subnetId).join(','),
      },
      'aws:elasticbeanstalk:environment': {
        EnvironmentType: 'LoadBalanced',
        LoadBalancerType: 'application',
      },
      'aws:elbv2:listener:443': {
        ListenerEnabled: 'true',
        Protocol: 'HTTPS',
        SSLCertificateArns: certificate.certificateArn,
      },
      'aws:autoscaling:launchconfiguration': {
        IamInstanceProfile: 'aws-elasticbeanstalk-ec2-role',
        InstanceType: 't3.medium',
      },
      'aws:elasticbeanstalk:application:environment': {
        CORS_ORIGIN_ALLOW_ALL: 'False',
      },
    };

    const environment = new elasticbeanstalk.CfnEnvironment(this, 'Environment', {
      environmentName: `env`,
      description: 'My Environment Description',
      applicationName: application.applicationName || 'Error',
      versionLabel: applicationVersion.ref,
      solutionStackName: '64bit Amazon Linux 2018.03 v2.9.5 running Python 3.6',
      optionSettings: OptionSettingsUtil.flatten(optionSettings),
    });

其中 OptionSettingsUtil.flatten 是我编写的用于展平配置选项的自定义函数。

如何获取将由此 Elastic Beanstalk 环境生成的 Application Load Balancer 资源的句柄?我需要它来关联一个 WAF ACL。

在实际创建之前您不能,然后您可以按如下方式查找它:

const loadBalancer = elbv2.ApplicationLoadBalancer.fromLookup(this, 'ALB', {
  loadBalancerTags: {
    'elasticbeanstalk:environment-name': environmentName
  },
})

文档:https://docs.aws.amazon.com/cdk/api/latest/docs/aws-elasticloadbalancingv2-readme.html#looking-up-load-balancers-and-listeners