ks888 / LambStatus

[Maintenance mode] Serverless Status Page System
https://lambstatus.github.io
Apache License 2.0
1.3k stars 119 forks source link

CloudWatch Metrics API #160

Open stopkaya opened 5 years ago

stopkaya commented 5 years ago

I was wondering how you are implementing the cloudwatch metrics to LambStatus, I could not find API information on it on the documentation.

I am trying to write a request so it creates, the same set of cloudwatch graphs for different stacks I currently have. It takes too much time to manually do it for each and new ones.

stopkaya commented 5 years ago

domain-end-point/api/metrics { "type":"CloudWatch", "props":{ "Region":"eu-central-1", "Namespace":"AWS/ECS", "MetricName":"CPUUtilization", "Dimensions":[{"Name":"fill-it-in","Value":"fill-it-in"}, {"Name":"fill-it-in","Value":"fill-it-in"}], "Statistics":"Sum" }, "title":"New Metric with API6", "status":"Visible", "unit":"", "description":"", "decimalPlaces":0

}

And for the headers, I have included authorization, because this is the only way to put a CloudWatch metrics, but the authorization changes each time, would it be possible to do this without any authorization header.

ks888 commented 5 years ago

I'm not really sure I understand your question correctly, but the link below is the codes to list metrics and fetch the metric data. https://github.com/ks888/LambStatus/blob/master/packages/lambda/src/aws/cloudWatch.js

Or do you want to create a new Metric using LambStatus API? Unfortunately the API for this is not supported.

stopkaya commented 5 years ago

Yeah I am trying to post cloudwatch metrics using an API, however there is no documentation on how to post these graphs using APIs. I am trying to get/post/delete cloudwatch metrics, without using the admin page. I realized I am using the same exact CloudWatch metrics, so I wanted automatize it using a script.

Update: I managed to use boto3 and called "initiate_auth" using a python script, so i can grab the authorization header token which is currently needed to post cloudwatch metrics. This header is given only when the admin logs in and expires in 1 hour. So the script I created asks for username and password, the user uses the same username and password like in the admin page. Each time I run the script I initiate the authorization since the "authorization" header changes like every hour.

It does work, I am not sure if this is the easiest way though.

# importing the requests library
import requests
import json
import boto3

USERNAME = input("Enter username for the LambStatus Admin Page: ")
PASSWORD = input("Enter password for the LambStatus Admin Page: ")

client = boto3.client('cognito-idp')
response = client.initiate_auth(
    AuthFlow='USER_PASSWORD_AUTH',
    AuthParameters={
        'USERNAME': USERNAME,
        'PASSWORD': PASSWORD
    },
    ClientId = input("Enter clientID for the LambStatus CognitoPool: ")           
)

keys = response.get('AuthenticationResult')
new = keys
AUTH = new.get('IdToken')

print('Welcome to Automated Cloudwatch Metrics Creation for LambStatus')

# api-endpoint
URL = input("Type the api-endpoint: ")

# Headers
HEADERS = {'authorization': AUTH}

# Parameters
PARAMS = {}

# Body
body = {"type": "CloudWatch", "props": {"Region": "**change**", "Namespace": "**change**", "MetricName": "**change**", "Dimensions": [{"Name": "**change**", "Value": "**change**"}, {"Name": "**change**", "Value": "**change**"}], "Statistics": "**change**"}, "title": "**change**", "status": "**change**", "unit": "", "description": "", "decimalPlaces": **change**}

# post-request
response = requests.post(
    url=URL,   
    headers=HEADERS,
    params=PARAMS,   
    data=json.dumps(body)
)

if response:
    print('Success!')
else:
    print('An error has occurred.')

print(response.content)
pharindoko commented 5 years ago

Currently each (cloudwatch) metric needs to be added manually in the admin menu... Would be nice if we can add metrics directly via api call without any manual effort.