constructor()

in packages/aws-rfdk/lib/core/lib/staticip-server.ts [223:271]


  constructor(scope: Construct, id: string, props: StaticPrivateIpServerProps) {
    super(scope, id);

    const { subnets } = props.vpc.selectSubnets(props.vpcSubnets);
    if (subnets.length === 0) {
      throw new Error(`Did not find any subnets matching ${JSON.stringify(props.vpcSubnets)}. Please use a different selection.`);
    }
    const subnet = subnets[0];

    if (props.resourceSignalTimeout && props.resourceSignalTimeout.toSeconds() > (12 * 60 * 60)) {
      throw new Error('Resource signal timeout cannot exceed 12 hours.');
    }

    this.autoscalingGroup = new AutoScalingGroup(this, 'Asg', {
      minCapacity: 1,
      maxCapacity: 1,
      vpc: props.vpc,
      instanceType: props.instanceType,
      machineImage: props.machineImage,
      vpcSubnets: { subnets: [subnet] },
      blockDevices: props.blockDevices,
      keyName: props.keyName,
      signals: props.resourceSignalTimeout ? Signals.waitForCount(1, { timeout: props.resourceSignalTimeout }) : undefined,
      role: props.role,
      securityGroup: props.securityGroup,
      userData: props.userData,
    });
    this.connections = this.autoscalingGroup.connections;
    this.grantPrincipal = this.autoscalingGroup.grantPrincipal;
    this.osType = this.autoscalingGroup.osType;
    this.role = this.autoscalingGroup.role;
    this.userData = this.autoscalingGroup.userData;

    const scopePath = this.node.scopes.map(construct => construct.node.id).slice(1); // Slice to remove the unnamed <root> scope.
    const eni = new CfnNetworkInterface(this, 'Eni', {
      subnetId: subnet.subnetId,
      description: `Static ENI for ${scopePath.join('/')}`,
      groupSet: Lazy.list({ produce: () => this.connections.securityGroups.map(sg => sg.securityGroupId) }),
      privateIpAddress: props.privateIpAddress,
    });
    this.privateIpAddress = eni.attrPrimaryPrivateIpAddress;

    // We need to be sure that the ENI is created before the instance would be brought up; otherwise, we cannot attach it.
    (this.autoscalingGroup.node.defaultChild as CfnResource).addDependency(eni);

    this.attachEniLifecyleTarget(eni);

    this.node.defaultChild = this.autoscalingGroup.node.defaultChild;
  }