API, Firebase, AWS

Firebase CloudFunctions <-> AWS API Gateway -> Lambda -> CloudWatch

AWS

 

AWS側にAPIを作って、CloudFunctionsで叩いてRealtimeDBに保存するタスクがあったので作っていく

AWS Lambda

Lamdaの実行ロールに

「CloudWatchReadOnlyAccess」
AmazonEC2ReadOnlyAccess

を設定

 

環境変数に設定

  • ec2_app_tag_name
    appサーバのtag:Nameの値
  • ec2_web_tag_name
    webサーバのtag:Nameの値
    例)
  • rds_name
    例) sampleDb
  • alb_id
    arnから下記の部分を抜き出す
    arn:aws:elasticloadbalancing:ap-northeast-1:99999999:loadbalancer/app/SampleProdLB/xxxxxxx

    例) app/SampleProdLB/xxxxxxx

 

Lambda関数

import json
import boto3
import os
from datetime import datetime, date, timedelta

setMinutes = 10           # 調査する分数
period = setMinutes * 60  # 調査期間となる秒数


now = datetime.now()
startTime = now - timedelta(minutes=setMinutes)
endTime = datetime.strftime(now, '%Y-%m-%d %H:%M:%S')
startTime = datetime.strftime(startTime, '%Y-%m-%d %H:%M:%S')

def lambda_handler(event, context):
    appInstanceId = getEc2InstanceIdBytagName(os.environ['ec2_app_tag_name']) # APIサーバのinstanceId
    webInstanceId = getEc2InstanceIdBytagName(os.environ['ec2_web_tag_name']) # WEBサーバのinstanceId
    rdsInstanceName = os.environ['rds_name'] # RDS名
    albId = os.environ['alb_id'] # ALBのid
    
    all_metrics_list = [
        {
        'NameSpaceHeader' : 'AWS/',
        'NameSpace' : 'EC2',
        'MetricName':'CPUUtilization',
        'Dimensions':[{"Name" : "InstanceId","Value" : appInstanceId}],
        'Statistics' : 'Average'
        },
        {
        'NameSpaceHeader' : 'AWS/',
        'NameSpace' : 'EC2',
        'MetricName':'CPUUtilization',
        'Dimensions':[{"Name" : "InstanceId","Value" : webInstanceId}],
        'Statistics' : 'Average'
        },
        {
        'NameSpaceHeader' : 'AWS/',
        'NameSpace' : 'RDS',
        'MetricName':'CPUUtilization',
        'Dimensions':[{"Name" : "DBInstanceIdentifier","Value" : rdsInstanceName}],
        'Statistics' : 'Average'
        },
        {
        'NameSpaceHeader' : 'AWS/',
        'NameSpace' : 'ApplicationELB',
        'MetricName':'HTTPCode_ELB_4XX_Count',
        'Dimensions':[{"Name" : "LoadBalancer","Value" : albId}],
        'Statistics' : 'Sum'
        },
    ]

    logs = []
    for target in all_metrics_list:
        #メトリクス取得
        logs.append(getMetricStatistics(target))


    return {
        'statusCode': 200,
        'body': json.dumps(logs, default=support_datetime_default)
    }

# tag:NameからinstanceIdを取得
def getEc2InstanceIdBytagName(ec2_tag_name):
    client = boto3.client('ec2')
    responce = client.describe_instances(Filters=[{'Name': 'tag:Name', 'Values': [ec2_tag_name]}])
    return responce['Reservations'][0]['Instances'][0]['InstanceId']

def getMetricStatistics(target_dict):
    cloudwatch = boto3.client('cloudwatch', region_name='ap-northeast-1')

    logs = cloudwatch.get_metric_statistics(
                                Namespace=target_dict["NameSpaceHeader"] + target_dict["NameSpace"],
                                MetricName=target_dict["MetricName"],
                                Dimensions=target_dict["Dimensions"],
                                StartTime=startTime,
                                EndTime=endTime,
                                Period=period,
                                Statistics=[target_dict["Statistics"]]
                                )
    return logs
    

def support_datetime_default(o):
    if isinstance(o, datetime):
        return o.isoformat()
    raise TypeError(repr(o) + " is not JSON serializable")

 

Response

