在 Lambda 中檢視資源型 IAM 政策 - AWS Lambda

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 Lambda 中檢視資源型 IAM 政策

Lambda 支援對 Lambda 函數和層使用資源型許可政策。您可以使用資源型政策,將存取權授予其他 AWS 帳戶組織服務。以資源為基礎的政策適用於單一函式、版本、別名或 layer 版本。

Console
檢視函式以資源為基礎的政策
  1. 開啟 Lambda 主控台中的 函數頁面

  2. 選擇一個函數。

  3. 選擇 Configuration (組態),然後選擇 Permissions (權限)。

  4. 向下捲動至 Resource-based policy (資源型政策),然後選擇 View policy document (檢視政策文件)。資源型政策會顯示當另一個帳戶 AWS 或服務嘗試存取函數時所套用的許可。下列範例顯示的陳述式允許 Amazon S3 針對帳戶 123456789012 中名稱為 amzn-s3-demo-bucket 的儲存貯體,叫用名為 my-function 的函數。

    範例 以資源為基礎政策
    { "Version": "2012-10-17", "Id": "default", "Statement": [ { "Sid": "lambda-allow-s3-my-function", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:us-east-2:123456789012:function:my-function", "Condition": { "StringEquals": { "AWS:SourceAccount": "123456789012" }, "ArnLike": { "AWS:SourceArn": "arn:aws:s3:::amzn-s3-demo-bucket" } } } ] }
AWS CLI

若要檢查函式的以資源為基礎的政策,請使用 get-policy 命令。

aws lambda get-policy \ --function-name my-function \ --output text

您應該會看到下列輸出:

{"Version":"2012-10-17","Id":"default","Statement":[{"Sid":"sns","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":"lambda:InvokeFunction","Resource":"arn:aws:lambda:us-east-2:123456789012:function:my-function","Condition":{"ArnLike":{"AWS:SourceArn":"arn:aws:sns:us-east-2:123456789012:lambda*"}}}]} 7c681fc9-b791-4e91-acdf-eb847fdaa0f0

針對版本和別名,在函式名稱的後方附加版本編號或別名。

aws lambda get-policy --function-name my-function:PROD

若要將許可從函式中移除,請使用 remove-permission

aws lambda remove-permission \ --function-name example \ --statement-id sns

使用 get-layer-version-policy 指令檢視層級上的許可。

aws lambda get-layer-version-policy \ --layer-name my-layer \ --version-number 3 \ --output text

您應該會看到下列輸出:

b0cd9796-d4eb-4564-939f-de7fe0b42236    {"Sid":"engineering-org","Effect":"Allow","Principal":"*","Action":"lambda:GetLayerVersion","Resource":"arn:aws:lambda:us-west-2:123456789012:layer:my-layer:3","Condition":{"StringEquals":{"aws:PrincipalOrgID":"o-t194hfs8cz"}}}"

使用 remove-layer-version-permission 以從策略中移除陳述式。

aws lambda remove-layer-version-permission --layer-name my-layer --version-number 3 --statement-id engineering-org

支援的 API 動作

下列 Lambda API 動作支援資源型政策: