Get the App
SLTechnology News&Howtos  ›  Servers  › 

Automatic management operation of Lambda,AWS and Python-automatic power on and off

Shulou Source: shulou.com Published: 2022-06-02 04:57:59 10月02日 Update

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.

Tags: Function time permission state interface next example wizard copy log time script expression trigger bean previous work prompt service select Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Huawei vpn Redmi