Automate Start-Stop EC2 Instances Using Lambda - Step-by-Step Guide | Start/Stop the EC2 by Lambda

Pubblicato il: 29 dicembre 2024
sul canale di: sagar infomode
214
1

welcome to my youtube channel sagar infomode

#sagarinfomode

i have started this youtube channel to make others to familier with use full technology and current technology


start lambda:
import boto3

def lambda_handler(event, context):
Specify the AWS region
region = 'eu-north-1'
ec2 = boto3.client('ec2', region_name=region)

List of EC2 instance IDs to start
instance_ids = ['i-0bcdc9sjdhfbbdfbdb', 'i-03e9c6mdbfghe']

try:
Start the EC2 instances
response = ec2.start_instances(InstanceIds=instance_ids)
print(f"Starting instances: {instance_ids}")
return {
'statusCode': 200,
'body': f"Successfully started instances: {instance_ids}"
}
except Exception as e:
print(f"Error starting instances: {str(e)}")
return {
'statusCode': 500,
'body': f"Error starting instances: {str(e)}"
}



stop lambda
import boto3

def lambda_handler(event, context):
Specify the AWS region
region = 'eu-north-1'
ec2 = boto3.client('ec2', region_name=region)

List of EC2 instance IDs to stop
instance_ids = ['i-0bcdc9befae4ce9db', 'i-03e9c6cbf537fce01']

try:
Stop the EC2 instances
response = ec2.stop_instances(InstanceIds=instance_ids)
print(f"Stopping instances: {instance_ids}")
return {
'statusCode': 200,
'body': f"Successfully stopped instances: {instance_ids}"
}
except Exception as e:
print(f"Error stopping instances: {str(e)}")
return {
'statusCode': 500,
'body': f"Error stopping instances: {str(e)}"
}


Permissions
AmazonEC2FullAccess
AmazonEventBridgeFullAccess
AmazonSSMAutomationRole
AWSLambda_FullAccess
CloudWatchFullAccess
lambda-ec2-start-policy (user created)
json below
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}

Social Media Links:
Instagram :   / sagar_singh214  
FaceBook :   / sagarsinghroshan  

************************************************
DISCLAIMER:This video is for educational purposes only.Copyright Disclaimer Under Section 107 of the Copyright Act 1976,allowance is made for "fair use" for purposes such as criticism, comment,news reporting, teaching, scholarship and research.Fair use is a use permitted copyright statue that might otherwise be infringing,non-profit, educational or personal use tips the balance in favor of fair use.
if you don't agree with my upload of one your content then simply send me a private message or e-mail and i will delete it immediately

Disclaimer : On Sagar Infomode Channel, We are Providing Online Course Websites With Certificates and Latest Online Internships and Job Updates. The content in this video is strictly for educational and informational purposes only.


In questa pagina del sito puoi guardare il video online Automate Start-Stop EC2 Instances Using Lambda - Step-by-Step Guide | Start/Stop the EC2 by Lambda della durata di ore minuti seconda in buona qualità , che l'utente ha caricato sagar infomode 29 dicembre 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 214 volte e gli è piaciuto 1 spettatori. Buona visione!