Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automated management operations for Lambda,AWS and Python-create a new EC2 instance

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Recently, I began to learn the automation application of Lambda in AWS from scratch. My operation is mainly based on the version of Python 3.7.The Python module of AWS is called Boto. The specific grammar and concepts are not detailed, let's just look at the specific examples.

First, let's take a look at the first simple operation, how to create a new EC2 instance.

The first step is to create a new lambda function and select author from scratch, where he automatically creates a role of IAM by default and gives access to cloudwatch log

The default permissions are displayed as follows

Here we need to grant permission to create the EC2, so we need to modify the corresponding JSON. We can find the default created Role in the role of IAM, and just add access to ec2:runinstances above the default permissions. Of course, we can also manually create a new role from the beginning.

The following is the updated permission map

Next is the Lambda function. Lambda functions are triggered by events, so there are event and context in the passed parameters.

The first thing in the function is the import module

Next, pass in several environment variables, which are customized directly in the table below the function.

Then in the lambda_handler entry function, create a new EC2 instance directly, passing in parameters. Notice that this function has two formal arguments, event and context, which come with the function. We can't use it here, but we still need to write

These environment variables represent the code of AMI, the type of instance, the name of Key pair and the subnet code of subnet.

Clicking test will remind you of the event that configures test. Here we do not need to pass in any parameters, so choose the simplest hello world template and pass in an empty parameter.

Click test again after saving, and the successful execution will be shown.

Take a look at the EC2 instance and it is true that a new instance has been created!

This example has been created successfully, but I have found it inconvenient to enter scripts in the console. Douzi is more accustomed to using Pycharm under windows when writing Python, so how to use boto3 in Pycharm?

First you need to install boto3

Pip install boto3

Then you need to install a tool called pyboto3, because Pycharm cannot automatically autocomplete boto3 by default. After installing pyboto3, you can prompt the function, but currently only boto3.client is supported.

Pip install pyboto3

Finally, open the settings of Pycharm and add Project Interpreter

I also need to configure credential in order to access my aws account. Download and install

AWS CLI . Then type in cmd

Aws configure

Fill in the corresponding information as prompted.

After the configuration is complete, here is a simple test to see if it works.

Execute it.

Import boto3def getEC2 (): ec2=boto3.client ('ec2') ": type: pyboto3.ec2" instance=ec2.describe_instances () print (instance) if _ _ name__ =' _ main__': getEC2 ()

Get the EC2 information I just created.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report