CS課ä½ç«¹ã§ãã
ä»åã¯ãAWS Systems Manager ã®ãã¼ããã©ã¯ã¼ãã£ã³ã°æ©è½ãã®éä¿¡è¦ä»¶ã«ã¤ãã¦å®éã«æ§ç¯ãè¡ããªããæ´çããã¾ããå·¦è¨ã®ããã°ã¨ããªãéè¤ããã¨ããã¯ããã¾ããããã®ããã°ã®ä¸»é¡ã¯ãWindows端æ«ã«ãªã¢ã¼ããã¹ã¯ãããæ¥ç¶ãè¡ãå ´åã®Systems Manager Port Forwardingã«é¢ããæå°éã®éä¿¡è¦ä»¶ãæ´çãããã¨ãã«ãªãã¾ãã
- åæ
- æ§æå³
- EC2(Windows)ãPrivate Subnetã«æ§ç¯/èµ·åãã
- SSMã¸ã®VPC Endpointã¨Security Groupãä½æãã
- SSMã®ã³ã³ã½ã¼ã«ããSSM Agentã®ãã¼ã¸ã§ã³ã確èªãã
- SSMã®ã³ã³ã½ã¼ã«ããSSM Agentã®ãã¼ã¸ã§ã³ã¢ãããè¡ã
- ããã¾ã§ã®ãããã
- ãã¼ã«ã«ã¯ã©ã¤ã¢ã³ãã«CLIã¨Pluginãã¤ã³ã¹ãã¼ã«ãã
- IAM User ã« Policyãä»ä¸ããCredentialãæãåºã
- åä½ç¢ºèª
- æ¥ç¶è¦ä»¶ ã¾ã¨ã
åæ
ä»åã以ä¸ã®åæã§å®è£ ãè¡ãã¾ãã
- EC2(Windows Server) ã¯Private Subnetã«é ç½®ããã¦ãããã¨
- Security Groupsã¯Inbound/Outbound ã¨ãã«å³å¯ã«å¶éãããã¨
- Port Forwarding ã§ã¯ç¹å®ã®EC2ã®ã¿ã«ã¢ã¯ã»ã¹ã§ããããIAMã§å¶éãããã¨
ã§ã¯è¡ã£ã¦ã¿ã¾ãããã
æ§æå³
以ä¸ã«å ¨ä½å³ãè¨ãã¾ããã¾ããã¾ã¨ãã¯æå¾ã«è¨è¼ãã¦ãã¾ããããã¾ã¨ãã ã確èªãããããæ¹ã¯æä¸é¨ã¾ã§ç§»åãã ããã
EC2(Windows)ãPrivate Subnetã«æ§ç¯/èµ·åãã
ã¾ãã¯EC2 Instanceãç¨æãã¾ããæ¢ã«EC2 Instanceãæ§ç¯æ¸ã®æ¹ã¯ãStart(èµ·å)ãã¦ãã ããã
ãSSMãã¤ãªãããªããã¨ãããã©ãã«ã§ãç¨ã«åå ããEC2ãããããèµ·åãã¦ããªãã£ããã¨ãããã¨ãããã¾ãã®ã§ãæ¯éEC2ãèµ·åç¶æ
ã§ããã®ãã¯ç¢ºèªããé¡ããã¾ãã
ä»åã¯ã i-0c678d2c038786416 (SSM Port Forwarding Windows 2019)ã¨ããEC2 InstanceãAmazonåºè·ã®AMIããæ°è¦ã«æ§ç¯ãã¾ããã®ã§ããããä¾ã«ã¨ã£ã¦é²ãã¦ããã¾ãã
EC2ã«IAM Roleãä»ä¸ãã
æ§æå³ã®å·¦ä¸ã«ããEC2 Instanceã®æ§ç¯ã¯å®äºãã¾ããã®ã§ã次ã«IAM Roleãä»ä¸ãã¾ãã
IAM Roleã¯ãããã¾ã§ã¯ãAmazonEC2RoleforSSMããå©ç¨ãããã¨ãæ¨å¥¨ããã¦ããã¾ããããç¾å¨ã¯éæ¨å¥¨ç¶æ
ã§ããçç±ã¨ãã¦ã¯ãS3ãã±ããå
¨ã¦ã«ã¢ã¯ã»ã¹ãå¯è½ã¨ãªã£ã¦ãããªã©ã許å¯ç¯å²ãåºãããããã¨æããã¾ãã
ã¨ãããã¨ã§ãä»åã¯æ°è¦ã«IAM Roleãä½æãããã¨æãã¾ããæ°è¦ã«ä½æããRoleåã¯ãAmazonEC2RoleforSSMPFãã¨ãã¾ãããIAMã®ç»é¢ãããRoleãä½æãã¾ããããªã·ã¼åãAmazonSSMManagedInstanceCoreãã ããã¾ãã¯é¸æãã¦ä½æãã¾ãã
ä½æãå®äºãããããã®RoleãEC2 Instanceã«ã¢ã¿ãããã¾ãã
ç¡äºã«ã¢ã¿ãããã§ãã¾ããã
次ã¯EC2 Instanceã«ã¤ã³ã¹ãã¼ã«ããã¦ããSSM Agentã®Versionã®ç¢ºèªã«ãªãã®ã§ãããããã¯SSMã®æ©è½ã§å¯¾å¿ãããã次ã«è¡ãã¾ãã
SSMã¸ã®VPC Endpointã¨Security Groupãä½æãã
以ä¸ã®å ¬å¼ããã¥ã¡ã³ããåèã«ãVPC Endpointã3ã¤ä½æãã¾ãã
Systems Manager ã使ç¨ãã¦ã¤ã³ã¿ã¼ãããã¢ã¯ã»ã¹ãªãã§ãã©ã¤ãã¼ã EC2 ã¤ã³ã¹ã¿ã³ã¹ã管çã§ããããã«ãVPC ã¨ã³ããã¤ã³ããä½æããã«ã¯ã©ãããã°ããã§ããï¼
- com.amazonaws.region.ssm
- com.amazonaws.region.ec2messages
- com.amazonaws.region.ssmmessages
ã®3ã¤ãä½æãã¦ãã ããããªãVPC Endpointã«ã¯Security Groupãå¿ è¦ã«ãªãã¾ããä»åã¯EC2ããã®ã¢ã¯ã»ã¹ã®ã¿ã«çµãããããssmvpcendpointãã¨ããSecurity Groupãæ°è¨ããããã3ã¤ã®Endpointã«ä»ä¸ãã¾ããã
å ·ä½çãªInboundã®è¨å®ã¯ä»¥ä¸ã®éãã§ããããã¥ã¡ã³ãã«ã以ä¸ã®éãè¨è¼ãããã¾ãããOutboundã®ã«ã¼ã«ã¯ä¸è¦ãªãããè¨è¼ãã¾ããã
ã»ãã¥ãªãã£ã°ã«ã¼ãã¯ãã¼ã 443 ã§ã¤ã³ã¹ã¿ã³ã¹ããã®ã¤ã³ãã¦ã³ããã©ãã£ãã¯ã許å¯ããå¿ è¦ãããã¾ãã
ä»åãEC2 Instance㯠192.168.64.0/24 ã®ç¯å²ã«ã®ã¿åå¨ãããããInbound ã¯ãã®ç¯å²ã«çµã£ãCIDRã¨ãã¾ããã
è£è¶³1ï¼S3ã®VPC Endpointãåããã¦ä½æãã
SSMã®å®è¡ã«ããã¦ãã°åºåãªã©ã§ãå©ç¨ãããããããS3ã®VPC Endpointããªãå ´åã¯åããã¦ä½æãã ããããªããS3ã®VPC Endpointã¯ã²ã¼ãã¦ã§ã¤å´ã®Endpointã¨ãªã£ã¦ãããEndpointã«ä»ä¸ããSecurity Groupã§ã¯å¶å¾¡ããï¼Sgã¯ä»ä¸ã§ãã¾ããï¼ãRoute tableã«è¨è¼ãã¾ãããªããæ±äº¬ãªã¼ã¸ã§ã³ã®S3 VPC Endpointã¯ãpl-61a54008ããPrefixListIdã¨ãã¦ä»ä¸ããã¦ããããããEC2å´ã®Security Groupã«è¨è¼ãããã¨ãå¯è½ã§ããå ·ä½çã«ã¯ä»¥ä¸ã®ããã«è¨è¼ãã¾ãã
EC2ã«VPC Endpointåãã®Security Groupãä»ä¸ãã
é常ãEC2 Instanceã«ã¢ã¿ããããSecurity Groupã®Outboundãå¶å¾¡ãããã¨ã¯ãã¾ãç¡ãã¨ã¯æãã¾ããããããå³æ ¼ãªå¶å¾¡ã好ã¾ããã客æ§ãããã£ãããã¾ããããä»åã¯Outboundãå¶éãã¾ãã
ä½æããã3ã¤ã®åVPC Endpointã¯ENIãä¿æãããããããããã®ENIã®Local IPãæå®ããSecurity Groupãä½æããã®ãå³æ ¼ã¨ã¯èãããã¾ãããIPã¢ãã¬ã¹ãå¤æ´ããã¦ãã¾ãå¯è½æ§ãå å³ããä»åã¯VPC Endpointã«ä»ä¸ãã¦ãããssmvpcendpointãã¨ããSecurity Groupã«å¯¾ãã¦ã®è¨±å¯ã¨ãã¾ãã
å®éã®è¨å®ã®åã«ãã¾ãã¯ãssmvpcendpointforEC2ãã¨ããååã®ç©ºã®Security Groupã1ã¤ä½æãã¾ããããã¦å
ã»ã©è¨è¼ããéããOutboundã®443/TCPããssmvpcendpointãã«ã ã許å¯ããè¨å®ãæ½ãã¾ãã
æå¾ã«ãã®ãssmvpcendpointforEC2ããEC2 Instanceã«ã¢ã¿ããããä½æ¥ã¯å®äºã§ãã
SSMã®ã³ã³ã½ã¼ã«ããSSM Agentã®ãã¼ã¸ã§ã³ã確èªãã
ããã¾ã§ä½æ¥ãã¾ãã¨ãä¸å³ã®ç¶æ ã«ãªãã¾ããããã¾ã§ã§ãæ¢ã«ä»¥ä¸ã®ä½æ¥ãå®äºãã¦ãã¾ãã
- EC2 Instanceã®æ§ç¯ã¨èµ·åï¼SSM Agentã¯Amazonåºè·ã®AMIã«ã¯æ¢ã«å«ã¾ãã¦ããï¼
- IAM Roleã®æ°è¦ä½æã¨EC2 Instanceã¸ã®ä»ä¸
- VPC Endpoint 3ã¤ã®ä½æã¨EC2 InstanceããVPC Endpointã¸ã®ã¢ã¯ã»ã¹ã許å¯ããSecurity Groupã®ä»ä¸(Inbound)
- EC2 InstanceããVPC Endpointã¸ã®ã¢ã¯ã»ã¹ã許å¯ããSecurity Groupã®ä»ä¸(Outbound)
ããã¾ã§ã®ä½æ¥ã§ãSystems Managerã®ã³ã³ã½ã¼ã«ãããManaged Instancesã¨ãã¦ç¢ºèªãå¯è½ã¨ãªãã¾ããå®éã«ç¢ºèªããã¦ã¿ã¾ãã
ä¸å³ã®éãã対象ã®Instanceã表示ãããã§ããããï¼ãããã®ä¸è¦§ã«è¡¨ç¤ºãããªãå ´åã¯ãEC2 Instanceã®åèµ·åãªã©ãè¡ã£ã¦ã¿ã¦ãã ããããã®ç»é¢ãããAgentã®ãã¼ã¸ã§ã³ã確èªå¯è½ã§ããã2.3.634.0ãã¨ãªã£ã¦ãã¾ãããSSMã®Port Forwardingã«ã¯ãSSM Agent (2.3.701.0)ã以ä¸ãå¿ é ã¨ã®ãã¨ã§ãã®ã§ããã¼ã¸ã§ã³ã¢ãããè¡ãã¾ãã
SSMã®ã³ã³ã½ã¼ã«ããSSM Agentã®ãã¼ã¸ã§ã³ã¢ãããè¡ã
Run Command ã使ç¨ã㦠SSM ã¨ã¼ã¸ã§ã³ã ãæ´æ°ãã https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/rc-console.html#rc-console-agentexample
ä¸è¨ããã¥ã¡ã³ãã«æ²¿ã£ã¦ãSSM Agentã®ãã¼ã¸ã§ã³ã¢ããï¼AWS-UpdateSSMAgent ã®RunCommandã§ã®å®è¡ï¼ãè¡ãã¾ãã以ä¸å°ã注æç¹ãè¨è¼ãã¾ãã
ã¿ã¼ã²ããã¨ãªãEC2 Instanceã«ã¯ãä»åãã¼ã¸ã§ã³ã¢ããããããEC2ã1å°ã®ã¿é¸æãã¾ãã
ãOutput optionsãã§ãSSMã®å®è¡çµæãS3ãã±ããã«åºåããããè¨å®ãå¯è½ã§ãããç¾å¨EC2ã«ä»ä¸ããã¦ããIAM Roleã«ãããAmazonSSMManagedInstanceCoreãã«ã¯S3ãã±ããã«åºåãã権éãä¿æãã¦ããªãããããã失æãã¾ãããã£ã¦ãä¸æ¦ãã®Optionã¯ä»åå©ç¨ããªããã¨ã¨ãã¾ãã
RunCommand ãå®è¡ããã¨ãä¸å³ã®ããã«ãIn Progressãã®ã¹ãã¼ã¿ã¹ã«ãªãã¾ãã
Successã¨ãªãã°å®äºã§ãããã¼ã¸ã§ã³ã確èªãã¾ããã2.3.722.0ãã«ã¢ãããã¼ãããã¾ããã
è£è¶³2ï¼S3 Bucketã«SSMã®ãã°ãåºåããå ´åã®æ¨©éè¨å®
ç¾å¨EC2 Instanceã«ä»ä¸ããã¦ããIAM Roleã¯ãAmazonEC2RoleforSSMPFãã§ãããããã«ã¤ã³ã©ã¤ã³ããªã·ã¼çã§ä»¥ä¸ã®IAM Policyã追è¨ãããã¨ã§S3 Bucketã«RunCommandã®å®è¡çµæãåºåãããã¨ãå¯è½ã§ããâ»SSMã®ãã°ã¯EC2ã®IAM Roleã®æ¨©éã§S3ã«Putããã¾ã
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl" ], "Resource": [ "arn:aws:s3:::ssm-result-bucket-999999999999/*" ] } ] }
[ssm-result-bucket-999999999999] ã®ç®æã¯ãçµæãputããããS3 bucketåã«å¤æ´ãã ããã
ããã¾ã§ã®ãããã
ããã¾ã§ã§ä¸è¨ã®æ§æå³ã¾ã§é²ã¿ã¾ãããEC2ã¨SSMã¯æ¢ã«éä¿¡ã確ç«ãã¦ãããã¾ãSSMããã®Run Commandãå®è¡ãå¯è½ãªç¶æ ã¨ãªã£ã¦ãã¾ãã
ãã¼ã«ã«ã¯ã©ã¤ã¢ã³ãã«CLIã¨Pluginãã¤ã³ã¹ãã¼ã«ãã
SSM Port Forwardingãå®è¡ããããã«ã¯ããã¼ã«ã«ã¯ã©ã¤ã¢ã³ãã®è¨å®ãå¿ è¦ã«ãªãã¾ããå ·ä½çã«ã¯ã以ä¸ã®2ã¤ãæºãããã¦ããå¿ è¦ãããã¾ã(ç¹ã« Windows Clientã®å ´å)ã
- AWS-CLI (1.16.234 以ä¸)
- session-manager-plugin (1.1.31.0 以ä¸)
CLIã¨Pluginã¯ä»¥ä¸ã®ããã¥ã¡ã³ããåèã«ã¤ã³ã¹ãã¼ã«ãã ããã
AWS CLI ã®ã¤ã³ã¹ãã¼ã« https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-chap-install.html
(ãªãã·ã§ã³) AWS CLI ç¨ã® Session Manager Plugin ãã¤ã³ã¹ãã¼ã«ãã https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html
ã©ã¡ããææ°ãã¼ã¸ã§ã³ãã¤ã³ã¹ãã¼ã«ãã¦é ããã°åé¡ããã¾ããããã¼ã¸ã§ã³ãå¤ãæ¹ã¯ãã¼ã¸ã§ã³ã¢ããããé¡ããã¾ãããªããããããã®ãã¼ã¸ã§ã³ç¢ºèªã³ãã³ãã¯ä»¥ä¸ã®éãã§ãã
aws --version session-manager-plugin --version
IAM User ã« Policyãä»ä¸ããCredentialãæãåºã
ç¾æç¹ã§ãããã¾ã§è¨å®ãå®äºãã¾ããããã®æç¹ã§æ¢ã«ã¢ã¯ã»ã¹ã§ããããªæ°é ã§ãããã¾ã Port Forwardingã¯ã§ãã¾ãããããã¯CLIãå®è¡ããIAM Userã®ä½æã¨æ¨©éè¨å®ãä¸è¶³ãã¦ããããã§ããä»ãããããè¨å®ãã¾ãã
ã¾ãä»åãä»®ã®IAM Userã¨ãã¦ãssmpfããä½æãã¾ãããCLIã§ã¢ã¯ã»ã¹ãããããCredentialãä½æãã¦CSVããã¦ã³ãã¼ããã¾ããã¾ãããssmpfãã¸ä»ä¸ããPolicyã§ãããä»åæä½éã®IAM Policyãè¨è¼ããããã¤ã³ã©ã¤ã³ããªã·ã¼ã«è¨è¼ãããã¨ã«ãã¾ããã以ä¸ããã¼ããã©ã¯ã¼ãã«å¿ è¦ãªæä½éã®IAMããªã·ã¼ã¨ãªãã¾ãã
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ssm:StartSession" ], "Resource": [ "arn:aws:ssm:ap-northeast-1::document/AWS-StartPortForwardingSession", "arn:aws:ec2:ap-northeast-1:999999999999:instance/i-0c678d2c038786416" ] }, { "Effect": "Allow", "Action": [ "ssm:TerminateSession" ], "Resource": [ "arn:aws:ssm:*:*:session/${aws:username}-*" ] } ] }
ä¸è¨ã®éããStartSessionã«ã¯å¯¾è±¡ã®ããã¥ã¡ã³ãã ãã§ã¯ãªããä»åRDPã¢ã¯ã»ã¹å¯¾è±¡ã®EC2 Instanceãè¨è¼ãã¾ãããã®Instance IDé¨åã§ã¿ã¼ã²ãããçµããã¨ãå¯è½ã§ããæ¬ããªã·ã¼ã¯ã追å ã® Session Manager ç¨ã®ãµã³ãã« IAM ããªã·ã¼ããåèã«ä½æãã¾ããã
åä½ç¢ºèª
以ä¸ã®ããã«ã³ãã³ããå®è¡ãã¦ãã ãããInstance idã¯ãèªèº«ãæ¥ç¶ããããEC2ã®idã«ãå¤æ´ãã ãããlocalPortNumberã¯ä½¿ãæ £ãã¦ãã33890ã¨ãã¾ãããã8389ã§ã13389ã§ãæ§ãã¾ãããã³ãã³ãå ã® --profile ã«ã¤ãã¦ãåããªãæ¹ã¯ãååä»ããããã¡ã¤ã«ãããåèãã ããã
aws ssm start-session --target i-0c678d2c038786416 --document-name AWS-StartPortForwardingSession --parameters portNumber=3389,localPortNumber=33890 --profile ssmpf
ãã®ã³ãã³ããæ£å¸¸ã«å®è¡ãããã¨ãã³ãã³ãããã³ããä¸ã«ä»¥ä¸ã®ãããªè¡¨è¨ãåºã¾ãã
ãã®ç¶æ ã§ããªã¢ã¼ããã¹ã¯ãããæ¥ç¶ã®ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ãã¾ããããã§ä»¥ä¸ã®éããlocalhost:33890ãã¨Portæå®ã§ãã¼ã«ã«ãã¹ãã«ã¢ã¯ã»ã¹ãã¾ãã
ãã¨ã¯é常éããã¦ã¼ã¶ï¼ãã¹ã¯ã¼ããå ¥ããã¨ã
RDPã§æ¥ç¶ããã¾ãããç²ãæ§ã§ããã
ãªããã³ãã³ãããã³ããä¸ã§ãCtrl+Cããå®è¡ãããã¨ã§ããã¼ããã©ã¯ã¼ãããã£ã³ã»ã«å¯è½ã§ãã以ä¸ã®ãããªãã°ã«ãªãã¾ãã
Starting session with SessionId: ssmpf-0c43a55263e8d099b Port 33890 opened for sessionId ssmpf-0c43a55263e8d099b. Connection accepted for session ssmpf-0c43a55263e8d099b. Terminate signal received, exiting. Exiting session with sessionId: ssmpf-0c43a55263e8d099b.
æ¥ç¶è¦ä»¶ ã¾ã¨ã
ä¸è¨ã«è¨è¼ããå 容ãã¾ã¨ãã¾ãã¨ä»¥ä¸ã®éãã«ãªãã¾ãã
- EC2 Instanceã®æ§ç¯ã¨èµ·å
- AmazonSSMManagedInstanceCoreãä»ä¸ããIAM Roleã®æ°è¦ä½æã¨EC2 Instanceã¸ã®ä»ä¸
- VPC Endpoint 3ã¤ã®ä½æã¨EC2 InstanceããVPC Endpointã¸ã®ã¢ã¯ã»ã¹ã許å¯ããSecurity Groupã®ä»ä¸(Inbound)
- com.amazonaws.region.ssm
- com.amazonaws.region.ec2messages
- com.amazonaws.region.ssmmessages
- EC2 InstanceããVPC Endpointã¸ã®ã¢ã¯ã»ã¹ã許å¯ããSecurity Groupã®ä»ä¸(Outbound)
- EC2 Instanceã®SSM Agentã®ãã¼ã¸ã§ã³ã¢ãããè¡ã(2.3.701.0以ä¸ã¨ãã)
- æ¥ç¶å ã®ã¯ã©ã¤ã¢ã³ãã« AWS-CLI ãã¤ã³ã¹ãã¼ã«ãã(1.16.234以ä¸ã¨ãã)
- æ¥ç¶å ã®ã¯ã©ã¤ã¢ã³ãã« Session Manager Plugin ãã¤ã³ã¹ãã¼ã«ãã(1.1.31.0以ä¸ã¨ãã)
- IAM Userãä½æãCredentialãåå¾ãã
- IAM Userã«ãã¼ããã©ã¯ã¼ãã£ã³ã°ãå¯è½ã¨ãªãããssm:StartSessionã®æ¨©éãä»ä¸ãã
以ä¸ãæ¥ç¶ã«å¿ è¦ã¨ãªãæä½éã®è¨å®ã¨ãªããã¨åãã¾ããåèã«ãªãã¾ããã幸ãã§ãã
ä½ç«¹ é½ä¸ (Yoichi Satake) ã¨ã³ã¸ãã¢ããã°ã®è¨äºä¸è¦§ã¯ã³ãã©
ããã¼ã¸ããµã¼ãã¹é¨æå±ãAWSè³æ ¼å ¨å ã2010å¹´1æããAWSãå©ç¨ãã¦ãã¦ãã¾ãã2021-2022 AWS Ambassadors/2023-2024 Japan AWS Top Engineers/2020-2024 All Certifications EngineersãAWSã®ã³ã¹ãåæ¸ãæé©åãå¾æã¨ãã¦ãã¾ãã