0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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}
  1. 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:*\"}}}"
  }
  1. 事後作業
    ===========

完了

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?