0
0

serverless frameworkを使って毎日AWSのコストをSlackに飛ばすlambda関数を作ってみる(ローカル開発環境準備)

Last updated at Posted at 2024-09-01

概要

serverless frameworkをつかってプライベートAWSアカウントのコストを毎日プライベートなSlackに飛ばすlambda関数などを作ってみる。
今回はそのためのローカル開発環境構築を行う。

詳細

  • PC: Mac(OS: 14.6.1)
  • Node.jsのバージョン管理: Volta
  • Node.jsバージョン: 20.11.1

方法

  • 下記の記事を参考にserverless frameworkの環境を準備

  • 上記の記事を参考に設定し、下記の様になったので前提条件は満たせていそう

    [12:35:30]~/workspace/02_study/serverless_framework$ npm -v
    10.2.4
    [12:39:05]~/workspace/02_study/serverless_framework$ node -v
    v20.11.1
    [12:39:07]~/workspace/02_study/serverless_framework$ volta -v
    1.1.1
    [12:39:13]~/workspace/02_study/serverless_framework$ aws -h
    
    usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
    To see help text, you can run:
    
      aws help
      aws <command> help
      aws <command> <subcommand> help
    
    aws: error: the following arguments are required: command
    
  • $ aws configureコマンドを実行してデプロイしたいAWSアカウントの情報が正しく設定されていることを確認した

  • ディレクトリaws-cost-notifyというディレクトリを作り、その中でserverless.ymlを作成し、下記のように記載

    aws-cost-notify/serverless.yml
    app: cost-monitoring
    service: aws-cost-notify
    
    provider:
      name: aws
      runtime: nodejs20.x
      region: ap-northeast-1
      iamRoleStatements:
        - Effect: "Allow"
          Action:
            - "ce:GetCostAndUsage"
          Resource: "*"
    
    functions:
      notify:
        handler: handler.notify
        name: aws-cost-notify
        events:
          - eventBridge:
              eventBus: default
              pattern:
                source:
                  - "aws.cost"
                detail-type:
                  - "AWS Cost and Usage Report"
    
  • aws-cost-notifyディレクトリにてコマンド$ serverlessを実行してserverless frameworkのプロジェクトを作成、作成過程で設定する内容があったので下記の様に指定(「Your Serverless Framework Service is ready to deploy.」のように出たのでデプロイ準備が整ったと思われる)

    ✔ Create Or Select An Existing App: · Create A New App
    
    ✔ Name Your New App: · aws-cost-notify
    Your Serverless Framework Service is ready to deploy.
    
  • aws-cost-notifyディレクトリにて下記を実行

    npm init -y
    npm install
    
  • aws-cost-notifyディレクトリ直下にhandler.jsを作成し、下記のように記入

    aws-cost-notify/handler.js
    exports.notify = async (event, context) => {
      try {
        console.log('コスト通知イベントを受信しました:', JSON.stringify(event));
    
        // ここにコスト通知ロジックを実装
        // 例: Slack通知、メール送信、データベース保存など
    
        return {
          statusCode: 200,
          body: JSON.stringify({ message: 'コスト通知が正常に処理されました' })
        };
      } catch (error) {
        console.error('エラーが発生しました:', error);
        return {
          statusCode: 500,
          body: JSON.stringify({ message: 'コスト通知の処理中にエラーが発生しました' })
        };
      }
    };
    
  • aws-cost-notifyディレクトリにて下記を実行して一旦今の状態でデプロイを実施

    serverless deploy
    
  • AWSのマネジメントコンソールにログインしLambdaに移動、「aws-cost-notify」という関数が作られているはずなので開く

  • テストのタブを開き、hello-worldテストのデフォルトで「key_value」というテストを作成

  • 作成したテストを実行し、下記のように値が返ったらデプロイ先のコードは問題ない

    CleanShot 2024-08-31 at 13.52.19@2x.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0