0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows PowerShell スクリプトで AWS 上の Linux 環境に SSM 接続

Posted at

はじめに

  • Windows PowerShell スクリプトと AWS CLI 使用して、ワンクリックで以下を実現します
    • Namet タグ元に、対象 EC2 インスタンスを起動
    • EC2 起動中は待機(10秒ごとに確認)
    • 起動ステータスが 2/2 となったら、Session Manager 経由でログイン

前提

  • 作業端末:Windows 10 Pro(他のバージョンでも大丈夫なはず)
  • 接続先:Linux環境

スクリプト

下記スクリプトを ps1 ファイルとして保存し、PowerShell で実行します。

## 初期設定
${profile} = "プロファイル名を指定"
${InstanceName} = "接続先 EC2 インスタンスの Name タグを指定"

## 指定インスタンスID取得
${InstanceId} = aws ec2 describe-instances `
    --profile ${profile} `
    --filter "Name=tag:Name,Values=${InstanceName}" `
    --query "Reservations[].Instances[].InstanceId" `
    --output text

## 指定インスタンス起動
aws ec2 start-instances `
    --profile ${profile} `
    --instance-ids ${InstanceId} > $null

Write-Host "Start Instances:" ${InstanceName} "/" ${InstanceId}

## 起動確認
do {
    Write-Host "checking status..."
    Start-Sleep -s 10
    ${SystemStatus} = aws ec2 describe-instance-status `
        --profile ${profile} `
        --instance-ids ${InstanceId} `
        --query "InstanceStatuses[].SystemStatus.Details[].Status" `
        --output text
    ${InstanceStatus} = aws ec2 describe-instance-status `
        --profile ${profile} `
        --instance-ids ${InstanceId} `
        --query "InstanceStatuses[].InstanceStatus.Details[].Status" `
        --output text
    Write-Host "  SystemStatus  :" ${SystemStatus}
    Write-Host "  InstanceStatus:" ${InstanceStatus}
} while ( (${InstanceStatus} -eq "initializing") -or (${SystemStatus} -eq "initializing") )

## 画面初期化
clear

## SSMで接続
aws ssm start-session `
    --profile ${profile} `
    --target ${InstanceId}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?