Response:
{
  "statusCode": 200,
  "body": "{\"Label\": \"CPUUtilization\", \"Datapoints\": [{\"Timestamp\": \"2020-10-06T20:20:00+00:00\", \"Average\": 6.070186162830832, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:30:00+00:00\", \"Average\": 6.462568305995153, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:20:00+00:00\", \"Average\": 6.145475595072712, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:45:00+00:00\", \"Average\": 6.099282208016868, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:50:00+00:00\", \"Average\": 8.488223580628976, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:10:00+00:00\", \"Average\": 6.079781420761401, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:40:00+00:00\", \"Average\": 6.053389830500646, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:10:00+00:00\", \"Average\": 10.378225432978748, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:45:00+00:00\", \"Average\": 6.1343104566163404, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:15:00+00:00\", \"Average\": 6.238121700480376, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:45:00+00:00\", \"Average\": 11.509803649162144, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:20:00+00:00\", \"Average\": 6.582370102806403, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:40:00+00:00\", \"Average\": 5.983717699364876, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:05:00+00:00\", \"Average\": 6.30182458090993, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:40:00+00:00\", \"Average\": 6.3637121422689855, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:05:00+00:00\", \"Average\": 6.1682087616931955, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:10:00+00:00\", \"Average\": 6.317791979257121, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:00:00+00:00\", \"Average\": 9.414721681948164, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:00:00+00:00\", \"Average\": 6.128961748637568, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:25:00+00:00\", \"Average\": 6.16415671019689, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:15:00+00:00\", \"Average\": 7.3838658886802815, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:45:00+00:00\", \"Average\": 6.281041029923612, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:35:00+00:00\", \"Average\": 9.190941928305403, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:05:00+00:00\", \"Average\": 7.279818468108256, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:55:00+00:00\", \"Average\": 6.4494581828169855, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:55:00+00:00\", \"Average\": 6.184282671108724, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:15:00+00:00\", \"Average\": 6.11595813653137, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:25:00+00:00\", \"Average\": 6.399759192372522, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:15:00+00:00\", \"Average\": 6.350703899227385, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:30:00+00:00\", \"Average\": 6.333189774937032, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:35:00+00:00\", \"Average\": 6.534231731028117, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:50:00+00:00\", \"Average\": 9.034296563852237, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:55:00+00:00\", \"Average\": 6.636051681030466, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:10:00+00:00\", \"Average\": 6.177132536827161, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:25:00+00:00\", \"Average\": 6.461067889232311, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:20:00+00:00\", \"Average\": 6.582698897841996, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:30:00+00:00\", \"Average\": 7.190298230986407, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:40:00+00:00\", \"Average\": 9.010299157176966, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:50:00+00:00\", \"Average\": 6.483764008524828, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:00:00+00:00\", \"Average\": 6.217078818178334, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:10:00+00:00\", \"Average\": 7.6256737982738745, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:35:00+00:00\", \"Average\": 6.267773455586022, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:25:00+00:00\", \"Average\": 6.237718810769006, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:25:00+00:00\", \"Average\": 6.117060294518236, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:30:00+00:00\", \"Average\": 6.29480874317328, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:35:00+00:00\", \"Average\": 7.301361489306406, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:40:00+00:00\", \"Average\": 6.300977123263604, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:20:00+00:00\", \"Average\": 6.016504584602823, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:45:00+00:00\", \"Average\": 6.080730758540356, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:40:00+00:00\", \"Average\": 6.132907289069149, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:50:00+00:00\", \"Average\": 8.927646568475597, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:55:00+00:00\", \"Average\": 6.170723349063185, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:00:00+00:00\", \"Average\": 10.53092988792776, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:10:00+00:00\", \"Average\": 6.806108178190018, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:25:00+00:00\", \"Average\": 6.482291377219154, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:35:00+00:00\", \"Average\": 6.9550847457781035, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:45:00+00:00\", \"Average\": 6.454802259883058, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:55:00+00:00\", \"Average\": 7.590960451981216, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:05:00+00:00\", \"Average\": 12.18412058905244, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:25:00+00:00\", \"Average\": 6.0846762989725836, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:00:00+00:00\", \"Average\": 5.934000185232433, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:15:00+00:00\", \"Average\": 7.591659720280708, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:20:00+00:00\", \"Average\": 6.114976382340457, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:30:00+00:00\", \"Average\": 6.082615541354082, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:30:00+00:00\", \"Average\": 5.999930536260722, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:50:00+00:00\", \"Average\": 6.046832453463173, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:45:00+00:00\", \"Average\": 6.296471242015488, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:40:00+00:00\", \"Average\": 6.050393627848015, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:55:00+00:00\", \"Average\": 6.5353061035584705, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:05:00+00:00\", \"Average\": 6.179508196732955, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:15:00+00:00\", \"Average\": 9.107576178583711, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:40:00+00:00\", \"Average\": 7.2724321570806065, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:30:00+00:00\", \"Average\": 6.103227748448446, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:05:00+00:00\", \"Average\": 7.392358988619717, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:05:00+00:00\", \"Average\": 6.20269982402092, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:30:00+00:00\", \"Average\": 6.279383161977992, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:00:00+00:00\", \"Average\": 6.696850977115771, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:45:00+00:00\", \"Average\": 7.857252014456087, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:55:00+00:00\", \"Average\": 8.498068908034483, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:30:00+00:00\", \"Average\": 6.28659812910651, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:45:00+00:00\", \"Average\": 6.08455589515561, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:20:00+00:00\", \"Average\": 7.575409836061695, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:35:00+00:00\", \"Average\": 8.039612855426817, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:10:00+00:00\", \"Average\": 6.75632120031751, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:35:00+00:00\", \"Average\": 6.132916550881736, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:00:00+00:00\", \"Average\": 10.484291932948155, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:50:00+00:00\", \"Average\": 6.186440677966102, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:25:00+00:00\", \"Average\": 7.157738260627822, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:15:00+00:00\", \"Average\": 5.9640363063851565, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:15:00+00:00\", \"Average\": 7.467352042233445, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:50:00+00:00\", \"Average\": 14.382328424558816, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:15:00+00:00\", \"Average\": 6.354519774011299, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:50:00+00:00\", \"Average\": 7.579619338705257, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:55:00+00:00\", \"Average\": 9.7418866351879, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:05:00+00:00\", \"Average\": 6.1532555339327715, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:45:00+00:00\", \"Average\": 8.513332407145997, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:40:00+00:00\", \"Average\": 10.171922756321202, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:30:00+00:00\", \"Average\": 6.31819949986476, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:40:00+00:00\", \"Average\": 6.012670186155255, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:50:00+00:00\", \"Average\": 6.079781420761084, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:20:00+00:00\", \"Average\": 6.385042141343254, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:25:00+00:00\", \"Average\": 8.06141983885168, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:15:00+00:00\", \"Average\": 6.354065944259483, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:35:00+00:00\", \"Average\": 6.136158192082371, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:10:00+00:00\", \"Average\": 8.490437158466127, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:00:00+00:00\", \"Average\": 6.033291655100695, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:30:00+00:00\", \"Average\": 6.166666666666666, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:05:00+00:00\", \"Average\": 9.525391312405208, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:00:00+00:00\", \"Average\": 6.4175465407058825, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:55:00+00:00\", \"Average\": 6.429901824592932, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:45:00+00:00\", \"Average\": 6.287982772988747, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:00:00+00:00\", \"Average\": 6.083462998981328, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:45:00+00:00\", \"Average\": 6.031494859675416, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:10:00+00:00\", \"Average\": 6.129758266169138, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:40:00+00:00\", \"Average\": 6.062841530054644, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:00:00+00:00\", \"Average\": 6.2500138927479885, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:10:00+00:00\", \"Average\": 6.015777530791902, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:15:00+00:00\", \"Average\": 6.151634713350759, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:50:00+00:00\", \"Average\": 5.983546355472929, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:25:00+00:00\", \"Average\": 6.28738075390523, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:05:00+00:00\", \"Average\": 6.301787533581648, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:20:00+00:00\", \"Average\": 6.695901639332684, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:55:00+00:00\", \"Average\": 6.253672316388258, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:30:00+00:00\", \"Average\": 7.802519218313023, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:05:00+00:00\", \"Average\": 6.383064740212796, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:35:00+00:00\", \"Average\": 6.115249606365024, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:35:00+00:00\", \"Average\": 6.317379827733485, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:50:00+00:00\", \"Average\": 6.034847642856124, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:15:00+00:00\", \"Average\": 6.166513846439022, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:25:00+00:00\", \"Average\": 6.113781605989958, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:35:00+00:00\", \"Average\": 6.154913401878392, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:00:00+00:00\", \"Average\": 6.133333333341094, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:15:00+00:00\", \"Average\": 6.369412799848186, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:45:00+00:00\", \"Average\": 6.033435213477276, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:25:00+00:00\", \"Average\": 6.362170047223701, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:20:00+00:00\", \"Average\": 6.6627442808188775, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:00:00+00:00\", \"Average\": 6.137973511152553, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:40:00+00:00\", \"Average\": 7.083333333333331, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:50:00+00:00\", \"Average\": 9.628619060853866, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:35:00+00:00\", \"Average\": 6.79683245346699, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:45:00+00:00\", \"Average\": 8.055084745763091, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:40:00+00:00\", \"Average\": 6.046994535523134, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:20:00+00:00\", \"Average\": 6.892812818387123, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:05:00+00:00\", \"Average\": 6.204366953779446, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:30:00+00:00\", \"Average\": 12.544873576000837, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:55:00+00:00\", \"Average\": 6.845540427889198, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:55:00+00:00\", \"Average\": 6.181944058549982, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:25:00+00:00\", \"Average\": 6.421343891825489, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:15:00+00:00\", \"Average\": 6.003107344636648, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:15:00+00:00\", \"Average\": 6.5213531536539175, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:45:00+00:00\", \"Average\": 6.416666666666667, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:35:00+00:00\", \"Average\": 10.05722885986422, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:35:00+00:00\", \"Average\": 6.116513846431262, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:40:00+00:00\", \"Average\": 6.551014170591559, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:05:00+00:00\", \"Average\": 6.57719736963558, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:55:00+00:00\", \"Average\": 6.894146522185902, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:10:00+00:00\", \"Average\": 6.116513846430932, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:10:00+00:00\", \"Average\": 6.396888024451561, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:15:00+00:00\", \"Average\": 6.352162637769816, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:30:00+00:00\", \"Average\": 6.101634713342035, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:35:00+00:00\", \"Average\": 6.31709734185849, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:40:00+00:00\", \"Average\": 6.216388811715107, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:10:00+00:00\", \"Average\": 6.367935537641453, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:00:00+00:00\", \"Average\": 11.06348985830147, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:10:00+00:00\", \"Average\": 6.086843567641898, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:30:00+00:00\", \"Average\": 6.531842178394401, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:20:00+00:00\", \"Average\": 6.050120403804366, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:30:00+00:00\", \"Average\": 6.030327868860349, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:50:00+00:00\", \"Average\": 6.483199036773547, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:50:00+00:00\", \"Average\": 5.9531999629485925, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:40:00+00:00\", \"Average\": 6.1171899601659065, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:05:00+00:00\", \"Average\": 9.01666666666279, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:00:00+00:00\", \"Average\": 6.13289802723781, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:00:00+00:00\", \"Average\": 10.328142076498855, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:25:00+00:00\", \"Average\": 6.01771788459408, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:25:00+00:00\", \"Average\": 6.117069556369956, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:45:00+00:00\", \"Average\": 6.070621468934443, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:20:00+00:00\", \"Average\": 6.195211632849726, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:20:00+00:00\", \"Average\": 6.200676113727214, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:05:00+00:00\", \"Average\": 6.198471797713698, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:45:00+00:00\", \"Average\": 6.038519959244383, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:40:00+00:00\", \"Average\": 9.633310178749483, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:05:00+00:00\", \"Average\": 13.599620264896217, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:15:00+00:00\", \"Average\": 7.821079929613754, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:25:00+00:00\", \"Average\": 6.25480225989082, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:35:00+00:00\", \"Average\": 6.166666666666666, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:35:00+00:00\", \"Average\": 6.399999999984476, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:55:00+00:00\", \"Average\": 6.25406594424759, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:45:00+00:00\", \"Average\": 21.66762526627366, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:45:00+00:00\", \"Average\": 6.305450588134292, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:55:00+00:00\", \"Average\": 6.630165786780919, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:10:00+00:00\", \"Average\": 6.082096878758934, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:50:00+00:00\", \"Average\": 6.083333333333332, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:30:00+00:00\", \"Average\": 5.9967213114714, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:35:00+00:00\", \"Average\": 6.417088080022293, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:45:00+00:00\", \"Average\": 6.500000000000003, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:50:00+00:00\", \"Average\": 6.0868435676419015, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:55:00+00:00\", \"Average\": 6.33443086042458, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:00:00+00:00\", \"Average\": 6.400606649999124, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:10:00+00:00\", \"Average\": 6.5315689543316635, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:20:00+00:00\", \"Average\": 6.45720107438823, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:30:00+00:00\", \"Average\": 6.7573585255169295, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:40:00+00:00\", \"Average\": 6.383356487921072, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:50:00+00:00\", \"Average\": 6.210475131985055, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:40:00+00:00\", \"Average\": 6.079781420761084, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:05:00+00:00\", \"Average\": 6.766666666682189, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:55:00+00:00\", \"Average\": 6.421297582665538, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:25:00+00:00\", \"Average\": 6.390112994362056, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:05:00+00:00\", \"Average\": 6.05000000000776, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:15:00+00:00\", \"Average\": 6.080466796328372, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:20:00+00:00\", \"Average\": 6.28021209596002, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:30:00+00:00\", \"Average\": 6.489422987874571, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:00:00+00:00\", \"Average\": 6.179929610069176, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:40:00+00:00\", \"Average\": 6.715661757903736, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:50:00+00:00\", \"Average\": 7.447068630170716, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:35:00+00:00\", \"Average\": 6.201653237021863, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:10:00+00:00\", \"Average\": 7.31495785866826, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:35:00+00:00\", \"Average\": 6.381286468471224, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:00:00+00:00\", \"Average\": 6.548772807249766, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:25:00+00:00\", \"Average\": 6.590858571817874, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:25:00+00:00\", \"Average\": 6.148318977497697, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:50:00+00:00\", \"Average\": 6.096022043171976, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:20:00+00:00\", \"Average\": 6.087005649701858, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:00:00+00:00\", \"Average\": 5.930054644820256, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:55:00+00:00\", \"Average\": 6.202926738901322, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:30:00+00:00\", \"Average\": 7.008937667863071, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:05:00+00:00\", \"Average\": 6.395248680188684, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:20:00+00:00\", \"Average\": 6.103107344632771, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:55:00+00:00\", \"Average\": 6.7999999999883585, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:10:00+00:00\", \"Average\": 6.05106974158363, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:45:00+00:00\", \"Average\": 6.383481522633666, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:20:00+00:00\", \"Average\": 6.4066268407968705, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:35:00+00:00\", \"Average\": 6.35412151523565, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:25:00+00:00\", \"Average\": 6.288557006572348, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:20:00+00:00\", \"Average\": 5.983726961196546, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:20:00+00:00\", \"Average\": 6.511998703339449, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:10:00+00:00\", \"Average\": 6.300129665636028, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:45:00+00:00\", \"Average\": 6.133462998969362, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T04:15:00+00:00\", \"Average\": 6.367935537661185, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T13:05:00+00:00\", \"Average\": 6.151231823639072, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:00:00+00:00\", \"Average\": 15.646059090496525, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:15:00+00:00\", \"Average\": 6.0792349726931825, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T00:50:00+00:00\", \"Average\": 6.167069556357986, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:40:00+00:00\", \"Average\": 6.246869500783376, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:45:00+00:00\", \"Average\": 7.265217189952733, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:10:00+00:00\", \"Average\": 6.062841530054643, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:35:00+00:00\", \"Average\": 6.017051032690128, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:30:00+00:00\", \"Average\": 6.905825692325953, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:55:00+00:00\", \"Average\": 6.233333333337212, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:20:00+00:00\", \"Average\": 6.233333333337214, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:30:00+00:00\", \"Average\": 5.884546633332028, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:20:00+00:00\", \"Average\": 8.052824858756933, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:50:00+00:00\", \"Average\": 7.219056219316364, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:05:00+00:00\", \"Average\": 6.289950912286903, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:55:00+00:00\", \"Average\": 6.277160322319259, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:55:00+00:00\", \"Average\": 6.288811706952339, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:05:00+00:00\", \"Average\": 6.3489858293886465, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:00:00+00:00\", \"Average\": 6.099999999996117, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:05:00+00:00\", \"Average\": 6.361623599136398, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:20:00+00:00\", \"Average\": 13.715476521244716, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:10:00+00:00\", \"Average\": 6.037116791697514, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T03:30:00+00:00\", \"Average\": 6.655367231646312, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T02:45:00+00:00\", \"Average\": 6.4130267666907015, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:10:00+00:00\", \"Average\": 8.396999166419524, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:15:00+00:00\", \"Average\": 8.134685560803922, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:25:00+00:00\", \"Average\": 6.050000000007762, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T16:15:00+00:00\", \"Average\": 6.020056497175074, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T10:40:00+00:00\", \"Average\": 6.633333333341096, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:50:00+00:00\", \"Average\": 6.135750671475062, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:25:00+00:00\", \"Average\": 10.880466796332053, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T18:25:00+00:00\", \"Average\": 5.950111141995332, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:40:00+00:00\", \"Average\": 6.0834352134850365, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T19:00:00+00:00\", \"Average\": 6.769046957484247, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T17:15:00+00:00\", \"Average\": 6.017051032709853, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T20:10:00+00:00\", \"Average\": 6.03483838104322, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T12:50:00+00:00\", \"Average\": 6.1165231082823315, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T21:55:00+00:00\", \"Average\": 6.286561081793688, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T22:40:00+00:00\", \"Average\": 6.332379364638392, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T14:35:00+00:00\", \"Average\": 6.18318977495288, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T23:50:00+00:00\", \"Average\": 6.2496017412243505, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T01:35:00+00:00\", \"Average\": 6.322427526172958, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T05:15:00+00:00\", \"Average\": 6.40000000000388, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T06:25:00+00:00\", \"Average\": 15.111447624336417, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T07:00:00+00:00\", \"Average\": 7.557872557191553, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T08:10:00+00:00\", \"Average\": 6.775696952869438, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T09:55:00+00:00\", \"Average\": 6.390798369929344, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-06T15:30:00+00:00\", \"Average\": 6.016212836895198, \"Unit\": \"Percent\"}, {\"Timestamp\": \"2020-10-07T11:05:00+00:00\", \"Average\": 6.676683337964052, \"Unit\": \"Percent\"}], \"ResponseMetadata\": {\"RequestId\": \"fad5c3d4-3718-4f04-9565-d2c5987044d5\", \"HTTPStatusCode\": 200, \"HTTPHeaders\": {\"x-amzn-requestid\": \"fad5c3d4-3718-4f04-9565-d2c5987044d5\", \"content-type\": \"text/xml\", \"content-length\": \"45578\", \"vary\": \"accept-encoding\", \"date\": \"Thu, 08 Oct 2020 14:08:27 GMT\"}, \"RetryAttempts\": 0}}"
}

 

