1
- #! /usr/bin/env sh
1
+ # !/usr/bin/env pwsh
2
2
# ####### Source ################################################################
3
3
#
4
4
# https://github.com/qoomon/aws-ssm-ec2-proxy-command
13
13
# Add following SSH Config Entry to ~/.ssh/config
14
14
# host i-* mi-*
15
15
# IdentityFile ~/.ssh/id_rsa
16
- # ProxyCommand powershell .exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub
16
+ # ProxyCommand powershell.exe ~/.ssh/aws-ssm-ec2-proxy-command.ps1 %h %r %p ~/.ssh/id_rsa.pub
17
17
# StrictHostKeyChecking no
18
18
#
19
19
# Ensure SSM Permissions for Target Instance Profile
20
20
# https://docs.aws.amazon.com/systems-manager/latest/userguide/setup-instance-profile.html
21
21
#
22
22
# Open SSH Connection
23
23
# ssh <INSTANCE_USER>@<INSTANCE_ID>
24
- #
24
+ #
25
25
# Ensure AWS CLI environment variables are set properly
26
26
# e.g. AWS_PROFILE='default' ssh ec2-user@i-xxxxxxxxxxxxxxxx
27
27
#
@@ -49,31 +49,30 @@ if ($splitted_instance.Length -gt 1)
49
49
$env: AWS_DEFAULT_REGION = $splitted_instance [1 ]
50
50
}
51
51
52
- $authorized_key = " $ssh_public_key ssm-session "
53
- $script = @"
54
- \"
55
- mkdir -p ~$ssh_user /.ssh && cd ~$ssh_user /.ssh || exit 1
52
+ Write-Output " Add public key $ssh_public_key_path for $ssh_user at instance $ec2_instance_id for $ssh_public_key_timeout seconds "
53
+ $addPublicKeyScript = @"
54
+ \"
55
+ mkdir -p ~$ssh_user /.ssh && cd ~$ssh_user /.ssh || exit 1
56
56
57
- echo '$authorized_key ' >> authorized_keys
57
+ authorized_key='$ssh_public_key ssm-session'
58
+ echo \\\"`$ authorized_key\\\" >> authorized_keys
58
59
59
- sleep $ssh_public_key_timeout
60
+ sleep $ssh_public_key_timeout
60
61
61
- grep -v -F ' $authorized_key ' authorized_keys > .authorized_keys
62
- mv .authorized_keys authorized_keys
63
- \"
62
+ grep -v -F \\\" ` $ authorized_key\\\" authorized_keys > .authorized_keys
63
+ mv .authorized_keys authorized_keys
64
+ \"
64
65
"@
65
-
66
- Write-Output " Add public key $ssh_public_key_path for $ssh_user at instance $ec2_instance_id for $ssh_public_key_timeout seconds"
67
66
aws ssm send-command `
68
67
-- instance- ids " $ec2_instance_id " `
69
68
-- document- name ' AWS-RunShellScript' `
70
69
-- comment " Add an SSH public key to authorized_keys for $ssh_public_key_timeout seconds" `
71
- --parameters commands=" $script "
70
+ -- parameters commands= " $addPublicKeyScript "
72
71
if ($LASTEXITCODE -ne 0 ) { Write-Error " Failed to add public key with error $output " }
73
72
74
73
Write-Output " Start ssm session to instance $ec2_instance_id "
75
74
aws ssm start-session `
76
75
-- target " $ec2_instance_id " `
77
76
-- document- name ' AWS-StartSSHSession' `
78
77
-- parameters " portNumber=$ssh_port "
79
- if($LASTEXITCODE -ne 0) { Write-Error " Failed to start ssm session to instance $output " }
78
+ if ($LASTEXITCODE -ne 0 ) { Write-Error " Failed to start ssm session to instance $output " }
0 commit comments