Arindam Bose
Arindam Bose

Reputation: 197

Starting an EC2 instance via AWS Lambda

I am trying to create a lambda function on AWS to start and stop an EC2 instance, but it's giving the following error

botocore.exceptions.ClientError: An error occurred (UnauthorizedOperation) when calling the StartInstances operation: You are not authorized to perform this operation.

What are the steps which should I take to stop this error? I am not a developer, just trying to run some ML model via EC2. I read something on creating an IAM role, which I did with full EC2 access, but I am not sure how to use the same in the lambda function. Can anyone guide me on the same?

Upvotes: 1

Views: 464

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269480

When creating an AWS Lambda function, you assign an IAM Role to the function.

When the function makes an AWS API call, it uses the permissions provided in the IAM Role to call other AWS services.

Therefore, you should:

  • Create an IAM Role with sufficient permissions for the Lambda function to work (but preferably not too much permission, since it could create security holes)
  • Assign the IAM Role to the Lambda function

Your function would need, at minimum, the ec2:StartInstances permission. It's likely that it will need other permissions too, depending upon the settings used when launching an instance.

Upvotes: 8

Related Questions