AWS Lambda is a serverless compute service where developers don’t have to worry about which AWS rescores to launch or manage.


Objective:

Invoke your lambda function over http using Amazon API gateway as the http end point. Now,your web client can invoke the API ,and the API gateway can route the request to lambda

It manages:    

  • Launching And Terminating resources
  • Health check-up
  • autoscaling 
  • Updating.

Pricing:

  • Pay as you use
  • No of the request that user made to the lambda function
  • The time duration of which user code executes. 

Steps to start with Lambda function.

  1. Login to the web console and search for the service called Lambda 
  1. Lambda > Function > Create function 

Choose the option to create a function and fill up basic information (here we are using Create Author from scratch).

AWS Lambda natively supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby code, and provides a Runtime API which allows you to use any additional programming languages to author your functions

  1. Click on create function.
  1. Add a public endpoint.

Choose : Lambda function > Designer > Add trigger 

  1. Choose API Gateway 
  • API type select  HTTP API  And keep Security Group Open And Click on Add.HTTP APIs enable to create RESTful APIs with lower latency and lower cost than REST APIs. User can use HTTP APIs to send requests to AWS Lambda functions or to any routable HTTP endpoint. For example, you can create an HTTP API that integrates with a Lambda function on the backend.
  1. Now you can see : API Gateway is created
  1. Click on the link to the API endpoint. To get the result. 
  1. On the monitoring tab Lambda function is monitored. 

Lambda Function to start and stop EC2 instance:

  • Lambda functions have at least permission to start and stop instances.

Need to create policy and then create the role and associate this role with the lambda function. 

Steps:

  • Go to AWS console and search for service called IAM (to create the policy and role)
  • Click on policy and create new policy to start and stop instances.

Create policy and navigate to JSON. And click on review code.

JSON Code:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:Start*",
        "ec2:Stop*"
      ],
      "Resource": "*"    }
  ]
}
  1. Name the policy startandstopinsatncep and click on create policy.
  1. Attach policy to role:
  • Go to IAM and the Roles And click on create a Role 
  • And we have to associate this role to lambda So select lambda And click on Next: permissions
  1. And here attach the policy which created i.e startandstopinstancep and click NEXT. add tag if want otherwise can click on NEXT again.
  1. Give a name to role startandstopinsatncerole and click on create role.
  1. Now navigate to lambda function and create function to start and stop instance 

First creating function for stop instance 

  • Give the function name stopinsatnce. 
  • select programing language here Python 3.7
  • Under permissions use the existing role i.e stopandstartinsatncesp. and click on create function. 
  1. Now click on stopinatncep lambda function and under function code

Stops the EC2 instances:Python Lambda Function

import boto3
region = 'instance region'
instances = [‘instance id’]
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))
  1. Click on deploy and test.

check in EC2 service instance is stopping. 


Kindly comment here or mail us support@data-stats.com and edugenixcloud@gmail.com in case of any doubts. We will reach you as soon as possible.

Categories: AWS

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert