

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

# AWS Serverless Application Repository 以身分為基礎的政策範例
<a name="security_iam_id-based-policy-examples"></a>

根據預設，IAM 使用者和角色不具備建立或修改 AWS Serverless Application Repository 資源的許可。他們也無法使用 AWS 管理主控台 AWS CLI或 AWS API 執行任務。IAM 管理員必須建立 IAM 政策，授予使用者和角色在指定資源上執行特定 API 作業的所需許可。管理員接著必須將這些政策連接至需要這些許可的 IAM 使用者或群組。

若要了解如何使用這些範例 JSON 政策文件建立 IAM 身分型政策，請參閱《*IAM 使用者指南*》中的[在 JSON 標籤上建立政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html#access_policies_create-json-editor)。

**Topics**
+ [政策最佳實務](#security_iam_service-with-iam-policy-best-practices)
+ [使用 AWS Serverless Application Repository 主控台](#security_iam_id-based-policy-examples-console)
+ [允許使用者檢視自己的許可](#security_iam_id-based-policy-examples-view-own-permissions)
+ [客戶受管政策範例](#security_iam_id-examples)

## 政策最佳實務
<a name="security_iam_service-with-iam-policy-best-practices"></a>

身分型政策相當強大。他們會判斷您帳戶中的某個人員是否可以建立、存取或刪除 AWS Serverless Application Repository 資源。這些動作可能會對您的帳戶產生成本 AWS 。當您建立或編輯身分型政策時，請遵循下列準則及建議事項：
+ **授予最低權限**：當您建立自訂政策時，請只授予執行任務所需要的許可。以最小一組許可開始，然後依需要授予額外的許可。這比一開始使用太寬鬆的許可，稍後再嘗試將他們限縮更為安全。如需詳細資訊，請參閱*《IAM 使用者指南》*中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。
+ **為敏感操作啟用 MFA**：為了增加安全，請要求 IAM 使用者使用多重要素驗證 (MFA) 存取敏感資源或 API 操作。如需詳細資訊，請參閱*《IAM 使用者指南》*中的[在 AWS中使用多重要素驗證 (MFA)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa.html)。
+ **使用原則條件以增加安全** – 在切實可行的範圍中，請定義您身分類型原則允許存取資源的條件。例如，您可以撰寫條件，指定請求必須來自一定的允許 IP 地址範圍。您也可以撰寫條件，只在指定的日期或時間範圍內允許請求，或是要求使用 SSL 或 MFA。如需詳細資訊，請參閱*《IAM 使用者指南》*中的 [IAM JSON 政策元素：條件](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html)。

## 使用 AWS Serverless Application Repository 主控台
<a name="security_iam_id-based-policy-examples-console"></a>

 AWS Serverless Application Repository 主控台提供整合式環境，讓您探索和管理 AWS Serverless Application Repository 應用程式。主控台提供功能和工作流程，除了 中記載的 API 特定許可之外，通常還需要管理 AWS Serverless Application Repository 應用程式的許可[AWS Serverless Application Repository API 許可：動作和資源參考](serverlessrepo-api-permissions-ref.md)。

如需使用 AWS Serverless Application Repository 主控台所需許可的詳細資訊，請參閱 [客戶受管政策範例](#security_iam_id-examples)。

## 允許使用者檢視自己的許可
<a name="security_iam_id-based-policy-examples-view-own-permissions"></a>

此範例會示範如何建立政策，允許 IAM 使用者檢視附加到他們使用者身分的內嵌及受管政策。此政策包含在主控台或使用 或 AWS CLI AWS API 以程式設計方式完成此動作的許可。

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ViewOwnUserInfo",
            "Effect": "Allow",
            "Action": [
                "iam:GetUserPolicy",
                "iam:ListGroupsForUser",
                "iam:ListAttachedUserPolicies",
                "iam:ListUserPolicies",
                "iam:GetUser"
            ],
            "Resource": ["arn:aws:iam::*:user/${aws:username}"]
        },
        {
            "Sid": "NavigateInConsole",
            "Effect": "Allow",
            "Action": [
                "iam:GetGroupPolicy",
                "iam:GetPolicyVersion",
                "iam:GetPolicy",
                "iam:ListAttachedGroupPolicies",
                "iam:ListGroupPolicies",
                "iam:ListPolicyVersions",
                "iam:ListPolicies",
                "iam:ListUsers"
            ],
            "Resource": "*"
        }
    ]
}
```

## 客戶受管政策範例
<a name="security_iam_id-examples"></a>

本節的範例提供一組範本政策可供您連接到使用者。如果您在建立政策方面是新手，我們建議您先在自己的帳戶中建立 IAM 使用者，接著將政策連接到該使用者，您也可以使用這些範例來建立單一自訂政策，其中包含執行多個動作的許可，然後將其連接至使用者。

 如需如何將政策連接至使用者的詳細資訊，請參閱《*IAM 使用者指南*》中的[將許可新增至使用者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_change-permissions.html#users_change_permissions-add-console)。

**Topics**
+ [發佈者範例 1：允許發佈者列出應用程式](#security_iam_id-example-publisher-list-apps)
+ [發佈者範例 2：允許發佈者查看應用程式或應用程式版本的詳細資訊](#security_iam_id-example-publisher-view-app-details)
+ [發佈者範例 3：允許發佈者建立應用程式或應用程式版本](#security_iam_id-example-publisher-create-apps)
+ [發佈者範例 4：允許發佈者建立應用程式政策以共用應用程式](#security_iam_id-example-publisher-create-app-policies)
+ [使用者範例 1：允許使用者搜尋應用程式](#security_iam_id-example-consumer-search-apps)
+ [使用者範例 2：允許使用者查看應用程式的詳細資訊](#security_iam_id-example-consumer-view-app-details)
+ [使用者範例 3：允許使用者部署應用程式](#security_iam_id-example-consumer-deploy-apps)
+ [使用者範例 4：拒絕存取部署資產](#security_iam_id-example-consumer-deny-deployment-assets)
+ [消費者範例 5：防止消費者搜尋及部署公有應用程式](#access-control-identity-based-example-consumer-deny-public-apps)

### 發佈者範例 1：允許發佈者列出應用程式
<a name="security_iam_id-example-publisher-list-apps"></a>

您帳戶中的 IAM 使用者必須具有 `serverlessrepo:ListApplications` 操作的許可，才能查看主控台上的內容。當您授予這些許可時，主控台可以顯示在使用者所屬特定 AWS 區域中建立之 AWS 帳戶中 AWS Serverless Application Repository 的應用程式清單。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ListExistingApplications",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:ListApplications"
            ],
            "Resource": "*"
        }
    ]
}
```

------

 

### 發佈者範例 2：允許發佈者查看應用程式或應用程式版本的詳細資訊
<a name="security_iam_id-example-publisher-view-app-details"></a>

使用者可以選取 AWS Serverless Application Repository 應用程式並檢視應用程式的詳細資訊。這類詳細資訊包括作者、描述、版本和其他組態資訊。若要這樣做，使用者需要 AWS Serverless Application Repository的 `serverlessrepo:GetApplication` 和 `serverlessrepo:ListApplicationVersions` API 操作的許可。

在下列範例中，這些許可授予特定應用程式，其 Amazon Resource Name (ARN) 指定為 `Resource` 值。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ViewApplication",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:GetApplication",
                "serverlessrepo:ListApplicationVersions"
            ],
            "Resource": "arn:aws:serverlessrepo:{{us-east-1}}:{{111122223333}}:applications/{{application-name}}"
        }
    ]
}
```

------

 

### 發佈者範例 3：允許發佈者建立應用程式或應用程式版本
<a name="security_iam_id-example-publisher-create-apps"></a>

如果您想要允許使用者擁有建立 AWS Serverless Application Repository 應用程式的許可，您需要將許可授予 `serverlessrepo:CreateApplication`和 `serverlessrepo:CreateApplicationVersions`操作，如下列政策所示。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "CreateApplication",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:CreateApplication",
                "serverlessrepo:CreateApplicationVersion"
            ],
            "Resource": "*"
        }
    ]
}
```

