4. ELB の作成
作成
- 作成者: moritalous
- 作成日: 2023/08/14
参照元:4. ELB の作成
ELBを作成
参考:Amazon Elastic Load Balancing V2 Construct Library
ロードバランサーを作成します。
import * as elb from "aws-cdk-lib/aws-elasticloadbalancingv2";
import * as targets from 'aws-cdk-lib/aws-elasticloadbalancingv2-targets';
注記
v2ではないaws-cdk-lib.aws_elasticloadbalancing module
はClassicロードバランサーです
const loadBalancer = new elb.ApplicationLoadBalancer(this, 'ELB', {
vpc: vpc,
internetFacing: true,
})
インターネットからのアクセスを受け付けるのでinternetFacing
とtrueにセットします。
そしてリスナーとターゲットをそれぞれ追加します。
- リスナー
const listener = loadBalancer.addListener('Listener', {
port: 80
})
- ターゲット
listener.addTargets('instance', {
targets: [
new targets.InstanceTarget(instance),
],
port: 80,
healthCheck: {
path: '/wp-includes/images/blank.gif'
}
})
EC2へのロードバランサーからの80番ポートインバウンド通信を許可します。
instance.connections.allowFrom(loadBalancer, ec2.Port.tcp(80))
課題
ELBを作成してください。
- 80番ポートで待ち受け
- ターゲットはEC2インスタンス
- ロードバランサーのドメイン名をOutputで出力
回答
bin/handson-cdk.ts
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
// import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
import * as rds from "aws-cdk-lib/aws-rds";
import * as elb from "aws-cdk-lib/aws-elasticloadbalancingv2";
import * as targets from 'aws-cdk-lib/aws-elasticloadbalancingv2-targets';
export class HandsonCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// example resource
// const queue = new sqs.Queue(this, 'HandsonCdkQueue', {
// visibilityTimeout: cdk.Duration.seconds(300)
// });
const vpc = new ec2.Vpc(this, "VPC", {
natGateways: 0,
});
const instance = new ec2.Instance(this, 'EC2Instance', {
vpc: vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
machineImage: ec2.MachineImage.latestAmazonLinux2023(),
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC
},
associatePublicIpAddress: true,
instanceName: 'webserver1-user1',
})
instance.connections.allowFromAnyIpv4(ec2.Port.tcp(80))
const asset = new Asset(this, 'Asset', {
path: './asset/install_wordpress.sh'
})
const localPath = instance.userData.addS3DownloadCommand({
bucket: asset.bucket,
bucketKey: asset.s3ObjectKey
})
instance.userData.addExecuteFileCommand({
filePath: localPath
})
asset.grantRead(instance.role)
new cdk.CfnOutput(this, 'EC2PublicIP', {
value: instance.instancePublicIp
})
const rdsInstance = new rds.DatabaseInstance(this, 'RdsDBInstance', {
vpc: vpc,
engine: rds.DatabaseInstanceEngine.MYSQL,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
databaseName: 'wordpress',
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_ISOLATED
},
})
rdsInstance.connections.allowDefaultPortFrom(instance)
new cdk.CfnOutput(this, 'RDSDomainName', {
value: rdsInstance.dbInstanceEndpointAddress
})
const loadBalancer = new elb.ApplicationLoadBalancer(this, 'ELB', {
vpc: vpc,
internetFacing: true,
})
const listener = loadBalancer.addListener('Listener', {
port: 80
})
listener.addTargets('instance', {
targets: [
new targets.InstanceTarget(instance),
],
port: 80,
healthCheck: {
path: '/wp-includes/images/blank.gif'
}
})
instance.connections.allowFrom(loadBalancer, ec2.Port.tcp(80))
new cdk.CfnOutput(this, 'ELBDomainName', {
value: loadBalancer.loadBalancerDnsName
})
}
}