Start/Stop EC2 using Lambda function

41 views Asked by At

I am configuring lambda function to Start/Stop Ec2 instance from the aws doc https://repost.aws/knowledge-center/start-stop-lambda-eventbridge but i am geeting below error while test the code.

"errorMessage": "Could not connect to the endpoint URL: "https://ec2.us-east-2c.amazonaws.com/""

import boto3   
region = 'us-east-2c'
instances = ['i-081929e311352ba0c']   
ec2 = boto3.client('ec2', region_name=region)   
def lambda_handler(event, context):    
    ec2.stop_instances(InstanceIds=instances)    
    print('stopped your instances: ' + str(instances))

I am not able to find the doc related to the issue. Please help what i am doing wrong.

1

There are 1 answers

1
Oyinlade Demola On

The error message you're encountering suggests there's an issue with the endpoint URL. The region name you provided seems incorrect. AWS regions typically follow the format us-east-2, us-east-2c is not a valid region name.

import boto3

region = 'us-east-2'  
instances = ['i-081929e311352ba0c']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('Stopped your instances: ' + str(instances))