File tree 1 file changed +15
-3
lines changed
1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/python
2
2
import time
3
-
3
+ import json
4
4
import boto3
5
5
#from dateutil import tz
6
6
7
7
8
8
def parse_command_id (send_command_output ):
9
9
return send_command_output ['Command' ]['CommandId' ]
10
10
11
+ def fwd (instance ):
12
+ # https://aws.amazon.com/blogs/aws/new-port-forwarding-using-aws-system-manager-sessions-manager/
13
+ #INSTANCE_ID=$(aws ec2 describe-instances --filter "Name=tag:Name,Values=CodeStack/NewsBlogInstance" --query "Reservations[].Instances[?State.Name == 'running'].InstanceId[]" --output text)
14
+ # create the port forwarding tunnel
15
+ prms = {
16
+ "portNumber" :["22" ],
17
+ "localPortNumber" :["2222" ]
18
+ }
19
+ prms_jsn = json .dumps (prms )
20
+ print (f"""aws ssm start-session --target { instance } --document-name AWS-StartPortForwardingSession --parameters '{ prms_jsn } '""" )
21
+
11
22
def main ():
12
23
ec2_client = boto3 .client ('ec2' )
13
24
ssm_client = boto3 .client ('ssm' )
@@ -26,8 +37,9 @@ def main():
26
37
instance_id = instance ['InstanceId' ]
27
38
state = instance ['State' ]['Name' ]
28
39
if state == 'running' :
29
- print (f"Starting command for instance: { instance_id } " )
30
- print (f"aws ssm start-session --target { instance_id } " )
40
+ #print(f"Starting command for instance: {instance_id}")
41
+ #print(f"aws ssm start-session --target {instance_id}")
42
+ fwd (instance_id )
31
43
32
44
if __name__ == "__main__" :
33
45
main ()
You can’t perform that action at this time.
0 commit comments