I have a fairly simple Lambda that I'm going to use to start and stop EC2 instances on a cron schedule. In it, I'm checking to see if the instance is already running or not.
# get instance
instance = ec2.Instance(instance_id)
# return if instance already running
if instance.state['Name'] in ["pending", "running"]:
return True
When I execute the Lambda, I get the following error:
An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
Here is a snippet of my policy, copied directly from IAM and edited to remove account info:
{
"Sid": "",
Effect": "Allow",
Action": [
"ec2:StopInstances",
"ec2:StartInstances",
"ec2:DescribeInstances"
],
"Resource": "arn:aws:ec2:us-east-1:############:instance/i-#############"
},
For what reason would I be seeing this error under these circumstances? I've confirmed that my code that starts the instance works, I only see an error when I attempt to get the status of the instance.