AWS API Gateway

「サービス」-> 「API Gateway」-> 「HTTP API」-> 「構築」-> 「統合を追加」->「構築」-> 作ったLambdaを選択 -> APIの名前を入れて -> ルート、ステージをウィザードにそってクリック

{URL}/{ルート}

でレスポンス取得できたら完了!

 

CloudFunctions

 

firebaseコマンド導入

$ npm install -g firebase-tools

 

シェルのリロードして反映

 

$ exec $SHELL -l

 

プロジェクトの作成

$ mkdir -p ./sample-status-prod
$ cd sample-status-prod
public $ firebase init

nodeのバージョンが10以上じゃないとあかんらしい

$ nodebrew install v10.10.0

$ nodebrew use v10.10.0
use v10.10.0

$ npm install -g firebase-tools

 

$ npm install -g firebase-tools

 

ログイン

aws-status-app $ firebase login

 

$ mkdir sample-status-prod
$ cd sample-status-prod
$ firebase login

sample-status-prod $ firebase init

     ######## #### ########  ######## ########     ###     ######  ########
     ##        ##  ##     ## ##       ##     ##  ##   ##  ##       ##
     ######    ##  ########  ######   ########  #########  ######  ######
     ##        ##  ##    ##  ##       ##     ## ##     ##       ## ##
     ##       #### ##     ## ######## ########  ##     ##  ######  ########

