Explore the AWS services and learn DevOps, Let's work on serverless architecture and about automation of starting and stopping your EC2 instance using Lambda, cloudwatchEvents Bridge, and CloudWatch logs.

Here are some details about the services. Let's understand serverless.

What’s serverless?

• Serverless is a new paradigm in which the developers don’t have to manage servers anymore…

• They just deploy code.

• They just deploy… functions !

• Initially... Serverless == FaaS (Function as a Service)

• Serverless was pioneered by AWS Lambda but now also includes anything that’s managed: “databases, messaging, storage, etc.”

• Serverless does not mean there are no servers… it means you just don’t manage / provision / see them.

Serverless services on AWS:

1. AWS Lambda: Aws lambda is an event-driven, pay-as-you-go compute service that lets you run code without provisioning or managing servers.

2. AWS Fargate: AWS Fargate is a serverless compute engine that works with Amazon Elastic Container Service(ECS) and Amazon Elastic Kubernetes Services(EKS).

Now let's discuss the services that I have used in automation project.

  1. Cloudwatch EventBridge: Amazon EventBridge is a serverless event bus service that connects applications with data from multiple sources. 

Amazon EventBridge integrates with the following services:

  • AWS CloudFormation
  • AWS CloudTrail
  • AWS Kinesis Data Streams
  • AWS Config AWS Lambda 
  • AWS Identity and Access Management (IAM)

Functions of Amazon EventBridge:

  • It helps to build loosely coupled and distributed event-driven architectures.
  • It connects applications and delivers events without the need to write custom code.
  • The EventBridge schema registry stores a collection of event structures(schemas) and allows users to download code for those schemas in the IDE representing events as objects in the code.
  • An event bus is an entity that receives events, and rules get attached to that event bus that matches the events received. 
  • It delivers a stream of real-time data from SaaS applications or other AWS services and routes that data to different targets such as Amazon EC2 instances, Amazon ECS tasks, AWS CodeBuild projects, etc

 I have used two rules to start and stop EC2 instances after every 5 minutes. You can define either cron expression or rate expression. A cron expression is a combination of 6 values that tell EventBridge when to run the rule. For example, cron(0 12 * * ? *) runs the rule every day at 12:00pm UTC+0.

No alt text provided for this image
AWS EventBridge


2. AWS Lambda: AWS Lambda is a serverless computing service that allows users to run code as functions without provisioning or managing servers.

  • It helps to run the code on highly available computing infrastructure and performs administrative tasks like server maintenance, logging, capacity provisioning, automatic scaling, and code monitoring.
  • Using AWS Lambda, one can build serverless applications composed of Lambda functions triggered by events and can be automatically deployed using AWS CodePipeline and AWS CodeBuild.
  • Charges are applied based on the number of requests for the functions and the time taken to execute the code.
  • They are termed virtual functions, limited by time(less execution time of 300 seconds), run on demand, and automated scaling.

Code:
                                                Stop instance overview


No alt text provided for this image
                                            Start Instance Lambda Function

To start the EC2 Instance:-

import boto
region ='ap-south-1'
instances = ['i-06a14086f3b314a40']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=instances)
    print('starting your instances: ' + str(instances))
    
    

To stop the EC2 Instance:-

import boto
region ='ap-south-1'
instances = ['i-06a14086f3b314a40']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stop your instances: ' + str(instances))
    
    

CloudWatch Logs: Amazon CloudWatch is a service that monitors based on multiple metrics of AWS and on-premises resources.

  • AWS CloudWatch monitors AWS resources such as Amazon RDS DB instances, Amazon EC2 instances, Amazon DynamoDB tables, and any log files generated by the applications.
  •  Collects and correlates monitoring data in logs, metrics, and events from AWS resources, applications, and services that run on AWS and on-premises servers.
  • Offers dashboards and creates graphs to visualize cloud resources.
  • Visualizes logs to address issues and improve performance by performing queries.

Amazon CloudWatch is used alongside the following applications:

❖ Amazon Simple Notification Service (Amazon SNS)

❖ Amazon EC2 Auto Scaling

❖ AWS CloudTrail

❖ AWS Identity and Access Management (IAM) 

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

That's all in this blog. I hope this will be helpful for beginners. See you at the next one. Open to more such conversations with students and professionals willing to learn, share and change the world. Your feedback and suggestion will be valuable to me and motivated to explore more about cloud technology and share it with you all.

Wish you great success!

Regards,

Shivant Kumar Pandey

MyBlog Portfolio Github