------

 

### 發佈者範例 4：允許發佈者建立應用程式政策以共用應用程式
<a name="security_iam_id-example-publisher-create-app-policies"></a>

為了讓使用者與他人共用應用程式，您必須授予其建立應用程式政策的許可，如以下政策所示。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ShareApplication",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:PutApplicationPolicy",
                "serverlessrepo:GetApplicationPolicy"
            ],
            "Resource": "*"
        }
    ]
}
```

------

 

### 使用者範例 1：允許使用者搜尋應用程式
<a name="security_iam_id-example-consumer-search-apps"></a>

若要允許使用者搜尋應用程式，您必須為其授予以下權限。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "SearchApplications",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:SearchApplications"
            ],
            "Resource": "*"
        }
    ]
}
```

------

 

### 使用者範例 2：允許使用者查看應用程式的詳細資訊
<a name="security_iam_id-example-consumer-view-app-details"></a>

使用者可以選取 AWS Serverless Application Repository 應用程式並檢視應用程式的詳細資訊，例如作者、描述、版本和其他組態資訊。若要這樣做，使用者必須具有下列 AWS Serverless Application Repository 操作的許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "ViewApplication",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:GetApplication",
                "serverlessrepo:ListApplicationVersions"
            ],
            "Resource": "*"
        }
    ]
}
```

------

 

### 使用者範例 3：允許使用者部署應用程式
<a name="security_iam_id-example-consumer-deploy-apps"></a>

客戶若要部署應用程式，您必須授予其許可，允許其執行一些操作。以下政策提供客戶所需許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "DeployApplication",
            "Effect": "Allow",
            "Action": [
                "serverlessrepo:CreateCloudFormationChangeSet",
                "cloudformation:CreateChangeSet",
                "cloudformation:ExecuteChangeSet",
                "cloudformation:DescribeStacks"

            ],
            "Resource": "*"
        }
    ]
}
```

