詰まりまくったので共有
必要なモジュール一覧
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)]
});
以上