You're about to initialize a Firebase project in this directory:

  /Users/kanehiroyuu/Desktop/develop.nosync/sample_project/sample-status-prod

? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices.
 ◉ Database: Deploy Firebase Realtime Database Rules
 ◯ Firestore: Deploy rules and create indexes for Firestore
 ◉ Functions: Configure and deploy Cloud Functions
❯◉ Hosting: Configure and deploy Firebase Hosting sites
 ◯ Storage: Deploy Cloud Storage security rules
 ◯ Emulators: Set up local emulators for Firebase features
 ◯ Remote Config: Get, deploy, and rollback configurations for Remote Config



 ? Please select an option:
  Use an existing project
❯ Create a new project
  Add Firebase to an existing Google Cloud Platform project
  Don't set up a default project

? Please specify a unique project id (warning: cannot be modified afterward) [6-30 characters]:
 sample-status-prod
? What would you like to call your project? (defaults to your project ID) ()



=== Functions Setup

A functions directory will be created in your project with a Node.js
package pre-configured. Functions can be deployed with firebase deploy.

? What language would you like to use to write Cloud Functions? (Use arrow keys)
❯ JavaScript
  TypeScrip

? Do you want to use ESLint to catch probable bugs and enforce style? (y/N)
✔  Wrote functions/package.json
✔  Wrote functions/index.js
✔  Wrote functions/.gitignore
? Do you want to install dependencies with npm now? (Y/n)


