Skip to content

Commit d7f97b7

Browse files
Add files via upload
1 parent 572a2a9 commit d7f97b7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

code_lambda.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import boto3
2+
import paramiko
3+
def code_lambda(event, context):
4+
host = 0
5+
s3_client = boto3.client('s3')
6+
ec2 = boto3.resource('ec2')
7+
running_instances = ec2.instances.filter(Filters=[{'Name': 'instance-id','Values': ['Add your instance id']}])
8+
for instance in running_instances:
9+
host = instance.public_ip_address
10+
11+
s3_client.download_file('Add your bucket name','key/Add your key name.pem', '/tmp/Add your key name.pem')
12+
13+
k = paramiko.RSAKey.from_private_key_file("/tmp/Add your key name.pem")
14+
c = paramiko.SSHClient()
15+
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
16+
17+
print("Connecting to " + host)
18+
c.connect( hostname = host, username = "ubuntu", pkey = k )
19+
print("Connected to " + host)
20+
commands = ["/home/ubuntu/demo.sh"]
21+
for command in commands:
22+
print("Executing {}".format(command))
23+
stdin , stdout, stderr = c.exec_command(command)
24+
print(stdout.read())
25+
print(stderr.read())
26+
return
27+
{
28+
'message' : "Done"
29+
}
30+

0 commit comments

Comments
 (0)