Resources used
IAM | EC2| Lambda | API Gateway
Steps:
Step 1: Create an IAM role with Admin permissions
Step 2: Create an EC2 instance
Step 3: Create the Lambda Function (start ec2)
Step 4: Create the Lambda Function (stop ec2)
Flow Diagram
In this scenario, when a user initiates a request to start or stop an EC2 instance through API-invoked URLs, the request is forwarded to a Lambda function. The Lambda function, based on the user’s request, is designed to either start or stop the specified EC2 instance. It then processes the request, communicates with the AWS services, and acts accordingly.
Subsequently, the Lambda function returns a response to the user, indicating whether the requested action was successfully executed or not. This serverless architecture streamlines the process of managing EC2 instances through a straightforward API, leveraging the flexibility and scalability of Lambda functions to handle user requests for starting or stopping EC2 instances with minimal infrastructure management.
Lambda:
AWS Lambda is a serverless computing service that lets you run code without provisioning or managing servers. Lambda runs your code in response to events and automatically manages the computing resources required by that code. This means that you don’t have to worry about provisioning or managing servers, and you only pay for the time that your code runs.
API Gateway:
An API gateway is a software application that sits between clients and servers and provides a single entry point for accessing and managing APIs. It acts as an intermediary, routing requests to the appropriate backend services and managing the flow of data between clients and servers. API gateways play a crucial role in modern web and mobile applications, enabling developers to build and manage APIs in a secure, scalable, and efficient manner.
Step 1: Create an IAM role with Admin permissions
IAM >> Roles >> Create role >> Select AWS Services — Lambda service — Next >> select Administrative Access — Next >> Provide name to the role(lambdarole) >> Create Role
Step 2: Create an EC2 instance
Services >> EC2 >> instances >> launch instance (Prefer free-tier)
Step 3: Create the Lambda Function (Start EC2)
1)All services >> Lambda >> functions >> create
Here use Existing role choose the one which was created in step 1: In this case it is lambdarole as shown in snapshot.
2)Goto code section of Lambda function created
Paste the below code, deploy and test
import boto3
def lambda_handler(event, context):
instance_id = 'YourInstanceID' #Replace with your instanceID
ec2 = boto3.client('ec2')
ec2.start_instances(InstanceIds=[instance_id])
return {
'statusCode': 200,
'body': 'Instance started successfully.'
}
Step 4: Create the Lambda Function (Stop EC2)
1)All services >> Lambda >> functions >> create
Here use Existing role choose the one which was created in step 1: In this case it is lambdarole as shown in snapshot.
2)Goto code section of Lambda function created
Paste the below code, deploy and test
import boto3
def lambda_handler(event, context):
instance_id = 'YourInstanceID' #Replace with your instanceID
ec2 = boto3.client('ec2')
ec2.stop_instances(InstanceIds=[instance_id])
return {
'statusCode': 200,
'body': 'Instance stopped successfully.'
}
All services >> API gateway >> Rest API >> API-NAME >>Create
1) Then from created API- we need to create a method for POST to invoke start lambda function
Select API >> Create method with startec2 lambda function
2) Create a method for POST to invoke stop lambda function
Then from the created api- we need to create a new resource and create a method there, since we can only use same API command only once within resource
Select API >> Create resource >> Create method with stopec2 lambda function
Then browse the Stages section of the API and grab the invoke URL for “POST” method of start and stop resources. Please find below snapshot:
Open Terminal – Run aws-configure and then test the API.
Run the below commands in terminal
curl -X POST https://kcdpqfvoj9.execute-api.us-east-1.amazonaws.com/prod/ec2 ----- Stop
curl -X POST https://kcdpqfvoj9.execute-api.us-east-1.amazonaws.com/prod/ --- Start
In the above commands “/ec2 “ is Resource name for Stop instance
In the above commands “/“ is Resource name for Start instance
You can verify in your Instances console for their behaviour for your inputs.