2
votes

I am using AWS CLI to create AWS spot instance but each time I am getting the below error. "An error occurred (UnauthorizedOperation) when calling the RequestSpotInstances operation: You are not authorized to perform this operation."

Note :- I am able to create spot instance from EC2 console(GUI) successfully. below mention is aws cli

aws ec2 request-spot-instances --spot-price "0.003" --instance-count 1 --type "persistent" --launch-specification file://param.json --instance-interruption-behavior "stop" --profile ""

content for param.json

{
  "ImageId": "ami-0123456",
  "UserData":"file:://my_script.txt"
  "KeyName": "gistdev_default_rsa",
  "SecurityGroupIds": [ "sg-0123456" ],
  "InstanceType": "t2.medium",
  "Placement": {
    "AvailabilityZone": "us-west-2a"
  },

  "NetworkInterfaces": [
    {
      "DeviceIndex": 0,
      "SubnetId": "subnet-0123456",
      "Groups": [ "sg-0123456" ],
      "AssociatePublicIpAddress": true
    }
  ]
}

I have not specified "IamInstanceProfile" parameter in json file. Is this mandatory to have it?

~Ashish

3
Using your own admin access to do something inside AWS console is totally different than using an API/SDK user access. An API access key doesn't have all the rights to do things compare to your primary "superuser".mootmoot

3 Answers

2
votes

Just because you can create spot instances using console (GUI) does not mean you can create them using CLI. What can go wrong?

  • Is API/SDK access enabled for the user?
  • How is the credentials supplied to the CLI? using access/secret or using metadata server (IAMProfile)
  • Is the credentials supplied to the CLI different from the one for the IAM user using the console/GUI? (most likely reason)
  • Is the CLI getting credentials in some other way? See: Configuration Settings and Precedence
0
votes

There is missing permission to iam user of AmazonEC2SpotFleetRole .Try after attaching AWS managed AmazonEC2SpotFleetRole policy to you iam user.

There is no mandatory of add "IamInstanceProfile" in json file while creating spot-instance . IamInstanceProfile is a Role that can be accessed via ec2 entity as a trusted enitity .

0
votes

When you create a new user, you have the option to grant them "Programmatic access" and/or "AWS Managed Console access" New User Access Options

You need to be sure "Programmatic access" is enabled for users who need to use the CLI.