AWS CDKで踏み台サーバ(EC2)をデプロイする方法
項目 | バージョン |
---|---|
CDK | 2.27.0 |
AWS CDKで踏み台サーバ(EC2)をデプロイする方法です。プライベートサブネットにデプロイしています。
踏み台サーバの場合はEC2のクラスでもOKですが、踏み台サーバ専用のBastionHostLinuxクラスが用意されているようです。
既存のVPCにプライベートサブネットがあればデフォルトではプライベートサブネットにEC2が作成されるようです。
Type: SubnetSelection (optional, default: private subnets of the supplied VPC)
Select the subnets to run the bastion host in.
Set this to PUBLIC if you need to connect to this instance via the internet and cannot use SSM. You have to allow port 22 manually by using the connections field
また、machineImageが未指定の場合は、SSMエージェントがプリインストールされているAMIが選択されるようです。
Type: IMachineImage (optional, default: An Amazon Linux 2 image which is kept up-to-date automatically (the instance may be replaced on every deployment) and already has SSM Agent installed.)
The machine image to use, assumed to have SSM Agent preinstalled.
という事は、あとはVPCエンドポイントとセキュリティグループの設定だけ別途作成が必要になるっぽいです。
VPCエンドポイント作成
VPCエンドポイントで以下3つ作成します。
- com.amazonaws.ap-northeast-1.ssm
- com.amazonaws.ap-northeast-1.ssmmessages
- com.amazonaws.ap-northeast-1.ec2messages
tsファイルで以下のように3つ作成します。
const endpoints : Array<[string, string]> = [['com.amazonaws.ap-northeast-1.ssm', 'ssm'], ['com.amazonaws.ap-northeast-1.ec2messages', 'ec2messages'], ['com.amazonaws.ap-northeast-1.ssmmessages', 'ssmmessages']] for(const data of endpoints) { new ec2.InterfaceVpcEndpoint(this, data[1], { vpc, service: new ec2.InterfaceVpcEndpointService(data[0], 443), privateDnsEnabled: true }); }
以下のように3つ作成されます。
※既存VPC上にデプロイする場合はsubnetsオプションの指定が必要です
const endpoints : Array<[string, string]> = [['com.amazonaws.ap-northeast-1.ssm', 'ssm'], ['com.amazonaws.ap-northeast-1.ec2messages', 'ec2messages'], ['com.amazonaws.ap-northeast-1.ssmmessages', 'ssmmessages']] for(const data of endpoints) { new ec2.InterfaceVpcEndpoint(this, data[1], { vpc, service: new ec2.InterfaceVpcEndpointService(data[0], 443), subnets: { subnetType: ec2.SubnetType.PUBLIC // 明示的に指定 }, privateDnsEnabled: true }); }
EC2作成(踏み台サーバ)
EC2(踏み台サーバ)をBastionHostLinuxクラスから作成します。
const ec2InstanceSG = new ec2.SecurityGroup(this, 'ec2-instance-sg', { vpc, // vpcの変数 CIDR 10.0.0.0/16 allowAllOutbound: false // アウトバウンドはデフォルトがtrueなのでfalseにしておく }); ec2InstanceSG.addEgressRule( // アウトバウンド ec2.Peer.ipv4('10.0.0.0/16'), // VPCと同じCIDR指定 ec2.Port.tcp(443) );
アウトバウンドがvpcと同じCIDRで443解放されています。
import { Stack, StackProps } from 'aws-cdk-lib'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; import { Construct } from 'constructs'; export class Sample001Stack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // create the VPC const vpc = new ec2.Vpc(this, 'vpc-sample-cdk', { cidr: '10.0.0.0/16', natGateways: 0, maxAzs: 2, subnetConfiguration: [ { name: 'public-subnet', subnetType: ec2.SubnetType.PUBLIC, cidrMask: 24, }, { name: 'isolated-subnet', subnetType: ec2.SubnetType.PRIVATE_ISOLATED, cidrMask: 24, } ] }); const endpoints : Array<[string, string]> = [['com.amazonaws.ap-northeast-1.ssm', 'ssm'], ['com.amazonaws.ap-northeast-1.ec2messages', 'ec2messages'], ['com.amazonaws.ap-northeast-1.ssmmessages', 'ssmmessages']] for(const data of endpoints) { new ec2.InterfaceVpcEndpoint(this, data[1], { vpc, service: new ec2.InterfaceVpcEndpointService(data[0], 443), privateDnsEnabled: true // プライベートDNS有効化しておく }); } const subnetSelection: ec2.SubnetSelection = { subnetType: ec2.SubnetType.PRIVATE_ISOLATED, onePerAz: true }; new ec2.GatewayVpcEndpoint(this, 's3gateway', { vpc, service: ec2.GatewayVpcEndpointAwsService.S3, subnets: [subnetSelection] }); // create a security group for the EC2 instance const ec2InstanceSG = new ec2.SecurityGroup(this, 'ec2-instance-sg', { vpc, allowAllOutbound: false // アウトバウンドはデフォルトがtrueなのでfalseにしておく }); ec2InstanceSG.addEgressRule( // アウトバウンド ec2.Peer.ipv4('0.0.0.0/0'), ec2.Port.tcp(80) ); ec2InstanceSG.addEgressRule( // アウトバウンド ec2.Peer.ipv4('0.0.0.0/0'), ec2.Port.tcp(443) ); // 踏み台サーバ const host = new ec2.BastionHostLinux(this, 'BastionHost', { vpc, instanceName: "bastion", instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MEDIUM), subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED // プライベートサブネットにEC2配置 }, securityGroup: ec2InstanceSG }); } }
これでデプロイします。
cdk deploy
デプロイに199.12sかかりました、EC2が作成されます。
SSMで接続確認OKです。
yumが使えるように「com.amazonaws.ap-northeast-1.s3」のVPCエンドポイント(Gatewayタイプ)を作成する、またアウトバウンド80と443を開放する必要があります。
yumコマンドが実行できることが確認できます。
参考サイト














KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^
資格:少額短期保険募集人,FP3級,宅建士
コメント