0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CfnInstanceで立てたEC2インスタンスにロードバランサをつける

Posted at

詰まりまくったので共有

必要なモジュール一覧

import * as ec2 from '@aws-cdk/aws-ec2';
import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
import * as targets from '@aws-cdk/aws-elasticloadbalancingv2-targets';

ec2立てる

const ec2Instance = new ec2.CfnInstance(this, 'instance', {
  instanceType: 't2.micro',
  subnetId: vpc.privateSubnets[0].subnetId,
  tags: [{
    key: 'Name',
    value: 'tag1',
  }],
})

ALB立てる

const alb = new elbv2.ApplicationLoadBalancer(this, 'ApplicationLoadBalancer', {
  vpc,
  vpcSubnets: {
    subnets: vpc.publicSubnets,
  },
  loadBalancerName: 'alb',
  internetFacing: true,
}

80ポートにリスナー設定

const listener = alb.addListener('Listener', { open: true, port: 80 }

80リスナーにターゲットを設定

listener.addTargets('target', {
  port: 80,
  targets: [new targets.InstanceIdTarget(ec2Instance.ref)]
});

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?