In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use AWS Serverless service Lambda to achieve timing switch". In daily operation, I believe many people have doubts about how to use AWS Serverless service Lambda to achieve timing switch. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use AWS Serverless service Lambda to achieve timing switch". Next, please follow the editor to study!
What is AWS Lambda
AWS Lambda is a computing service that allows you to run code without preconfiguring or managing the server. You only have to pay for the computing time consumed-there is no charge when the code is not running. With AWS Lambda, you can run code for almost any type of application or back-end service without performing any administration. AWS Lambda runs your code on a highly available computing infrastructure and performs all the management of computing resources, including server and operating system maintenance, capacity provisioning and automatic expansion, code monitoring and recording. Languages such as Node.js, Java, C# and Python are currently supported.
Second, scene description
In many cases, for testing and development examples, in order to save money, we usually choose to set the timing switch. There are many ways to realize timing switch:
1. AWSCLI + Crontab
2. AWS Lambda + Cloudwatch Event
3. Write programs with AWS API (supported languages such as Centro, Python, Java, etc.)
4. Software developed by a third party
Today we mainly introduce the use of AWS Lambda and Cloudwatch Event to achieve timing switch.
Third, principle analysis
In this case, we mainly create two AWS Lambda functions (based on the Python function) to power on and off regularly, and create a CloudWatch Events to trigger it to power off every night and start in the morning.
IV. AWS Lambda configuration
1. Open the AWS Lambda console and select "create AWS Lambda function" (you need to choose to start using it immediately when using AWS Lambda for the first time). In the select function interface, select "blank function".
two。 Configure the trigger interface and select the next step directly.
3. To configure the function interface, you need to configure relevant Lambda function information:
Name: "StopEC2Instances"
Description: "stops EC2 instances every day at night".
Language: Python 2.7.
4. In the Lambda function code edit box, copy the following information:
Import boto3
# Enter the region your instances are in, e.g. 'us-east-1'
Region = 'XX-XXXXX-X'
# Enter your instances here: ex. ['XmurXXXXXXXXXXXXXXXXXXXX']
Instances = ['Xcopyright XXXXXXXXXX']
Def lambda_handler (event, context):
Ec2 = boto3.client ('ec2', region_name=region)
Ec2.stop_instances (InstanceIds=instances)
Print 'stopped your instances:' + str (instances)
5. Lambda function handlers and roles. Select "Custom role" and a new interface will pop up.
6. In the create IAM role interface, the role selects "Custom IAM role", role name, write "lambda_start_stop_ec2" or other
7. In the view policy document, main, copy the following policy:
{
"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: "*"
}
]
}
8. Choose to allow.
9. Just click "next" to create a function.
11. Repeat the above steps to create a function to start the instance, as follows:
Import boto3
# Enter the region your instances are in, e.g. 'us-east-1'
Region = 'XX-XXXXX-X'
Instances = ['Xcopyright XXXXXXXXXX']
Def lambda_handler (event, context):
Ec2 = boto3.client ('ec2', region_name=region)
Ec2.start_instances (InstanceIds=instances)
Print 'started your instances:' + str (instances)
Remarks: fill in the name and description yourself, and select the role you created before.
5. Configure Cloudwatch Event
Next, we need to create a CloudWatch event to trigger our AWS Lambda function.
1. Enter the Cloudwatch console.
two。 Select events, rules, and click "create Rule"
3. Event source: select "Plan", Crontab expression: 00 23 * *? *
That is, to set the instance to shut down at 23:00 every night. For more information about the format of the expression, please refer to: https://docs.aws.amazon.com/zh_cn/console/cloudwatch/events/createrule
4. In the target bar, select "add destination", select "Lambda function" for the target type, select the StopEC2Instances function we created, and click the configuration details. For more information, please see:
File name: "StopEC2Instances"
Description: "stops EC2 instances every day at night".
Shape: enabled
5. Click to create a rule.
Follow the same steps to create another rule in opening the configuration of the instance.
At this point, the study on "how to use the AWS Serverless service Lambda to achieve timing switch" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.