{"id":1254,"date":"2021-02-20T14:40:24","date_gmt":"2021-02-20T05:40:24","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=1254"},"modified":"2021-03-01T08:26:53","modified_gmt":"2021-02-28T23:26:53","slug":"cloudwatch_synthetics_lambda","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2021\/02\/20\/cloudwatch_synthetics_lambda\/","title":{"rendered":"CloudWatch Synthetics\u3092\u6642\u523b\u6307\u5b9a\u3067\u5b9f\u884c\u3057\u3066\u307f\u305f"},"content":{"rendered":"

CloudWatch Synthetics<\/code> \u3092\u3001 AWS Lambda<\/code> \u304b\u3089\u6642\u523b\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002
\n\u4eca\u56de\u306f\u3001\u305d\u306e\u80cc\u666f\u3068\u5b9f\u884c\u65b9\u6cd5\u3084\u6ce8\u610f\u4e8b\u9805\u306b\u3064\u3044\u3066\u3054\u7d39\u4ecb\u3044\u305f\u3057\u307e\u3059\u3002<\/p>\n

\u80cc\u666f<\/h2>\n

\u5f0a\u793e\u3067\u306f\u3001\u30b3\u30fc\u30dd\u30ec\u30fc\u30c8\u30b5\u30a4\u30c8\u306e\u304a\u554f\u3044\u5408\u308f\u305b\u30da\u30fc\u30b8\u304c\u6b63\u5e38\u306b\u6a5f\u80fd\u3057\u3066\u3044\u308b\u304b\u3068\u3044\u3046\u78ba\u8a8d\u3092\u3001 Amazon CloudWatch Synthetics<\/code><\/a> \u3092\u4f7f\u3063\u3066\u884c\u3063\u3066\u3044\u307e\u3059\u3002 <\/p>\n

Amazon CloudWatch Synthetics<\/code> \u306fREST API\u3001URL\u3001\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u76e3\u8996\u3059\u308b\u305f\u3081\u306e\u30b5\u30fc\u30d3\u30b9\u3067\u3059\u3002<\/p>\n

2021\u5e742\u670820\u65e5\u73fe\u5728\u3001 CloudWatch Synthetics<\/code> \u306e\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u8a2d\u5b9a\u753b\u9762\u306b\u306f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u304a\u308a\u3001\u300c1\u56de\u5b9f\u884c\u300d\u300c\u7d99\u7d9a\u7684\u306b\u5b9f\u884c\u300d\u306e2\u3064\u3057\u304b\u9078\u3079\u307e\u305b\u3093\u3002 <\/p>\n

\"\"<\/p>\n

\u300c\u7d99\u7d9a\u7684\u306b\u5b9f\u884c\u300d\u306e\u8a2d\u5b9a\u5024\u3068\u3057\u3066\u306f\u6700\u5927\u3067 60<\/code> \u307e\u3067\u3057\u304b\u5165\u529b\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u4f8b\u3048\u3070\u300c1\u65e5\u306b1\u5ea6\u3060\u3051\u5b9f\u884c\u3057\u305f\u3044\u300d\u5834\u5408\u3084\u300cXX\u6708XX\u65e5\u306e10\u6642\u306b\u5b9f\u884c\u3057\u305f\u3044\u300d\u306a\u3069\u306e\u7d30\u304b\u3044\u8a2d\u5b9a\u306f\u884c\u306a\u3048\u307e\u305b\u3093\u3002 <\/p>\n

\u305d\u306e\u305f\u3081\u3001\u3053\u308c\u307e\u3067\u5f0a\u793e\u3067\u306f\u5b9f\u884c\u30bf\u30a4\u30df\u30f3\u30b0\u30921\u6642\u9593\u306b1\u5ea6\u306b(\u8a2d\u5b9a\u5024\u3092 60 \u306b\u8a2d\u5b9a)\u3057\u305f\u4e0a\u3067\u3001\u5b9f\u884c\u3055\u308c\u305f\u6642\u9593\u304c9\u6642\u53f0\u3060\u3063\u305f\u3089\u51e6\u7406\u3092\u884c\u3046\u304c\u3001\u305d\u308c\u4ee5\u5916\u3060\u3063\u305f\u3089\u4f55\u3082\u884c\u308f\u306a\u3044(\u51e6\u7406\u3092\u629c\u3051\u3066\u3057\u307e\u3046)\u3068\u3044\u3046\u5b9f\u88c5\u306b\u3057\u3066\u30011\u65e5\u306b1\u56de\u306e\u52d5\u4f5c\u78ba\u8a8d\u3092\u884c\u3063\u3066\u3044\u307e\u3057\u305f\u3002 <\/p>\n

\u30109\u6642\u53f0\u306b\u3060\u3051\u5b9f\u884c\u3059\u308b\u5834\u5408\u306e\u5b9f\u88c5\u4f8b\u3011<\/p>\n

\/\/ only 09:00 JST\nconst now = new Date()\nif (now.getHours() !== 0) return;\n---\u305d\u306e\u5f8c\u306e\u51e6\u7406\u7565---<\/code><\/pre>\n

\u3053\u306e\u5b9f\u88c5\u3067\u3001\u6570\u30ab\u6708\u9593\u904b\u7528\u3057\u3066\u304d\u305f\u3082\u306e\u306e\u3001\u306a\u305c\u304b\u5f90\u3005\u306b\u5b9f\u884c\u6642\u9593\u304c\u305a\u308c\u3066\u304d\u307e\u3057\u305f\u3002 <\/p>\n

\u3010\u904b\u7528\u958b\u59cb\u76f4\u5f8c(2020\/10\/26)\u3011 <\/p>\n

\"\"<\/p>\n

\u3010\u6700\u8fd1(2021\/02\/13)\u3011 <\/p>\n

\"\"<\/p>\n

\u753b\u50cf\u3092\u3054\u89a7\u3044\u305f\u3060\u3051\u308b\u3068\u304a\u308f\u304b\u308a\u304b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u904b\u7528\u958b\u59cb\u76f4\u5f8c\u306f 09:01<\/code> \u306b\u7d42\u308f\u3063\u3066\u3044\u308b\u51e6\u7406\u304c\u3001\u6700\u8fd1\u3067\u306f 09:36<\/code> \u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u3053\u3093\u306a\u72b6\u6cc1\u3060\u3063\u305f\u306e\u3067\u3001\u300c\u3042\u308c\uff1f\u4eca\u65e5\u306f\u52d5\u4f5c\u78ba\u8a8d\u306e\u901a\u77e5\u304c\u6765\u3066\u306a\u3044\u306a\u300d\u3068\u601d\u3063\u305f\u6570\u5206\u5f8c\u306b\u6765\u308b\u3053\u3068\u304c\u3042\u3063\u305f\u308a\u3001\u5b9f\u969b\u306e\u304a\u5ba2\u69d8\u304b\u3089\u306e\u304a\u554f\u3044\u5408\u308f\u305b\u304c\u6765\u305f\uff01\u3068\u601d\u3063\u305f\u3089\u52d5\u4f5c\u78ba\u8a8d\u306e\u901a\u77e5\u3060\u3063\u305f\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3057\u305f\u3002
\n\u3053\u306e\u72b6\u6cc1\u3092\u89e3\u6d88\u3059\u3079\u304f\u3001 CloudWatch Synthetics<\/code> \u3092\u5b9f\u884c\u3059\u308b\u3060\u3051\u306e AWS Lambda<\/code> \u3092\u4f5c\u308a\u3001\u305d\u308c\u3092\u6642\u523b\u6307\u5b9a\u3067\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002
\nAWS Lambda<\/code> \u306f\u3053\u3046\u3044\u3063\u305f\u300c\u6c7a\u307e\u3063\u305f\u6642\u9593\u300d\u3084\u3001\u300cS3\u306b\u30d5\u30a1\u30a4\u30eb\u304c\u7f6e\u304b\u308c\u305f\u3068\u3044\u3046\u3088\u3046\u306a\u30a4\u30d9\u30f3\u30c8\u300d\u3092\u30c8\u30ea\u30ac\u30fc\u306b\u3001\u3061\u3087\u3063\u3068\u3057\u305f\u95a2\u6570\u3092\u5b89\u4fa1\u306b\u5b9f\u884c\u3057\u305f\u3044\u3068\u3044\u3046\u5834\u5408\u306b\u6700\u9069\u3067\u3059\u3002<\/p>\n

\u5b9f\u884c\u65b9\u6cd5<\/h2>\n

Lambda \u95a2\u6570\u4f5c\u6210<\/h3>\n

Lambda \u95a2\u6570\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002
\nLambda \u306e\u95a2\u6570\u3068\u3057\u3066\u306f\u4e3b\u306b
synthetics \u306e StartCanary<\/a> \u3092\u5b9f\u884c\u3059\u308b\u3060\u3051\u306e\u51e6\u7406\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

package main\n\nimport (\n    "log"\n    "os"\n\n    "github.com\/aws\/aws-lambda-go\/lambda"\n    "github.com\/aws\/aws-sdk-go\/aws"\n    "github.com\/aws\/aws-sdk-go\/aws\/session"\n    "github.com\/aws\/aws-sdk-go\/service\/synthetics"\n)\n\nconst region = "ap-northeast-1"\n\nfunc main() {\n    lambda.Start(startSynthetics)\n}\n\nfunc startSynthetics() error {\n    name := os.Getenv("CANARY_NAME")\n    sess, err := session.NewSession(&aws.Config{Region: aws.String(region)})\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    svc := synthetics.New(sess)\n    input := &synthetics.StartCanaryInput{\n        Name: aws.String(name),\n    }\n    _, err = svc.StartCanary(input)\n    if err != nil {\n        log.Fatal(err)\n    }\n    return nil\n}<\/code><\/pre>\n

\u6c4e\u7528\u7684\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u3001Canary\u540d\u3092 CANARY_NAME<\/code> \u306e\u74b0\u5883\u5909\u6570\u304b\u3089\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002 <\/p>\n

serverless framework \u306e\u8a2d\u5b9a<\/h3>\n

\u5148\u7a0b\u4f5c\u6210\u3057\u305f\u95a2\u6570\u3092\u3001 serverless framework<\/a> \u3092\u4f7f\u3063\u3066\u30c7\u30d7\u30ed\u30a4\u3057\u307e\u3059\u3002
\nserverless framework<\/code> \u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb serverless.yml<\/code> \u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n

service: synthetics-start\nframeworkVersion: '2.23.0'\n\nprovider:\n  name: aws\n  runtime: go1.x\n  stage: prd\n  region: ap-northeast-1\n  timeout: 300\n  memorySize: 128\n  iamManagedPolicies:\n    - "arn:aws:iam::aws:policy\/CloudWatchSyntheticsFullAccess"\n  iamRoleStatements:\n    - Effect: Allow\n      Action:\n        - logs:CreateLogGroup\n        - logs:CreateLogStream\n        - logs:PutLogEvents\n      Resource: "arn:aws:logs:*:*:*"\n  environment:\n    CANARY_NAME: ${env:CANARY_NAME}\n\npackage:\n  exclude:\n    - .\/**\n  include:\n    - .\/bin\/**\n\nfunctions:\n  syntheticsstart:\n    handler: bin\/app\n    events:\n      - schedule: cron(0 0 * * ? *)<\/code><\/pre>\n

\u30dd\u30a4\u30f3\u30c8\u3068\u3057\u3066\u306f\u3001 iamManagedPolicies<\/code> \u306b CloudWatchSyntheticsFullAccess<\/code> \u3092\u4ed8\u4e0e\u3059\u308b\u3053\u3068\u3067\u3059\u3002
\n\u3053\u308c\u304c\u306a\u3044\u3068\u3001Lambda \u304b\u3089 CloudWatch Synthetics<\/code> \u304c\u5b9f\u884c\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n

iamManagedPolicies:\n  - "arn:aws:iam::aws:policy\/CloudWatchSyntheticsFullAccess"<\/code><\/pre>\n

CloudWatchSyntheticsFullAccess<\/code> \u3092\u4ed8\u4e0e\u3059\u308b\u3053\u3068\u306b\u62b5\u6297\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e\u30da\u30fc\u30b8\u3092\u53c2\u8003\u306b\u3088\u308a\u53b3\u5bc6\u306a\u6a29\u9650\u3092\u8a2d\u5b9a\u3057\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002<\/p>\n