Help us understand the problem. What is going on with this article?

[JAWS-UG CLI] Lambda:#22 Permissionの追加 (CloudWatch Logs)

More than 3 years have passed since last update.

CloudWatch Logs に、関数を実行するためのアクセス権限を付与します。

前提条件

Lambdaへの権限

Lambdaに対してフル権限があること。

STSへの権限

STSに対して読み取り権限があること。

AWS CLI

以下のバージョンで動作確認済

  • AWS CLI 1.11.28
コマンド
aws --version
結果(例)
      aws-cli/1.11.7 Python/2.7.11 Darwin/15.6.0 botocore/1.4.64

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

1. 事前作業

1.1. AWS IDの取得

変数の設定
AWS_ID=$( \
        aws sts get-caller-identity \
          --query 'Account' \
          --output text \
) \
        && echo ${AWS_ID}

結果(例)

  XXXXXXXXXXXX

1.2. ソースの指定

変数の設定
LOGS_GROUP_NAME='RDSOSMetrics'
変数の設定
LAMBDA_SOURCE_ARN="arn:aws:logs:${AWS_DEFAULT_REGION}:${AWS_ID}:log-group:${LOGS_GROUP_NAME}:*" \
        && echo ${LAMBDA_SOURCE_ARN}

2. Permissionの追加

変数の設定
LAMBDA_STAT_ID='AllowLogs-datadog_process_rds_metrics-20161212'
LAMBDA_PERMIT_ACTION='lambda:InvokeFunction'
LAMBDA_PERMIT_PRINCIPAL="logs.${AWS_DEFAULT_REGION}.amazonaws.com"
変数の確認
cat << ETX

        LAMBDA_FUNC_NAME:        ${LAMBDA_FUNC_NAME}
        LAMBDA_STAT_ID:          ${LAMBDA_STAT_ID}
        LAMBDA_PERMIT_ACTION:    ${LAMBDA_PERMIT_ACTION}
        LAMBDA_PERMIT_PRINCIPAL: ${LAMBDA_PERMIT_PRINCIPAL}
        LAMBDA_SOURCE_ARN        ${LAMBDA_SOURCE_ARN}
        AWS_ID:                  ${AWS_ID}

ETX
コマンド
aws lambda add-permission \
        --function-name ${LAMBDA_FUNC_NAME} \
        --statement-id ${LAMBDA_STAT_ID} \
        --action ${LAMBDA_PERMIT_ACTION} \
        --principal ${LAMBDA_PERMIT_PRINCIPAL} \
        --source-arn ${LAMBDA_SOURCE_ARN} \
        --source-account ${AWS_ID}

結果(例):

  {
    "Statement": "{\"Sid\":\"AllowLogs-datadog_process_rds_metrics-20161212\",\"Resource\":\"arn:aws:lambda:\ |AWS_DEFAULT_REGION|\ :XXXXXXXXXXXX:function:\ |LAMBDA_FUNC_NAME|\ \",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"logs.\ |AWS_DEFAULT_REGION|\ .amazonaws.com\"},\"Action\":[\"lambda:InvokeFunction\"],\"Condition\":{\"StringEquals\":{\"AWS:SourceAccount\":\"XXXXXXXXXXXX\"},\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:logs:\ |AWS_DEFAULT_REGION|\ :XXXXXXXXXXXX:log-group:RDSOSMetrics:*\"}}}"
  }

3. 事後作業

完了

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away