We see as implements a Python code with Lambda e Boto3 in AWS for start instances in EC2
First, we go in AWS console e digit Lambda. We create a new function, we defining the NAME, the RUNTIME (Python 3.7) e we create a NEW ROLE.
In this post, i show you how send command for start instances by input tag. In this case, we must define the role for allow the lambda to send command at the instances. So, suits us to define the role, so go down with the windows and in the section EXECUTION ROLE click VIEW THE EXECUTIONCODE-ROLE-OUR NAME ROLE.
Click ADD INLINE POLICY
Click JSON, in this case we allow all the access to the instances. So paste this code:
We define the name of the policy and click to CREATE POLICY
Now, return to Lambda for test the code, we must CONFIGURE TEST EVENTS
This windows is so interesting because we can pass preset values (JSON) that we can use as input (obtaining them from the event object). In this example i pass a particolar tag of my instance.
If you want assign a tag to an instance, you can go in INSTANCES, select the instance, click to TAG and ADD TAG
In my code, a use boto3 library and i get in input the value pass in the CONFIGURE TEST EVENTS. With this value, i can obtain the instances id and send a particolar command for start them.
I use the print() for print my result:
We have see how start a instance, but you can fun to get stop,restart,get data by instances. It’s enough read the EC2 section in the Boto3 library.
Let us know if you have solved the problem!