------

**注意**  
部署應用程式可能需要許可才能使用其他 AWS 資源。由於 AWS Serverless Application Repository 使用與 相同的基礎部署機制 CloudFormation，如需詳細資訊，請參閱[使用 AWS Identity and Access Management 控制存取](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html)。如需協助排除與許可相關的部署問題，請參閱[故障診斷：IAM 許可不足](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html#troubleshooting-errors-insufficient-iam-permissions)。

### 使用者範例 4：拒絕存取部署資產
<a name="security_iam_id-example-consumer-deny-deployment-assets"></a>

根據預設，當應用程式與 AWS 帳戶私下共用時，該帳戶中的所有使用者都可以存取相同帳戶中所有其他使用者的部署資產。下列政策可防止 帳戶中的使用者存取部署資產，這些資產存放在 的 Amazon S3 儲存貯體中 AWS Serverless Application Repository。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "DenyDeploymentAssetAccess",
            "Effect": "Deny",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::awsserverlessrepo-changesets*/*"
            ]
        }
    ]
}
```

------

### 消費者範例 5：防止消費者搜尋及部署公有應用程式
<a name="access-control-identity-based-example-consumer-deny-public-apps"></a>

您可以防止使用者對應用程式執行某些動作。

下列政策可透過將 `serverlessrepo:applicationType` 指定為 `public`，來套用至公有應用程式。該政策可透過將 `Effect` 指定為 `Deny`，來防止使用者執行某些動作。如需有關適用於 的條件金鑰的詳細資訊 AWS Serverless Application Repository，請參閱[適用於 的動作、資源和條件金鑰 AWS Serverless Application Repository](https://docs.aws.amazon.com/IAM/latest/UserGuide/list_awsserverlessapplicationrepository.html)。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Condition": {
                "StringEquals": {
                    "serverlessrepo:applicationType": "public"
                }
            },
            "Action": [
                "serverlessrepo:SearchApplications",
                "serverlessrepo:GetApplication",
                "serverlessrepo:CreateCloudFormationTemplate",
                "serverlessrepo:CreateCloudFormationChangeSet",
                "serverlessrepo:ListApplicationVersions",
                "serverlessrepo:ListApplicationDependencies"
            ],
            "Resource": "*",
            "Effect": "Deny"
        }
    ]
}
```

------

**注意**  
此政策陳述式也可以用作服務控制政策，並套用到 AWS 組織。如需服務控制政策的詳細資訊，請參閱*AWS Organizations 《 使用者指南*》中的[服務控制政策](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scp.html)。