AWS CDKでプライベートサブネットに配置した踏み台サーバ(EC2)からRDS(PostgreSQL)接続できる環境をデプロイする方法
項目 | バージョン |
---|---|
CDK | 2.27.0 |
プライベートサブネットに配置した踏み台サーバからSSMでRDS(PostgreSQL)に接続できる環境をデプロイしてみました。
SSMを有効にするためにVPCエンドポイント作成、yumなど実行できるようにVPCエンドポイント作成しています。
RDSのインバウンドはポート5432、ソースはEC2のセキュリティグループとなるように設定しています。
RDSの情報はシークレットに格納しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import { Stack, StackProps, aws_secretsmanager } from 'aws-cdk-lib'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; import { Construct } from 'constructs'; import * as rds from 'aws-cdk-lib/aws-rds'; export class Sample001Stack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); 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, } ] }); // VPC Endpoint 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 // プライベートDNS有効化しておく }); } const subnetSelection: ec2.SubnetSelection = { subnetType: ec2.SubnetType.PRIVATE_ISOLATED, onePerAz: true }; new ec2.GatewayVpcEndpoint(this, 's3gateway', { vpc, service: ec2.GatewayVpcEndpointAwsService.S3, subnets: [subnetSelection] }); // シークレット const newSecret = new aws_secretsmanager.Secret( this, 'DBCredentialsSecret', { secretName: 'secret-test', generateSecretString: { excludePunctuation: true, includeSpace: false, generateStringKey: 'password', secretStringTemplate: JSON.stringify({ username: 'hoge', }) } } ); // EC2 SecurityGroup const ec2InstanceSG = new ec2.SecurityGroup(this, 'ec2-instance-sg', { vpc, allowAllOutbound: true // アウトバウンドはデフォルトがtrue }); ec2InstanceSG.addIngressRule( // インバウンド ec2.Peer.anyIpv4(), ec2.Port.tcp(5432) ); // RDS SecurityGroup const rdsSG = new ec2.SecurityGroup(this, 'rds-sg', { vpc, allowAllOutbound: true }); rdsSG.addIngressRule( // インバウンド ec2InstanceSG, ec2.Port.tcp(5432) ); // 踏み台サーバ const host = new ec2.BastionHostLinux(this, 'BastionHost', { vpc, instanceName: "bastion", instanceType: ec2.InstanceType.of(ec2.InstanceClass.T4G, ec2.InstanceSize.NANO), subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED, // プライベートサブネットにEC2配置 }, securityGroup: ec2InstanceSG }); // あらかじめインストールしておく host.instance.addUserData("yum -y update", "yum install -y postgresql jq"); // RDS(postgreSQL) private subnet const postgres = new rds.DatabaseInstance(this, 'Instance', { engine: rds.DatabaseInstanceEngine.postgres({ version: rds.PostgresEngineVersion.VER_12, // バージョン12指定 }), vpc, // vpc変数指定 vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED // プライベートサブネットに配置 }, databaseName: 'postgrexxx', // DB名 securityGroups: [rdsSG], credentials: rds.Credentials.fromSecret(newSecret) // シークレットにRDS情報格納 }); } } |
これでデプロイします。
1 |
cdk deploy |
VPCエンドポイントが作成されています。
RDSのセキュリティグループのインバウンドがEC2のセキュリティグループになっています。
シークレットが作成されています。
RDSの情報が格納されています。
SSMでコマンドプロンプトから接続してみます。
1 2 3 4 5 6 7 8 |
c:\>aws ssm start-session --target 踏み台サーバのid Starting session with SessionId: takahashi-h5-013e27cf085bde113 sh-4.2$ sudo su [root@ip-10-0-2-81 bin]# yum list installed | grep postgres postgresql.aarch64 9.2.24-6.amzn2 @amzn2-core postgresql-libs.aarch64 9.2.24-6.amzn2 @amzn2-core [root@ip-10-0-2-81 bin]# |
あらかじめインストールしたpsqlがインストールされていることが確認できます。
このままpsqlコマンドを実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@ip-10-0-2-81 bin]# psql -h sitqgavnxm2bbs.cjvwtdcasc7l.ap-northeast-1.rds.amazonaws.com --dbname=postgrexxx -U hoge --password Password for user hoge: psql (9.2.24, server 12.8) WARNING: psql version 9.2, server version 12.0. Some psql features might not work. SSL connection (cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256) Type "help" for help. postgrexxx=> \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ------------+----------+----------+-------------+-------------+----------------------- postgres | hoge | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgrexxx | hoge | UTF8 | en_US.UTF-8 | en_US.UTF-8 | rdsadmin | rdsadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin template0 | rdsadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin + | | | | | rdsadmin=CTc/rdsadmin template1 | hoge | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/hoge + | | | | | hoge=CTc/hoge (5 rows) postgrexxx=> |
接続が確認できました。
参考サイト
https://dev.classmethod.jp/articles/db-client-through-ssm/

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