Cyberithub

How to Create an EC2 Instance Using Lambda Function in AWS

Advertisements

In this article, I will take you through the steps to create an EC2 instance using lambda function in AWS. Lambda function gives lot of flexibility in managing AWS resources and allows you to efficiently use only the resources required to run the business logic. It allows us to add custom code to manage AWS resources using multiple programming languages that includes Python, Java and C++. So it is very developer friendly and can easily be integrated and used from almost anywhere. Here we are going to create an EC2 instance using a simple lambda function written in Python programming language.

 

What is Lambda Function

Lambda Function is a serverless compute service that allows you to run your code in response to certain information and events. It manages underlying resources on your behalf so that you can put all your focus on computation logic instead of managing resources.

 

Features

  • Pay only what you use
  • Provides easy integration with IAM to ensure secure code access
  • Can run code in AWS Locations Globally
  • Easy connection to Shared File System
  • Supports function packaging and deployment as container images
  • Invokes the code only when needed
  • Excellent trust and integration controls
  • Allows us to choose the amount of resources required to run the functions
  • Easy Integration with AWS Lambda Extensions

How to Create an EC2 Instance Using Lambda Function in AWS

How to Create an EC2 Instance Using Lambda Function in AWS

Also Read: How to Create a Cloudwatch Rule Using AWS Management Console

Step 1: Prerequisites

a) You should have a valid AWS Account.

b) You should have sufficient IAM access to create resources in your account.

 

Step 2: Login to AWS Management Console

First you need to go to AWS Management Console login page and provide your credentials. Once given, Click on Sign In.

 

Step 3: Go to Lambda Dashboard

To open Lambda dashboard, you need to search lambda in the search box and when it pops up, tap on it to open.

How to Create an EC2 Instance Using Lambda Function in AWS 2

You will be redirected to AWS Lambda Dashboard as shown below.

How to Create an EC2 Instance Using Lambda Function in AWS 3

 

Step 4: Create Function

To create Lambda function, you need to go to Lambda -> Functions and click on Create Function as shown below.

How to Create an EC2 Instance Using Lambda Function in AWS 4

Then provide a valid Function name, Runtime info and select the appropriate architecture.

How to Create an EC2 Instance Using Lambda Function in AWS 5

Further scroll down a bit and select Create a new role with basic lambda permissions option as shown below. Finally Click on Create Function.

How to Create an EC2 Instance Using Lambda Function in AWS 6

You will see your Lambda function created like as shown below. In our case, we have created a lambda function named TestLambdaFunction.

How to Create an EC2 Instance Using Lambda Function in AWS 7

After creating lambda function successfully, you can now change its code and configuration. You can remove the default code created under lambda_function.py.

How to Create an EC2 Instance Using Lambda Function in AWS 8

You can then put the below code in lamda_function.py python file.

How to Create an EC2 Instance Using Lambda Function in AWS 9

To set the value of AMI in environment variable, you need to select your machine image from AMIs list and copy the image name. Since here we are going to use Amazon Linux 2 AMI 64-bit so we will copy the id ami-033b95fb8079dc481.

How to Create an EC2 Instance Using Lambda Function in AWS 10

 

Step 5: Verify IAM Role

To verify IAM role, you need to search IAM in the search box as shown below. You will see IAM service in the results. Click on it to Open.

How to Create an EC2 Instance Using Lambda Function in AWS 11

Under IAM on the left side side, you need to click on Roles under Access Management as highlighted below.

How to Create an EC2 Instance Using Lambda Function in AWS 12

You will see that the role is created as shown below.

How to Create an EC2 Instance Using Lambda Function in AWS 13

 

Step 6: Verify Policy

You can then verify the policies by going to IAM -> Policies. Then click on Permissions to check all the privileges required to create an EC2 instance.

How to Create an EC2 Instance Using Lambda Function in AWS 14

 

Step 7: Edit Policy

Then click on Edit policy, update action to "ec2:RunInstances" and Effect to "Allow" as shown below.

How to Create an EC2 Instance Using Lambda Function in AWS 15

Review this policy and then save the changes by clicking on Save Changes.

How to Create an EC2 Instance Using Lambda Function in AWS 16

Now click on Configuration Tab and then on Edit to add environment variables for your function code.

How to Create an EC2 Instance Using Lambda Function in AWS 17

Here tap on Add environment variable.

How to Create an EC2 Instance Using Lambda Function in AWS 18

You need to define the below environment variable which you have used in your function code and its values in the form of key-value pairs as shown below. You can define the value as per your requirement and does not necessarily needs to be same as below. Then click on Save.

How to Create an EC2 Instance Using Lambda Function in AWS 19

You Lambda function will be successfully updated as shown below.

How to Create an EC2 Instance Using Lambda Function in AWS 20

 

Step 8: Test Your Function

Now that function is created, it is time to test your function. For that you need to go to Test tab as shown below and then click on Test.

How to Create an EC2 Instance Using Lambda Function in AWS 21

If everything goes well then you should see below succeeded message in the Execution result. This also means that your EC2 instance is created successfully.

How to Create an EC2 Instance Using Lambda Function in AWS 22

 

Step 9: Verify EC2 Instance

To verify the successful creation of EC2 instance, go to Services->EC2 and check for Running Instances. You will see an EC2 instance created by lambda function.

Leave a Comment