ã¯ããã«
EC2 Instance Connect Endpoint(EIC Endpoint)使ã£ã¦ãã¾ããï¼ ç¡æã§è¸ã¿å°ãã¹ããæ²æ» ã§ããç»æçãªãµã¼ãã¹ã§ãã
ãããCDKã§ç°¡åã«ä½æã§ããããã«ãªã£ãã®ã§ããã®ãç´¹ä»ã§ãã
ãã¿ãã¬
EC2ã¸ã®ç®¡çéä¿¡ã¯ãã¡ããã®ãã¨ãRDS(Aurora)ã¸ã®ç®¡çéä¿¡ã確ç«ã§ãã¾ãã ãã¡ããã¡ã便å©ã ã¨æãã®ã§ããã²ã試ããã ããã
ä¸æºå
EIC Endpointã¯å ¬å¼L2ã³ã³ã¹ãã©ã¯ããåå¨ãã¾ããããã³ãã¥ããã£ä¸»å°ã§ã®Construct libraryã§ããopen-constructsã«L2ãåå¨ãã¦ãã¾ãã ããã§ãCDKããã¸ã§ã¯ããã»ããã¢ããããopen-constructsãã¤ã³ã¹ãã¼ã«ãã¦ä½¿ã£ã¦ããã¾ãã
npx cdk init --language=typescript
npm install @open-constructs/aws-cdk
ã¦ã¼ã¹ã±ã¼ã¹
EC2 instanceã¸ã®ç®¡çéä¿¡
ãã©ã¤ãã¼ããµããããã«EC2ã¤ã³ã¹ã¿ã³ã¹ãè¨ç½®ããSSHã§ã®ç®¡çéä¿¡ãEIC Endpointçµç±ã§å®è¡ãã¦ã¿ã¾ãã
CDKã§ã®å®è£ ä¾
import * as ocf from '@open-constructs/aws-cdk' export class TempCdkProjectStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // NATGWãIGWãä¸è¦ã§ããé©å®åé¤ãã¦ãã ããã const vpc = new ec2.Vpc(this, 'VPC') // EC2 instance const instance = new ec2.Instance(this, 'Instance', { vpc, instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO), machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023, }) }); // EIC Endpointã®L2ã³ã³ã¹ãã©ã¯ã const eicEndpoint = new ocf.aws_ec2.InstanceConnectEndpoint(this, 'InstanceConnectEndpoint', { vpc, }); // EIC Endpoint -> EC2 Instanceã¸ã®Security Groupã®ç©´ãã eicEndpoint.connections.allowTo(instance, ec2.Port.tcp(22)); // Instance IDãåºå new cdk.CfnOutput(this, 'InstanceId', { value: instance.instanceId, }); } }
æ¥ç¶æ¹æ³
ä¸è¨CDKã³ã¼ãããããã¤å¾ã以ä¸ã®ã³ãã³ããå®è¡ãã¾ãã instance idã¯cdk deployæã«åºåããããã®ã«ä¿®æ£ãã¦ãã ããã
$ aws ec2-instance-connect ssh --instance-id i-12345example --connection-type eice The authenticity of host '10.0.0.1 (<no hostip for proxy command>)' can't be established. ED25519 key fingerprint is SHA256:abcdefg. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '10.0.0.1' (ED25519) to the list of known hosts. , #_ ~\_ ####_ Amazon Linux 2023 ~~ \_#####\ ~~ \###| ~~ \#/ ___ https://aws.amazon.com/linux/amazon-linux-2023 ~~ V~' '-> ~~~ / ~~._. _/ _/ _/ _/m/' [ec2-user@ip-10-0-0-1 ~]$
ãã£ããç¹ããã¾ããï¼æé«ã§ããã
RDS(Aurora)ã¸ã®ç®¡çéä¿¡
ç¶ãã¦ããã©ã¤ãã¼ããµããããã«Aurora(mysql)ãè¨ç½®ããEIC Endpointçµç±ã§ç®¡çéä¿¡ãè¡ã£ã¦ã¿ã¾ãã mysqlã®ããã©ã«ããã¼ãã¯3306ãªã®ã§EIC Endpointã¯é対å¿ã§ãããDBãlistenãããã¼ãã3389ã«å¤ããèæ¥ã§åãæãã¦ã¿ããã¨æãã¾ãã
CDKã§ã®å®è£ ä¾
import * as ocf from '@open-constructs/aws-cdk' export class TempCdkProjectStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const vpc = new ec2.Vpc(this, 'VPC') // Aurora cluster const auroraCluster = new rds.DatabaseCluster(this, 'Aurora', { engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_3_06_0, }), // ãéè¦ãEIC Endpointã§ã¢ã¯ã»ã¹ããããããã¼ãã3389ã«å¤æ´ port: 3389, // ãã¹ã¯ã¼ããã¼ãã³ã¼ãã£ã³ã°ã®é§ç®ãªä¾ã§ããæ¬çªã§ã¯rds.Credentials.fromGeneratedSecret('hogeuser')ãªã©ã使ã£ã¦ãã ãã credentials: rds.Credentials.fromPassword('admin', cdk.SecretValue.unsafePlainText('testPass')), vpc, writer: rds.ClusterInstance.serverlessV2('writer'), }); const eicEndpoint = new ocf.aws_ec2.InstanceConnectEndpoint(this, 'InstanceConnectEndpoint', { vpc, }); // EIC Endpoint -> Aurora Cluster ã¸ã®Security Groupã®ç©´ãã eicEndpoint.connections.allowTo(auroraCluster, ec2.Port.tcp(3389)); // DB Endpoint (ãã©ã¤ãã¼ãIPã®åå¾ã«ç¨ãã¾ã) new cdk.CfnOutput(this, 'AuroraEndpoint', { value: auroraCluster.clusterEndpoint.hostname, }); new cdk.CfnOutput(this, 'EicEndpointId', { value: eicEndpoint.instanceConnectEndpointId, }); } }
æ¥ç¶æ¹æ³
ã¾ããDBã®ãã©ã¤ãã¼ãIPãåå¾ãã¾ãããããªãã¯DNSãµã¼ãä¸ã§ã®åå解決ã§åé¡ããã¾ããã
$ nslookup hogehoge.cluster-cvekcubvryhp.ap-northeast-1.rds.amazonaws.com Server: 8.8.8.8 Address: 8.8.8.8#53 Non-authoritative answer: hogehoge.cluster-cvekcubvryhp.ap-northeast-1.rds.amazonaws.com canonical name = hogehoge.cluster-cvekcubvryhp.ap-northeast-1.rds.amazonaws.com. Name: hogehoge.cluster-cvekcubvryhp.ap-northeast-1.rds.amazonaws.com Address: 10.0.198.63 // ãã¡ã
ç¶ãã¦ãSSHãã³ãã«ãå¼µãã¾ãã以ä¸ã®éãããã©ã¡ã¼ã¿ãæå®ãã¦ãã ããã
ãã©ã¡ã¼ã¿å | value |
---|---|
--private-ip-address | DBã®ãã©ã¤ãã¼ãIP |
--instance-connect-endpoint-id | EIC Endpointã®ID |
--local-port | ãã¼ã«ã«ã§listenãããã¼ã (ä½ã§ãOK) |
--remote-port | DBãµã¼ããlistenãããã¼ã(3389) |
$ aws ec2-instance-connect open-tunnel --instance-connect-endpoint-id eice-hogehoge --private-ip-address 10.0.198.63 --local-port 3306 --remote-port 3389 Listening for connections on port 3306.
ç¡äºã«ãã³ãã«ãå¼µãã¾ããï¼ä¸è¨ã®ã³ãã³ãã¯åãããã¾ã¾ãé©å½ãªDBã¯ã©ã¤ã¢ã³ããã¼ã«ã§localhost:3306ã«æ¥ç¶ãã¦ã¿ã¾ãããã
ä»åã¯Sequel Aceã§æ¥ç¶ãã¦ã¿ã¾ããã¯ã¬ãã³ã·ã£ã«ã¯CDKã§æå®ããusername:admin, pass: testPassã§ãã
ããã¾ãããï¼æé«ã§ããã
ããã¯Session Managerã§ã¯ã§ããªãã®ã§ã大ããªå·®å¥åãã¤ã³ãã ã¨æãã¾ãã
使ç¨ä¸ã®æ³¨æ
EIC Endpointçµç±ã§ã®ã¢ã¯ã»ã¹æã®æ¨©é
ã¦ã¼ã¶ã«ç¹å®ã®æ¨©éãå¿ è¦ã§ããæ£ç¢ºãªæ å ±ã¯å ¬å¼ããã¥ã¡ã³ããåç §ãã¦ãã ããã
ãããæ´»ç¨ãããã¨ã§ãç¹å®ã®ã¦ã¼ã¶ã«ã®ã¿ã¢ã¯ã»ã¹æ¨©éãä¸ãããã¨ãã§ãã¾ããIAMã®ãé°ã§èªå¯ç®¡çãã©ã¯ã©ã¯ã§ããï¼
DBã¢ã¯ã»ã¹ã¯EIC Endpointã®ã¦ã¼ã¹ã±ã¼ã¹ã«å«ã¾ãã¦ãã¾ããï¼
ãããã¾ãããå¤åã°ã¬ã¼ã¾ã¼ã³ã§ãã
å½åEIC Endpointã®ãµã¼ãã¹ãªãªã¼ã¹æã«ã¯å ¨portãéæ¾ããã¦ãã¾ããããããã«22, 3389以å¤closeããã¦ãã¾ãã¾ããããããã£ã¦ãAWSçã«ã¯å¤§äººããSSH, RDPã§ã ã使ã£ã¦ã»ããã®ã ã¨æã£ã¦ãã¾ãã
ç¾ç¶ãEIC Endpointã¯ããã¾ã§ãã¼ãçªå·ã ã(L4ã¬ã¤ã¤ã ã)ã§éä¿¡ã®è¨±å¯/æå¦ãå¤å®ãã¦ãã¾ãã æ´ã«L7ã¬ã¤ã¤ã®ãã¤ãã¼ãã¾ã§æ·±å ã£ã¦è¨±å¯/æå¦ã®å¤å®ã¯æµç³ã«ããªãã¨æãã®ã§ããããããããæè¡çã«ã¯ã§ããã®ã§ãããã...??詳ãã人æãã¦ä¸ãã...
ã¨ãããã¨ã§ãåãã¯ãã¾ãããèªå·±è²¬ä»»ã§ãé¡ããããã¾ãã
ãã®ä»ã®VPCãªã½ã¼ã¹ã¸ã®ã¢ã¯ã»ã¹ãã§ãã¾ããï¼
VPCå ã®ãã©ã¤ãã¼ãIPã¨ãã¼ãçªå·ãã決ã¾ãã°ã¤ã±ã«ã¯ãã§ãã
æå¾ã«
å®ã¯ãã®L2ã³ã³ã¹ãã©ã¯ãã¯ç§ãPRãä½æãã¾ããï¼
open-constructsã«ã¯ã¾ã 2ã¤ããL2ãç¡ãã®ã§ãã³ã³ããªãã¥ã¼ããã£ã³ã¹ãç¡éã«ç ã£ã¦ãã¾ãã çæ§ããã²ãã£ã¬ã³ã¸ãã ãããï¼