Issue
I have a python script present in my AWS EC2 instance that does some job. I have to trigger that script whenever a new file enters a particular bucket.
My idea was to add a lambda trigger to that bucket which in turns triggers the script present in EC2, but failed to do so.
So how to achieve the solution if according to my plan or is there any other workarounds for this problem?
Solution
I managed it with the help of a blog I found online, which I have lost the link of, but have the code.
import time
import boto3
import paramiko
import os
def lambda_handler(event, context):
ec2 = boto3.resource('ec2', region_name='us-east-1',aws_access_key_id='XXXXXXXXXXXXXXXXXXXX',aws_secret_access_key='XXXXXXXXXXXXXXXXXXXX')
instance_id = 'XXXXXXXXXXXXXXXX'
instance = ec2.Instance(instance_id)
# Start the instance
instance.start()
# Giving some time to start the instance completely
#time.sleep(60)
# Connect to S3, we will use it get the pem key file of your ec2 instance
s3_client = boto3.client('s3',aws_access_key_id='XXXXXXXXXXXXXXXXXXXX',aws_secret_access_key='XXXXXXXXXXXXXXXXXXXX')
# # # Download private key file from secure S3 bucket
# # # and save it inside /tmp/ folder of lambda event
bucket_name = ''
key_name = ''
key_location = ''
s3_client.download_file(bucket_name, key_name, key_location)
# # # # Allowing few seconds for the download to complete
time.sleep(10)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privkey = paramiko.RSAKey.from_private_key_file(key_location)
# # # username is most likely 'ec2-user' or 'root' or 'ubuntu'
# # # depending upon yor ec2 AMI
ssh.connect(instance.private_ip_address,22, username='ubuntu', pkey=privkey)
commands = []
for command in commands:
print("Executing {}".format(command))
stdin , stdout, stderr = ssh.exec_command(command)
stdin.flush()
data = stdout.read().splitlines()
for line in data:
print(line)
ssh.close()
return 'Success'
Now just zip the paramiko library along with it. Will udate the answer if I found the blog again.
Answered By - noswear