0

I tried to lookup for answer or similar behavior at various site and I dont see anyone facing this issue. That indicates that either I am not doing something correctly or I havent looked much. If you believe this is duplicate question then please point me to question where I can find answer. Thank you.

I am using EC2 Ubuntu t2.micro machine.

I have a script which checks instance "Scale in Protection Bit" and if it is not set then it terminates it.

Script Used: python script

Ways tried: update-rc.d defaults rc.local

Symptoms: If I run the script from console it works fine however, if I reboot the system, then line which checks executes "aws autoscaling describe-auto-scaling-instances --instance-id consistently fails

Code Snippet:

try: myInstanceID = subprocess.check_output(["ec2metadata","--instance-id"]) myInstanceID = myInstanceID.strip("\n") print myInstanceID except: print "Still Booting" continue

   #result = subprocess.check_output(["/usr/local/bin/aws","autoscaling","describe-auto-scaling-instances","--instance-id",myInstanceID])
   p = subprocess.Popen(["/usr/local/bin/aws","autoscaling","describe-auto-scaling-instances","--instance-id",myInstanceID],stdout=subprocess.PIPE)
   result = p.communicate()[0]
   #try:
   #   #result = subprocess.check_output(["aws','autoscaling','describe-auto-scaling-instances','--instance-id',myInstanceID]) 
   #   result = subprocess.check_output(["/usr/local/bin/aws","autoscaling","describe-auto-scaling-instances","--instance-id",myInstanceID]) 
   #except:
   #   #print e.output
   #   print "ERROR"
   #   continue

Tried commented lines also, but same result.

Design
  • 1
  • Does your instance have a role with permissions to run than command? – EEAA Aug 01 '16 at 12:09
  • 1
    Side note: if you are scripting in python to interface with Amazon's AWS APIs, I'd suggest you look at `import boto3` rather than invoking `aws` (which is just a shell interface to `botocore`). – user4556274 Aug 01 '16 at 12:37
  • @EEAA yes it does have all the permissions. Thanks for the comment. – Design Aug 04 '16 at 11:19
  • @user4556274 Yes I figured that out. One of my friend suggested that. Thanks for the comment. – Design Aug 04 '16 at 11:20

1 Answers1

0

Reason for failure: When I create a script that is in user space and when I do "aws configure" it happens for the user ubuntu after system completes boot process. However, when start scripts runs that runs under root and hence SU (Super User) space and I havent configured it to have aws credentials.

Solution: did "sudo su" to go to super user mode and then did "aws configure" to provide all aws credentials. And it works as start up scripts can find all aws credentials.

Design
  • 1