I have coded this in AWS Lambda console. I have not used any python editor like sublime. I get this error message. somehow it doesn't like the line opt=int(input("Enter your option: "))
import json
import boto3
import sys
def lambda_handler(event, context):
aws_mgmt_console = boto3.session.Session()
aws_mgmt_ec2 = aws_mgmt_console.resource('ec2')
print('ec2 instances')
while True:
print("This script performs the following actions on ec2 instance")
print("""
1. start
2. stop
3. terminate
4. Exit""")
opt=int(input("Enter your option: "))
if opt==1:
instance_id=input('Enter your EC2 Instance Id: ')
#print(dir(my_req_instance_object))
print("Starting ec2 instance.....")
ec2_con_cli.start_instances(InstanceIds=[instance_id])
elif opt==2:
instance_id=input('Enter your EC2 Instance Id: ')
print("Stopping ec2 instance.....")
ec2_con_cli.stop_instances(InstanceIds=[instance_id])
elif opt==3:
instance_id=input('Enter your EC2 Instance Id: ')
print("Terminating ec2 instance.....")
ec2_con_cli.terminate_instances(InstanceIds=[instance_id])
elif opt==4:
print("Thank you for using this script")
sys.exit()
else:
print("Your option is invalid. Please try once again")
It's impossible to execute Lambda function interactively - you are not able to retrieve user input like this.
However, there are a lot other solutions to pass input into your lambda function - for example, you can:
Lambda URLs - https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html Please be patient while you are creating these URLs, remember to don't use public accessibility.
By event - https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html#python-example