

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为您的 AWS SAM 应用程序设置代码签名
<a name="authoring-codesigning"></a>

为确保仅部署可信代码，您可以使用 AWS SAM 对无服务器应用程序启用代码签名。对代码进行签名有助于确保代码在签名后未被更改，并且只有来自可信发布者的签名代码包才能在您的 Lambda 函数中运行。这有助于组织摆脱在部署管线中构建网关守卫组件的负担。

有关代码签名的更多信息，请参阅《AWS Lambda 开发人员指南》**中的[配置 Lambda 函数的代码签名](https://docs.aws.amazon.com/lambda/latest/dg/configuration-codesigning.html)。

在为无服务器应用程序配置代码签名之前，必须使用 S AWS igner 创建签名配置文件。您可以将此签名配置文件用于以下任务：

1. **创建代码签名配置** – 声明 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-codesigningconfig.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-codesigningconfig.html) 资源以指定可信发布者的签名配置文件并设置验证检查的策略操作。您可以在与无服务器函数相同的 AWS SAM 模板中声明此对象，也可以在不同的模板中声明此对象，也可以在 AWS SAM 模板中声明此对象 CloudFormation 。然后，您可以使用 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-codesigningconfig.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-codesigningconfig.html) 资源的 Amazon 资源名称（ARN）指定该函数的 [https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-codesigningconfigarn](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-codesigningconfigarn) 属性，从而为无服务器函数启用代码签名。

1. **签署代码** – 使用带 `--signing-profiles` 选项的 [https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html) 或 [https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html) 命令。

**注意**  
要使用 `sam package` 或 `sam deploy` 命令成功签署您的代码，必须为这些命令中使用的 Amazon S3 存储桶启用版本控制。如果您使用的是为您 AWS SAM 创建的 Amazon S3 存储桶，则会自动启用版本控制。有关 Amazon S3 存储桶版本控制的更多信息以及在您提供的 Amazon S3 存储桶上启用版本控制的说明，请参阅《Amazon Simple Storage Service 用户指南》**中的[在 Amazon S3 存储桶中使用版本控制](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Versioning.html)。

部署无服务器应用程序时，Lambda 会对您启用代码签名的所有函数执行验证检查。Lambda 还会对这些函数所依赖的任何层执行验证检查。有关 Lambda 验证检查的更多信息，请参阅《AWS Lambda 开发人员指南》**中的[签名验证](https://docs.aws.amazon.com/lambda/latest/dg/configuration-codesigning.html#config-codesigning-valid)。

## 示例
<a name="authoring-codesigning-example"></a>

### 创建签名配置文件
<a name="authoring-codesigning-example-signing-profile"></a>

要创建签名配置文件，请运行以下命令：

```
aws signer put-signing-profile --platform-id "AWSLambda-SHA384-ECDSA" --profile-name MySigningProfile
```

如果上一个命令成功，您将看到签名配置文件的 ARN 已返回。例如：

```
{
    "arn": "arn:aws:signer:us-east-1:111122223333:/signing-profiles/MySigningProfile",
    "profileVersion": "SAMPLEverx",
    "profileVersionArn": "arn:aws:signer:us-east-1:111122223333:/signing-profiles/MySigningProfile/SAMPLEverx"
}
```

`profileVersionArn` 字段包含创建代码签名配置时要使用的 ARN。

### 创建代码签名配置并为函数启用代码签名
<a name="authoring-codesigning-example-configure-trusted-deployments"></a>

以下示例 AWS SAM 模板声明[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-codesigningconfig.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-codesigningconfig.html)资源并启用 Lambda 函数的代码签名。在此示例中，有一个可信配置文件，如果签名检查失败，则部署将被拒绝。

```
Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7
      CodeSigningConfigArn: !Ref MySignedFunctionCodeSigningConfig

  MySignedFunctionCodeSigningConfig:
    Type: AWS::Lambda::CodeSigningConfig
    Properties:
      Description: "Code Signing for MySignedLambdaFunction"
      AllowedPublishers:
        SigningProfileVersionArns:
          - MySigningProfile-profileVersionArn
      CodeSigningPolicies:
        UntrustedArtifactOnDeployment: "Enforce"
```

### 签署代码
<a name="authoring-codesigning-example-signing-code"></a>

您可以在打包或部署应用程序时签署代码。使用 `sam package` 或 `sam deploy` 命令指定 `--signing-profiles` 选项，如以下示例命令所示。

在打包应用程序时对函数代码进行签名：

```
sam package --signing-profiles HelloWorld=MySigningProfile --s3-bucket amzn-s3-demo-bucket --output-template-file packaged.yaml
```

在打包应用程序时，对函数代码和函数所依赖的层进行签名：

```
sam package --signing-profiles HelloWorld=MySigningProfile MyLayer=MySigningProfile --s3-bucket amzn-s3-demo-bucket --output-template-file packaged.yaml
```

对函数代码和层进行签名，然后执行部署：

```
sam deploy --signing-profiles HelloWorld=MySigningProfile MyLayer=MySigningProfile --s3-bucket amzn-s3-demo-bucket --template-file packaged.yaml --stack-name --region us-east-1 --capabilities CAPABILITY_IAM
```

**注意**  
要使用 `sam package` 或 `sam deploy` 命令成功签署您的代码，必须为这些命令中使用的 Amazon S3 存储桶启用版本控制。如果您使用的是为您 AWS SAM 创建的 Amazon S3 存储桶，则会自动启用版本控制。有关 Amazon S3 存储桶版本控制的更多信息以及在您提供的 Amazon S3 存储桶上启用版本控制的说明，请参阅《Amazon Simple Storage Service 用户指南》**中的[在 Amazon S3 存储桶中使用版本控制](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Versioning.html)。

## 通过 `sam deploy --guided` 提供签名配置文件
<a name="authoring-codesigning-sam-deploy-guided"></a>

当您使用配置了代码签名的无服务器应用程序运行该`sam deploy --guided`命令时， AWS SAM 会提示您提供用于代码签名的签名配置文件。有关 `sam deploy --guided` 提示的更多信息，请参阅 AWS SAM CLI 命令参考中的 [sam deploy](sam-cli-command-reference-sam-deploy.md)。