constructor()

in source/patterns/@aws-solutions-constructs/aws-route53-alb/lib/index.ts [106:172]


  constructor(scope: Construct, id: string, props: Route53ToAlbProps) {
    super(scope, id);
    defaults.CheckProps(props);

    // NOTE: We don't call CheckAlbProps() here, because this construct creates an ALB
    // with no listener or target, so some of those checks don't apply
    if (props?.loadBalancerProps?.vpc) {
      throw new Error('Specify any existing VPC at the construct level, not within loadBalancerProps.');
    }

    if (props.existingLoadBalancerObj && !props.existingVpc) {
      throw new Error('An existing ALB already exists in a VPC, so that VPC must be passed to the construct in props.existingVpc');
    }

    if (props.existingHostedZoneInterface && !props.publicApi && !props.existingVpc) {
      throw new Error('An existing Private Hosted Zone already exists in a VPC, so that VPC must be passed to the construct in props.existingVpc');
    }

    if (props.existingVpc) {
      this.vpc = props.existingVpc;
    } else {
      this.vpc = defaults.buildVpc(scope, {
        defaultVpcProps: props.publicApi ?
          defaults.DefaultPublicPrivateVpcProps() :
          // If this is an internal app, we're going to turn on DNS
          // by default to allow gateway and interface service endpoints
          defaults.overrideProps(defaults.DefaultIsolatedVpcProps(), { enableDnsHostnames: true, enableDnsSupport: true, }),
        userVpcProps: props.vpcProps,
      });
    }

    if (props.existingHostedZoneInterface) {
      this.hostedZone = props.existingHostedZoneInterface;
    } else {
      if (props.publicApi) {
        throw new Error('Public APIs require an existingHostedZone be passed in the Props object.');
      } else {
        if (!props.privateHostedZoneProps) {
          throw new Error('Must supply privateHostedZoneProps to create a private API');
        }
        if (props.privateHostedZoneProps.vpc) {
          throw new Error('All VPC specs must be provided at the Construct level in Route53ToAlbProps');
        }
        const manufacturedProps: r53.PrivateHostedZoneProps = defaults.overrideProps(props.privateHostedZoneProps, { vpc: this.vpc });
        this.hostedZone = new r53.PrivateHostedZone(this, `${id}-zone`, manufacturedProps);
      }
    }

    this.loadBalancer = defaults.ObtainAlb(
      this,
      id,
      this.vpc,
      props.publicApi,
      props.existingLoadBalancerObj,
      props.loadBalancerProps,
      props.logAlbAccessLogs,
      props.albLoggingBucketProps
    );

    // Add the ALB to the HostedZone as a target
    const hostedZoneTarget = new r53t.LoadBalancerTarget(this.loadBalancer);

    new r53.ARecord(this, `${id}-alias`, {
      target: { aliasTarget: hostedZoneTarget },
      zone: this.hostedZone
    });
  }