AWS CDKでロードバランサとターゲットグループの設定

SSL証明書を指定

    // SSL証明書指定(ACM)
    const certificate = acm.Certificate.fromCertificateArn(
      this,
      'MyCertificate',
      'arn:aws:acm:ap-northeast-1:123456789012:certificate/abcdefg1-h2ijk-3456-7890-1m2n3o4p5q6r',
    );

ALB(ロードバランサ)の設定

    // ALB作成
    const alb = new elbv2.ApplicationLoadBalancer(this, 'MyALB', {
      vpc,
      vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
      securityGroup,
      internetFacing: true,
    });

ターゲットグループの作成

    // ターゲットグループ
    const targetGroup = new elbv2.ApplicationTargetGroup(this, 'MyTargetGroup', {
      vpc,
      port: 80,
      protocol: elbv2.ApplicationProtocol.HTTP,
      targetType: elbv2.TargetType.INSTANCE,
      targets: [new InstanceTarget(ec2Instance, 80)],
      // 以下の設定ではCDK 2.1001.0 で動作しない。
      // targets: [ec2Instance],
      // targets: [new elbv2.InstanceTarget(ec2Instance, 80)],
      // targets: [new elbv2.InstanceTargetId(ec2Instance.instanceId)],
      healthCheck: {
        path: '/',
        port: 'traffic-port',
      },
    });
    // 以下の設定ではCDK 2.1001.0 で動作しない。
    // targetGroup.addTarget(ec2Instance);

ALBにリスナーを追加(ターゲットグループと証明書を追加)

    // ALBにリスナーを追加
    alb.addListener('Listener', {
      port: 443,
      protocol: elbv2.ApplicationProtocol.HTTPS,
      certificates: [certificate],
      defaultAction: elbv2.ListenerAction.forward([targetGroup]),
    });
This entry was posted in AWS, CDK, TypeScript, 技術情報. Bookmark the permalink.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です