Intro: First step to AWS Lambda
Not many people choose Lambda as their new service
Lambda is a compute service that lets you run without provisioning or managing servers.
It is an essential service for automating AWS environments
I learned AWS Lambda with little experiences about code
But the advantage of being able to customize the various environments of AWS for automation and integration, motivated and challenged me to use lambda
Load CloudWatch Metric Index through Lambda
[Summary]
We worked on cloning Lambda for the first time
CloudWatch is the most used AWS service for monitoring AWS infra environment
We synced this service and lambda for delivering monitoring index to community messenger called slack
[Purposes]
Deliver CloudWatch metric index to slack channel by running Lambda code
If we achieve that goal, we don’t need to check CloudWatch monitoring information by accessing user account console.
We can implement the system that can directly requests to slack about CloudWatch information of various accounts.
It detects disabilities and enhances the readability by loading each essential CloudWatch metric index.
[Implementation]
<Set up modules to use Lambda>
1. pip Package management system
Install pip package to set up modules for training
C:\Users\user>curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
C:\Users\user>python get-pip.py
C:\Users\user>python -m pip install –upgrade pip
2. Essential modules Installation
C:\Users\user>pip install boto3
certifi
chardet
idna
json
requests
3. Compress(.zip) modules to use Lambda
C:\Users\your account\AppData\Local\Programs\Python\Python37\Lib\site-packages
<Create IAM roles to use Lambda>
1. Create IAM policies
IAM - Policy - Create Policy
Paste Jason policy code
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow", //
"Action": [
"cloudwatch:DescribeAlarmHistory",
"cloudwatch:GetDashboard",
"cloudwatch:GetMetricData",
"cloudwatch:DescribeAlarmsForMetric",
"cloudwatch:DescribeAlarms",
"cloudwatch:GetMetricStatistics",
"cloudwatch:GetMetricWidgetImage"
],
"Resource": "*"
}
]
}
2. Create IAM roles
IAM - roles - create roles
Lambda - choose created policies - create roles
<Write Lambda functions>
1. Create Lambda functions
Lambda - functions - create functions
write new - function names - runtime - running roles - existing roles
- Create random function names
- Choose the language and version when you write functions
- Apply roles that you created
2. Slack Settings
Slack - Apps - Create Bots - Copy API Token
3. Write Lambda functions
CloudWatch - Index - Choose Index - Copy sources
"metrics": [
[ "AWS/EC2", "CPUUtilization", "InstanceId", "i-0cf3d15721cedbf7e" ]
],
"view": "timeSeries",
"stacked": False,
"stat": "Maximum",
"period": 300,
"width": 914,
"height": 250,
"start": "-PT3H",
"end": "P0D",
"timezone": "+0900"
}
- false → False to correct the grammar
function codes - work - modules(.zip)upload
Create files - Write sample codes
import json
import boto3
import requests
cloudwatch = boto3.client('cloudwatch')
metric_sources = [
{
"metrics":
[ "AWS/EC2", "CPUUtilization"
],
"view": "timeSeries",
"stacked": False,
"region": "ap-northeast-2",
"timezone": "+0900"
}
]
def metric(event, context):
for metric_data in metric_sources :
metric_data = json.dumps(metric_data)
#print(metric_data)
image_data = cloudwatch.get_metric_widget_image(MetricWidget=metric_data)
#print(image_data)
slack_params = {
"filename":"test.png",
"token":"SLACK BOT의 token 값을 넣어 주시면 됩니다",
"channels":['#aws-db-slowquery-noti <-와 같이 채널 명칭을 넣어주세요.']
}
image = {'file': image_data['MetricWidgetImage']}
requests.post("https://slack.com/api/files.upload", params=slack_params, files=image)
-Paste CloudWatch metric sources to metric_sources
-Paste Token value of token Slack Bots
-Type channels name (ex. #awschannels)
4. Lambda functions test
Lambda - Choose functions - Test
-Set-up CloudWatch metric information will be printed out when functions run
Review: Potentials of Lambda
With this training process, We sync AWS services to trigger functions automatically to use AWS Lambda more flexibly and we will implement methods to use AWS Lambda more easily.
Lambda is an essential AWS service to implement the automation environment.
Our final goal is to get custom abilities for the automation environment and we need to get used to Lambda by practicing it.
References