? What do you want to use as your public directory? (public)

? Configure as a single-page app (rewrite all urls to /index.html)? (y/N)

? Set up automatic builds and deploys with GitHub? (y/N) N

✔  Wrote public/404.html
✔  Wrote public/index.html

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...
i  Writing gitignore file to .gitignore...

✔  Firebase initialization complete!

 

functions/index.js

const functions = require('firebase-functions');
var admin = require("firebase-admin");
var serviceAccount = require("./serviceAccount.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://sample-status2.firebaseio.com/"
});

const https = require('https');
const lambdaUrl = 'https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/get-CloudWatch-metrics'
const CPUUsageBadAverage = 70;
const ELBBadCount = 100;
const redMessage = '動作が不安定になっている場合があります。しばらく待つかサポートまでご連絡ください。';
const greenMessage = '正常動作。もし気になる点がありましたらサポートまでご連絡ください。';


exports.updateSampleStatus = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
    const request = https.request(lambdaUrl, (res) => {
        res.on('data', (chunk) => {
            var result = `${chunk}`;
            const chunkString = chunk.toString();
            const obj = JSON.parse(chunkString);
            var appCpuAverage = Math.floor(obj[0]['Datapoints'][0]['Average']); // APIとコンソールのCPU使用率平均
            var webCpuAverage = Math.floor(obj[1]['Datapoints'][0]['Average']); // WEBのCPU使用率平均
            var rdsCpuAverage = Math.floor(obj[2]['Datapoints'][0]['Average']); // RDSのCPU使用率平均
            var elb4xxCountSum = Math.floor(obj[3]['Datapoints'][0]['Sum']);    // ELBの4xxのカウント合計

            var db = admin.database();
            var ref = db.ref();
            ref.once('value', snapshot => {
                const data = snapshot.val();

                // APIとconsoleの値を更新
                if (appCpuAverage >= CPUUsageBadAverage) {
                    data['app'] = 'BAD';
                    data['app_cpu'] = ""+appCpuAverage+"%";
                    data['console'] = 'BAD';
                    data['message'] = redMessage;
                } else {
                    data['app'] = 'OK';
                    data['app_cpu'] = ""+appCpuAverage+"%";
                    data['console'] = 'OK';
                    data['console_cpu'] = ""+appCpuAverage+"%";
            }

            // WEBの値を更新
            if (webCpuAverage >= CPUUsageBadAverage) {
                    data['web'] = 'BAD';
                    data['web_cpu'] = ""+webCpuAverage+"%";
                    data['message'] = redMessage;
            } else {
                    data['web'] = 'OK';
                    data['web_cpu'] = ""+webCpuAverage+"%";
            }

            // RDSの値を更新
            if (rdsCpuAverage >= CPUUsageBadAverage) {
                    data['rds'] = 'BAD';
                    data['rds_cpu'] = ""+rdsCpuAverage+"%";
                    data['message'] = redMessage;
            } else {
                    data['rds'] = 'OK';
                    data['rds_cpu'] = ""+rdsCpuAverage+"%";
            }

            // ELBの値を更新
            if (elb4xxCountSum >= ELBBadCount) {
                    data['elb'] = 'BAD';
                    data['elb_code_HTTP4xx_count'] = elb4xxCountSum;
                    data['message'] = redMessage;
            } else {
                    data['elb'] = 'OK';
                    data['elb_code_HTTP4xx_count'] = elb4xxCountSum;
            }

            // すべて正常な場合
            if (webCpuAverage < CPUUsageBadAverage && appCpuAverage < CPUUsageBadAverage && rdsCpuAverage < CPUUsageBadAverage  && elb4xxCountSum < ELBBadCount) {
                data['message'] = greenMessage;
            }

            // 保存
                ref.set(data);
            });
        });

        res.on('end', () => {
            console.log('No more data in response.');
        });
    })

    request.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
    });

    request.end();
});

 

