In our previous blog, we have discussed serverless architecture and AWS lambda functions along with their benefits.
Here in this blog, we will discuss how we can create a basic lambda function in AWS.
The lambda function that we are going to use is just a simple Lambda function that will show all the EC2 instances running in your AWS account whether they are in a running state or not.
We are not using any kind of filters here but if you want to use filters then you can add commands to your code accordingly.
This function will only list down all the ec2 instances irrespective of in which AWS region they are.
Here is the code –
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
instances = ec2.instances.filter()
for instance in instances:
Runtime – Python 3.7
And here are the EC2 instances that we are going to list using the Lambda Function –
We are using python in our code. But, you can create a lambda function using any of the following Supported Languages –
Lets create a Lambda Function in AWS
Step 1 – Search for Lambda
Now search for Lambda service under your AWS management console.
Step 2 – Choose AWS region
Then, select the AWS region where you want to create the lambda function from the following available regions or where your server is already running.
Step 3 – Choose ‘Create function’
Under AWS lambda console, choose ‘create function’. You will see this page if you do not have any lambda function created else you will see all your existing lambda functions here.
Step 4 – Choose ‘Use a Blueprint’
Choose any of the following to create a function, we have chosen ‘Use a Blueprint’.
Step 5 – Choose ‘Hello-word’ Blueprint
Under Blueprints, search for Hello-word and choose ‘Hello-word-python’ Blueprint.
Step 6 – Enter Basic details
Enter Function name – mylambda
Runtime – Python 3.7
Under execution role – choose ‘create a new role with basic Lambda permissions’
Step 7 – Click on ‘create function’
After entering all the basic details, click on ‘create function’ at the right corner downwards.
Step 8 – Function created Successfully
Now your function is created successfully as shown in the green color notification bar.
Step 9- Run Python code
Now you can put your code into the console. After pushing the code, you need to Deploy your code after making any kind of changes in it. After deploying the code, you will have to Test it to show the output of the code.
Step 10 – Permission Error
At the runtime, an ‘Unauthorized operation’ error occurred that shows this lambda function is not authorized to access the EC2 instances.
By default, lambda does not have any permission to access any of the AWS resources until we grant permissions to it.
Step 11 – Create an IAM role to run Lambda Function
Now, we need to create an IAM role that will grant Read-only access to our lambda function so that this function can only Read the EC2 instances in the AWS account.
(i) Navigate to IAM Under AWS Management Console and then choose ‘Roles’ from the left-hand side options.
(ii) From the top right corner, click on ‘Create Role’
(iii) Under AWS services, choose Lambda to grant permission to our lambda function. Then click on ‘Next: Permission’.
(iv) Now under policies, we need to choose the appropriate policy that we want to attach with the created role.
As we have mentioned earlier that this lambda function will only list down all the EC2 instances of the respective AWS account. In this case, we will choose Read-only permission for our ec2 instances.
For that, we need to search for the ‘ec2readonly’ policy. Then choose that policy.
(v) Enter required details and review created Role.
(vi) Role has been created successfully, you can check by searching using your role name – ‘lambdaec2readonly’.
Step 12 – Change Role name under created Lambda Function
Again navigate LAMBDA under AWS Management close. Click on your created function and look for ‘Configurations’ to change the Role name. Click on ‘Edit’ for this.
Step 13 – Change IAM role
In Basic settings, under the Execution role please search for the role that you have created i.e, lambdaec2readonly. Choose this role and then click on ‘Save’.
You have successfully created the IAM role required to execute the lambda code.
Step 14 – Test the code Again
Now, test the code again to check whether we have got the IAM role permission or not.
Your code has been executed properly. You can check the status of the execution i.e, SUCCEEDED.
Step 15 – Final/Execution Result
Your code has been executed successfully. The results are those EC2 instances that are available in our AWS account. You can cross these instances as we have already mentioned the instances IDs in the initial phase of this installation.
Hope you are clear with each and every step of the installation guide as we tried to give you a simples way to create a LAMBDA Function in AWS.
This is the first step in learning how to launch applications without the requirement for server provisioning or management. Lambda scales your applications automatically by executing your code in response to each trigger, adjusting to the size of your workloads.
This was a basic function that we have used to execute lambda. You can perform more actions by manipulating your code.
In the future blogs, we will be sharing more updates with you about the latest technologies and will keep updating you.
Thank You for reading this Blog!
For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at https://webkul.uvdesk.com/en/.