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

[JAWS-UG CLI] IAM:#27 IAMロールの作成 (Lambda: Basic execution)

More than 3 years have passed since last update.

前提条件

IAMへの権限

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

AWS CLIのバージョン

コマンド
aws --version
結果(例)
      aws-cli/1.9.0 Python/2.7.5 Darwin/13.4.0 botocore/1.3.0

0. 準備

変数の確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       iamFull-prjZ-mbp13iamFull-prjZ-mbp13              env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                eu-west-1              env    AWS_DEFAULT_REGION

1. 事前作業

1.1. IAMロール名の決定

変数の設定
IAM_ROLE_NAME='lambdaBasicExecution'

同じ名前のIAMロールが存在しないことを確認します。

コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}
結果(例)
      A client error (NoSuchEntity) occurred when calling the GetRole operation: The role with name lambdaBasicExecution cannot be found.

2. ロールの作成

2.1. assumeロールの決定

変数の設定
FILE_ROLE_DOC="${IAM_ROLE_NAME}.json" \
        && echo ${FILE_ROLE_DOC}
コマンド
cat << EOF > ${FILE_ROLE_DOC}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF

cat ${FILE_ROLE_DOC}
コマンド
jsonlint -q ${FILE_ROLE_DOC}

2.2. ロールの作成

変数の確認
cat << ETX

        IAM_ROLE_NAME: ${IAM_ROLE_NAME}
        FILE_ROLE_DOC: ${FILE_ROLE_DOC}

ETX
コマンド
aws iam create-role \
        --role-name ${IAM_ROLE_NAME} \
        --assume-role-policy-document file://${FILE_ROLE_DOC}
結果(例)
      {
        "Role": {
          "AssumeRolePolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                  {
                      "Action": "sts:AssumeRole",
                      "Sid": "",
                      "Effect": "Allow",
                      "Principal": {
                          "Service": "lambda.amazonaws.com"
                      }
                  }
              ]
          },
          "RoleId": "AROAJOMDKX4MPARLRM5CO",
          "CreateDate": "2015-10-23T02:05:55.750Z",
          "RoleName": "lambdaBasicExecution",
          "Path": "/",
          "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution"
        }
      }
コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}
結果(例)
      {
        "Role": {
          "AssumeRolePolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                  {
                      "Action": "sts:AssumeRole",
                      "Principal": {
                          "Service": "lambda.amazonaws.com"
                      },
                      "Effect": "Allow",
                      "Sid": ""
                  }
              ]
          },
          "RoleId": "AROAJOMDKX4MPARLRM5CO",
          "CreateDate": "2015-10-23T02:05:55Z",
          "RoleName": "lambdaBasicExecution",
          "Path": "/",
          "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution"
        }
      }

3. ロールポリシー (AWS管理ポリシーの場合)

3.1. ポリシーの決定

存在するAWS管理ポリシーを確認します。

コマンド
aws iam list-policies \
        --scope AWS \
        --query 'Policies[].PolicyName' \
        --max-items 1000

利用するIAMポリシーを決めます。

変数の設定
IAM_POLICY_NAME='AWSLambdaBasicExecutionRole'

ARNを取得します。

コマンド
IAM_POLICY_ARN=$( \
        aws iam list-policies \
          --scope AWS \
          --max-items 1000 \
          --query "Policies[?PolicyName==\`${IAM_POLICY_NAME}\`].Arn" \
          --output text \
      ) \
        && echo "${IAM_POLICY_ARN}"
結果(例)
arn:aws:iam::aws:policy/AWSLambdaBasicExecutionRole

ポリシーのバージョンを取得します。

コマンド
IAM_POLICY_VERSION=$( \
        aws iam list-policies \
          --scope AWS \
          --max-items 1000 \
          --query "Policies[?PolicyName==\`${IAM_POLICY_NAME}\`].DefaultVersionId" \
          --output text \
      ) \
        && echo ${IAM_POLICY_VERSION}
結果(例)
      v1

ポリシーの内容を見てみましょう。

コマンド
aws iam get-policy-version \
        --policy-arn ${IAM_POLICY_ARN} \
        --version-id ${IAM_POLICY_VERSION}
結果(例)
{
    "PolicyVersion": {
        "CreateDate": "2015-04-09T15:03:43Z", 
        "VersionId": "v1", 
        "Document": {
            "Version": "2012-10-17", 
            "Statement": [
                {
                    "Action": [
                        "logs:CreateLogGroup", 
                        "logs:CreateLogStream", 
                        "logs:PutLogEvents"
                    ], 
                    "Resource": "*", 
                    "Effect": "Allow"
                }
            ]
        }, 
        "IsDefaultVersion": true
    }
}

3.2. ポリシーの確認

ポリシーを確認します。

コマンド
aws iam list-attached-role-policies \
        --role-name ${IAM_ROLE_NAME}
結果
      {
          "AttachedPolicies": [], 
      }

3.3. ポリシーの適用

ポリシーをロールに適用します。

変数の確認
cat << ETX

        IAM_ROLE_NAME:  ${IAM_ROLE_NAME}
        IAM_POLICY_ARN: ${IAM_POLICY_ARN}

ETX
コマンド
aws iam attach-role-policy \
        --role-name ${IAM_ROLE_NAME} \
        --policy-arn ${IAM_POLICY_ARN}
結果
(戻り値なし)

3.4. ポリシーの確認

ポリシーを確認します。

コマンド
aws iam list-attached-role-policies \
        --role-name ${IAM_ROLE_NAME}
結果
      {
        "AttachedPolicies": [
          {
              "PolicyName": "lambdaExecution",
              "PolicyArn": "service-role/AWSLambdaBasicExecutionRole"
          }
        ]
      }

4. 事後作業

最後に再度、ロールとロールに適用されているポリシーを確認しておきましょう。

4.1. ロールの確認

コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}
結果(例)
      {
        "Role": {
          "AssumeRolePolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                  {
                      "Action": "sts:AssumeRole",
                      "Principal": {
                          "Service": "lambda.amazonaws.com"
                      },
                      "Effect": "Allow",
                      "Sid": ""
                  }
              ]
          },
          "RoleId": "AROAICY7DJANABQMFHX6G",
          "CreateDate": "2015-10-23T01:24:42Z",
          "RoleName": "lambdaBasicExecution",
          "Path": "/",
          "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution"
        }
      }

4.2. ロールに適用されているポリシーの確認

コマンド
aws iam list-attached-role-policies \
        --role-name ${IAM_ROLE_NAME}
結果
      {
        "AttachedPolicies": [
          {
              "PolicyName": "lambdaBasicExecution",
              "PolicyArn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
          }
        ],
        "IsTruncated": false
      }

完了

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした