如何获得 ALB 的 ARN

问题描述

我尝试在 TypeScript 中通过 CDK 创建一个 alb。但是当执行 'cdk deploy' 时,我遇到了以下代码错误。我如何获得 ALB 的 ARN?

AlbStack Failed: Error [ValidationError]: Template error: resource alb does not support attribute type Arn in Fn::GetAtt
constructor(scope: cdk.Construct,id: string,props?: cdk.StackProps) {
    super(scope,id,props);

    const target_group = new elbv2.CfnTargetGroup(this,'targetgroup',{
    ~
    });
    
    const alb = new elbv2.CfnLoadBalancer(this,'alb',{
    ~
    });

    new elbv2.CfnListener(this,'alblistener80',{
      defaultActions: xxx,loadBalancerArn: cdk.Fn.getAtt(alb.logicalId,'Arn').toString(),// <----- Here
      port: 80,protocol: 'HTTP',});
  }

解决方法

基于 CloudFormation docs{!! $message->message !!} 返回 Arn,而不是 GetAtt。因此,如果您想使用 Ref,您应该使用 CDK 的 ref

Ref 内在函数返回指定参数或资源的值。

因此,您可以尝试:

cdk.Fn