In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
The previous article has been configured to add boto3 and pyboto3 to PyCharm, so you can write scripts directly in your own PyCharm.
Here is an example of traversing all region to find EC2, turn it off if the state is on, or vice versa, turn it on if it is off.
Import boto3def lambda_handler (event, context): # Get list of regions ec2_client = boto3.client ('ec2') regions = [region [' RegionName'] for region in ec2_client.describe_regions () ['Regions']] # Iterate over each region for region in regions: ec2 = boto3.resource (' ec2', region_name=region) print ("Region:" Region) # Get only running instances instances = ec2.instances.filter (Filters= [{'Name':' instance-state-name', 'Values': [' running']}]) # Stop the instances for instance in instances: instance.stop () print ('Stopped instance:' Instance.id) # instances = ec2.instances.filter (# Filters= [{'Name':' instance-state-name', # 'Values': [' stopped']}]) # # for instance in instances: # instance.start () # print ('Start instance:' Instance.id) if _ _ name__ = ='_ _ main__': lambda_handler (0jin0)
It's a job to execute it.
C:\ Users\ yuan\ PycharmProjects\ aws\ venv\ Scripts\ python.exe C:/Users/yuan/PycharmProjects/aws/StopInstance.pyRegion: eu-north-1Region: ap-south-1Region: eu-west-3Region: eu-west-2Region: eu-west-1Region: ap-northeast-2Region: ap-northeast-1Region: sa-east-1Region: ca-central-1Region: ap-southeast-1Region: ap-southeast-2Stopped instance: i-0bb70cc9666ce2af3Region: eu-central-1Region: us-east-1Stopped instance: I -00e9dc7c254dbe497Region: us-east-2Region: us-west-1Region: us-west-2
Then we create a new function in the Lambda of aws. Here I have customized a role to make sure that this role can power on and off the ec2.
The permissions for IAM are as follows:
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"], "Resource": "arn:aws:logs:*"}, {"Effect": "Allow" "Action": ["ec2:DescribeInstances", "ec2:DescribeRegions", "ec2:StartInstances", "ec2:StopInstances"], "Resource": "*"}
Copy the function up
Next, add a new rule to the cloudwatch
Create wizard, select schedule here, use the expression of cron here, notice that it is the time of GMT, so you need to convert yourself to the local time.
After writing it, he will have a friendly prompt interface.
Complete creation
When you go back to the interface of Lambda, you can see that his trigger has an extra CloudWatch Events.
After waiting for execution, you can view the log
You can also confirm that the EC2 service is indeed turned off.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.