Lambda, AWS and Python automated management operations (1)

Lambda recently started learning from scratch using automation in AWS inside. My operation is based mainly on the version of Python 3.7, AWS Python module, called Boto. The syntax and concepts do not go into specific examples of direct view.

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

First, create a new lambda function, select the author from scratch, where he will automatically create a default role of an IAM, and give access to cloudwatch log in

Lambda, AWS and Python automated management operations (1)

The default permissions are shown below
Lambda, AWS and Python automated management operations (1)

Here we need to grant permission to create EC2, so the need to modify the corresponding JSON. We can find this Role created by default in the IAM's role which, added to the default permissions ec2 above: runinstances access it. Of course, we can start to create a new role on manually.

Lambda, AWS and Python automated management operations (1)

The following is a chart of authority after update
Lambda, AWS and Python automated management operations (1)

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

Which is introduced into the first function module
next incoming several environment variables, these environment variables are defined in the function directly from the following table which
is then lambda_handler function inside the entrance, to create a new instance of EC2 directly imported parameters . Note that this function has two formal parameters event and context, this is a function built-in, where we do not have access, but still need to write on

Lambda, AWS and Python automated management operations (1)

These environment variables representing the AMI code, instance type, Key pair name and subnet subnet Code

Lambda, AWS and Python automated management operations (1)

Click test, will remind event configuration test, where we do not need to pass any arguments, so choose the simplest hello world template, passing a null parameter on the line

Lambda, AWS and Python automated management operations (1)

Click Save after test again, it will show the successful implementation

Lambda, AWS and Python automated management operations (1)

Look EC2 instances, indeed a new instance has been created!
Lambda, AWS and Python automated management operations (1)

Guess you like

Origin blog.51cto.com/beanxyz/2438381