Table of Contents
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
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.
You will be redirected to AWS Lambda
Dashboard as shown below.
Step 4: Create Function
To create Lambda function, you need to go to Lambda
-> Functions
and click on Create Function
as shown below.
Then provide a valid Function name, Runtime info and select the appropriate architecture.
Further scroll down a bit and select Create a new role with basic lambda permissions option as shown below. Finally Click on Create Function
.
You will see your Lambda function created like as shown below. In our case, we have created a lambda function named TestLambdaFunction
.
After creating lambda function successfully, you can now change its code and configuration. You can remove the default code created under lambda_function.py
.
You can then put the below code in lamda_function.py
python file.
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
.
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.
Under IAM on the left side side, you need to click on Roles
under Access Management
as highlighted below.
You will see that the role is created as shown below.
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.
Step 7: Edit Policy
Then click on Edit policy
, update action
to "ec2:RunInstances"
and Effect
to "Allow"
as shown below.
Review this policy and then save the changes by clicking on Save Changes
.
Now click on Configuration
Tab and then on Edit
to add environment variables for your function code.
Here tap on Add environment variable
.
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
.
You Lambda function will be successfully updated as shown below.
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
.
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.
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.