●functions/serviceAccount.json

秘密鍵を貼り付ける

 

●public/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Welcome to Firebase Hosting</title>

    <!-- update the version number as needed -->
    <script defer src="/__/firebase/7.23.0/firebase-app.js"></script>
    <!-- include only the Firebase features as you need -->
    <script defer src="/__/firebase/7.23.0/firebase-auth.js"></script>
    <script defer src="/__/firebase/7.23.0/firebase-database.js"></script>
    <script defer src="/__/firebase/7.23.0/firebase-messaging.js"></script>
    <script defer src="/__/firebase/7.23.0/firebase-storage.js"></script>
    <script defer src="/__/firebase/7.23.0/firebase-analytics.js"></script>
    <script defer src="/__/firebase/7.23.0/firebase-remote-config.js"></script>
    <script defer src="/__/firebase/7.23.0/firebase-performance.js"></script>
    <!-- initialize the SDK after all desired features are loaded -->
    <script defer src="/__/firebase/init.js"></script>

    <style media="screen">
      body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
      #message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
      #message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
      #message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
      #message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
      #message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
      #message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
      #load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
      @media (max-width: 600px) {
        body, #message { margin-top: 0; background: white; box-shadow: none; }
        body { border-top: 16px solid #ffa100; }
      }
    </style>
  </head>
  <body>
    <div id="message">
      <h1>sampleステータス</h1>
      <h2 id="statusMessage"></h2>
      <p>sampleの各サービス稼働状況です。</p>
      <table border="1" cellspacing="0" cellpadding="5" bordercolor="#333333">
        <tr>
          <th>サービス</th>
          <th>ステータス</th>
          <th>CPU使用率</th>
        </tr>
        <tr>
          <td>WEBサーバ</td>
          <td id="webStatus"></td>
          <td id="webCpu"></td>
        </tr>
        <tr>
          <td>APIサーバ</td>
          <td id="appStatus"></td>
          <td id="appCpu"></td>
        </tr>
        <tr>
          <td>コンソール</td>
          <td id="consoleStatus"></td>
          <td id="consoleCpu"></td>
        </tr>
        <tr>
          <td>データベース</td>
          <td id="rdsStatus"></td>
          <td id="rdsCpu"></td>
        </tr>
      </table>
      <hr/>
      <table border="1" cellspacing="0" cellpadding="5" bordercolor="#333333">
        <tr>
          <th>サービス</th>
          <th>ステータス</th>
          <th>4xxカウント数</th>
        </tr>
        <tr>
          <td>ロードバランサ</td>
          <td id="elbStatus"></td>
          <td id="elbCodeHTTP4xxCount"></td>
        </tr>
      </table>
    </div>
    <p id="load">Firebase SDK Loading&hellip;</p>

    <script>
      document.addEventListener('DOMContentLoaded', function() {

        var db = firebase.database();
        db.ref().on("value", function(snapshot) {
            document.getElementById("webStatus").innerText = snapshot.val().web;
            document.getElementById("webStatus").innerText = snapshot.val().web;
            document.getElementById("webCpu").innerText = snapshot.val().web_cpu;
            document.getElementById("appStatus").innerText = snapshot.val().app;
            document.getElementById("appCpu").innerText = snapshot.val().app_cpu;
            document.getElementById("consoleStatus").innerText = snapshot.val().console;
            document.getElementById("consoleCpu").innerText = snapshot.val().console_cpu;
            document.getElementById("rdsStatus").innerText = snapshot.val().rds;
            document.getElementById("rdsCpu").innerText = snapshot.val().rds_cpu;
            document.getElementById("elbStatus").innerText = snapshot.val().elb;
            document.getElementById("elbCodeHTTP4xxCount").innerText = snapshot.val().elb_code_HTTP4xx_count;
            document.getElementById("statusMessage").innerText = snapshot.val().message;
        });

        const loadEl = document.querySelector('#load');
        // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
        // // The Firebase SDK is initialized and available here!
        //
        // firebase.auth().onAuthStateChanged(user => { });
        // firebase.database().ref('/path/to/ref').on('value', snapshot => { });
        // firebase.messaging().requestPermission().then(() => { });
        // firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { });
        // firebase.analytics(); // call to activate
        // firebase.analytics().logEvent('tutorial_completed');
        // firebase.performance(); // call to activate
        //
        // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

        try {
          let app = firebase.app();
          let features = [
            'auth',
            'database',
            'messaging',
            'storage',
            'analytics',
            'remoteConfig',
            'performance',
          ].filter(feature => typeof app[feature] === 'function');
          loadEl.textContent = `Firebase SDK loaded with ${features.join(', ')}`;
        } catch (e) {
          console.error(e);
          loadEl.textContent = 'Error loading the Firebase SDK, check the console.';
        }
      });
    </script>
  </body>
</html>

 

コンソール作業

  • ロケーションの設定 asia-northeast1 (東京)に設定
  • hostingを作る
  • databaseの作成
  • Blazeプランに変更

 

デプロイ

$ firebase deploy --only functions
$ firebase deploy --only hosting:sample-status-prod

 

サンプルデータ作成

PHP date 日付を加算してサンプルデータを作る

 

@see

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)