šAutomated EC2 Instance Management and Alerting with Azure DevOps and AWS Lambda
>>> Implementing CI/CD for EC2 Instance Deployment and Monitoring with Real-time Email Notifications on State Changes
Hello World! Welcome to the ultimate hands-on guide on a task using AWS Lambda. It is a serverless computing service that allows developers to run code without provisioning or managing servers. With AWS Lambda, you can execute code in response to various events such as changes to data in an Amazon S3 bucket, updates to a DynamoDB table, HTTP requests via Amazon API Gateway, or triggered by other AWS services. Lambda functions are scalable, cost-effective, and automatically handle the underlying infrastructure, enabling developers to focus solely on writing code. This makes AWS Lambda an ideal solution for building microservices, real-time data processing applications, and back-end services. So letās get startedā¦
Task Overview:
- Launching an EC2 instance using Azure DevOps pipeline.
- Whenever the launched instance goes down, stopped, or terminated, a lambda function is triggered and sends an email to the respective team.
- This is implemented using AWS services like Lambda Functions, EC2 Instances, CloudWatch Rules, and SNS (Simple Notification Service)
Functionality Overview:
- Azure DevOps Pipeline: To automate the deployment of EC2 instances. This includes defining the necessary infrastructure as code, managing version control, and automating the deployment process.
- EC2 Instance: Provides scalable computing resources in AWS. These instances are the core computing resources where applications or services can run. The instancesā state (running, stopped, terminated) will be monitored for operational continuity.
- Lambda Functions: This executes code in response to specific events without provisioning or managing servers. A Lambda function will be triggered whenever there is a state change in the EC2 instance (e.g., instance goes down or stops). This function will check the state of the EC2 instance, and trigger an SNS notification if the instance is found to be stopped or terminated.
- CloudWatch Rules: CloudWatch rules will be set up to listen for EC2 instance state change notifications. When an instance changes state (e.g., stops or is terminated), the rule will trigger the Lambda function to handle the event.
- SNS, Simple Notification Service: SNS will be used to send email notifications to employees. When the Lambda function detects that an EC2 instance has stopped or been terminated, it will publish a message to an SNS topic. Subscribers (e.g., email addresses) to this topic will receive the notification, ensuring timely awareness and response to instance state changes.
Hands-On Guide:
Step 1: Launch an EC2 instance using the Azure DevOps pipeline.
- Steps to launch using: Azure DevOps, Jenkins, and GitLab Pipelines.
Step 2: Create an SNS topic and subscription.
- Navigate to SNS Service.
- Create a Topic.
- Go to Topics and follow the below process:
- Now, create a subscription for the topic.
- Go to Subscriptions and follow the below process:
- Once the subscription make sure to accept the subscription invitation that is sent to the above-given mail address.
- So, if we want to send the alert notifications to mobile follow the same process and just change the protocol to SMS.
Step 3: Write the Lambda function.
- Navigate to Lambda service and then select Functions.
- Here, an IAM role will be created and now attach the AmazonEC2ReadOnlyAccess and AmazonSNSFullAccess policies to this role.
- Now, select the created function and paste the below Python code snippet into the Code Source.
- And then select the Deploy option.
- Whenever the changes are made to the code make sure to Deploy.
Step 4: Create a CloudWatch Rule.
- Navigate to the CloudWatch service from the AWS console.
- Select āRulesā from the āEventsā section from the left navigation menu.
Step 5: Testing
- Launch an EC2 instance and stop it manually.
- Check the respective email.
- Finally, a notification is sentā¦šš
Architecture Diagram:
Source Code:
Step into my GitHub Repo, where Iāve compiled a comprehensive collection of source code.
Letās Connect:
Feel free to reach out, share your thoughts, or ask any questions. Iām excited to engage with you and learn from each other as we navigate this exciting field!
LinkedIn: Sai Manasa
GitHub: Sai Manasa
Happy Alert-ingā
Happy Learningš»