I want to allow users to start an EC2 instance only when it is needed.
So I created a Lambda function to do just that:
import boto3
def lambda_handler(event, context):
ec2 = boto3.resource('ec2', region_name='eu-central-1')
return ec2.instances.filter(InstanceIds=['i-abc123']).start()
I've also added the following IAM permissions:
{
"Effect": "Allow",
"Action": [
"ec2:StartInstances"
],
"Resource": "arn:aws:ec2:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances"
],
"Resource": "*"
}
Problem is that when I execute the Lambda I get timed out.
BTW running the exact same code from an EC2 within the same VPC and same permissions, returns immediately.
Any idea?