

這是新的 *CloudFormation 範本參考指南*。請更新您的書籤和連結。如需 CloudFormation 入門的說明，請參閱 [AWS CloudFormation 使用者指南](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

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

# `AWS::SecretsManager` 轉換程序
<a name="transform-aws-secretsmanager"></a>

本主題說明如何使用 `AWS::SecretsManager` 轉換和 [AWS::SecretsManager::RotationSchedule](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-secretsmanager-rotationschedule.html) 資源類型來指定 Lambda 函式，以執行秘密輪換。

`AWS::SecretsManager` 轉換是一種 CloudFormation 巨集，當在堆疊範本中參考它時，如果使用變更集來建立或更新堆疊，會為秘密輪換自動產生 Lambda 函式。Lambda 函式放置在已處理範本的巢狀堆疊中。它使用來自 [AWS Secrets Manager Rotation Lambda Functions](https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas) 儲存庫的函數範本，以 [AWS::SecretsManager::RotationSchedule](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-secretsmanager-rotationschedule.html) 資源的 [RotationType](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-secretsmanager-rotationschedule-hostedrotationlambda.html#cfn-secretsmanager-rotationschedule-hostedrotationlambda-rotationtype) 屬性值為基礎。

## Usage
<a name="aws-secretsmanager-usage"></a>

若要使用 `AWS::SecretsManager` 轉換，必須在 CloudFormation 範本的頂層宣告轉換。您無法將 `AWS::SecretsManager` 作為內嵌於任何其他範本區段的轉換使用。

宣告必須使用常值字串 `AWS::SecretsManager-2020-07-23` 或 `AWS::SecretsManager-2024-09-16` 作為其值。您不能使用參數或函數來指定轉換值。

### 語法
<a name="aws-secretsmanager-syntax"></a>

若要在您的 CloudFormation 範本中宣告此轉換，請使用下列語法：

#### JSON
<a name="aws-secretsmanager-syntax.json"></a>

```
{
  "Transform":"AWS::SecretsManager-{{2020-07-23}}",
  "Resources":{
    {{...}}
  }
}
```

#### YAML
<a name="aws-secretsmanager-syntax.yaml"></a>

```
Transform: AWS::SecretsManager-{{2020-07-23}}
Resources:
  {{...}}
```

`AWS::SecretsManager` 轉換是沒有其他參數的獨立宣告。反之，可以在堆疊範本中設定 [AWS::SecretsManager::RotationSchedule](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-secretsmanager-rotationschedule.html) 資源的 [HostedRotationLambda](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-secretsmanager-rotationschedule-hostedrotationlambda.html) 屬性。[HostedRotationLambda](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-properties-secretsmanager-rotationschedule-hostedrotationlambda.html) 屬性會指定 Lambda 函式，以執行秘密輪換。

## `AWS::SecretsManager-2024-09-16` 中的新功能
<a name="aws-secretsmanager-new-version"></a>

最新版本的 `AWS::SecretsManager` 轉換 (`AWS::SecretsManager-2024-09-16`) 引入了下列增強功能：
+ **自動 Lambda 升級** – 當您更新 CloudFormation 堆疊時，Lambda 函式現在會自動更新其執行時期組態和內部相依性。這可確保您使用最安全且可靠的程式碼版本，以管理 Secrets Manager 中的秘密輪換。
+ **支援其他屬性** – 新轉換在與 `HostedRotationLambda` 屬性搭配使用時，支援 `AWS::SecretsManager::RotationSchedule` 資源類型的其他資源屬性，包括 `DependsOn` 屬性。
**注意**  
這兩個版本都支援 `DeletionPolicy` 和 `UpdateReplacePolicy` 屬性。

若要進一步了解此新版本的`AWS::SecretsManager`轉換，請參閱 AWS 安全部落格上的[介紹增強版本的 AWS Secrets Manager 轉換：AWS::SecretsManager-2024-09-16](https://aws.amazon.com/blogs/security/introducing-an-enhanced-version-of-the-aws-secrets-manager-transform-awssecretsmanager-2024-09-16/)。

## 範例
<a name="aws-secretsmanager-examples"></a>

下列範例示範如何在範本中使用 `AWS::SecretsManager` 轉換 (`AWS::SecretsManager-2024-09-16`) 和 [AWS::SecretsManager::RotationSchedule](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-secretsmanager-rotationschedule.html) 資源。在此範例中，CloudFormation 將自動為 MySQL 單一使用者秘密輪換產生 Lambda 函式。

秘密設定為每天午夜自動輪換 (UTC)。輪換程序可能最多需要 2 小時才能完成。更新輪換排程不會立即開始輪換。

### JSON
<a name="aws-secretsmanager-example.json"></a>

```
{
  "AWSTemplateFormatVersion":"2010-09-09",
  "Transform":"AWS::SecretsManager-2024-09-16",
  "Resources":{

  {{...}}

    "{{MySecretRotationSchedule}}":{
      "Type":"AWS::SecretsManager::RotationSchedule",
      "DependsOn":"{{logical name of AWS::SecretsManager::SecretTargetAttachment resource}}",
      "Properties":{
        "SecretId":{
          "Ref":"{{logical name of AWS::SecretsManager::Secret resource}}"
        },
        "HostedRotationLambda":{
          "RotationType":"MySQLSingleUser",
          "RotationLambdaName":"{{name of Lambda function to be created}}",
          "VpcSecurityGroupIds":{
            "Fn::GetAtt":[
              "{{logical name of AWS::EC2::SecurityGroup resource}}",
              "GroupId"
            ]
          },
          "VpcSubnetIds":{
            "Fn::Join":[
              ",",
              [
                {
                  "Ref":"{{logical name of primary subnet}}"
                },
                {
                  "Ref":"{{logical name of secondary subnet}}"
                }
              ]
            ]
          }
        },
        "RotationRules":{
          "ScheduleExpression":"cron(0 0 * * ? *)",
          "Duration":"2h"
        },
        "RotateImmediatelyOnUpdate":false
      }
    }
  }
}
```

### YAML
<a name="aws-secretsmanager-example.yaml"></a>

```
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::SecretsManager-2024-09-16
Resources:

  {{...}}

  {{MySecretRotationSchedule}}:
    Type: AWS::SecretsManager::RotationSchedule
    DependsOn: {{logical name of AWS::SecretsManager::SecretTargetAttachment resource}}
    Properties:
      SecretId: !Ref {{logical name of AWS::SecretsManager::Secret resource}}
      HostedRotationLambda:
        RotationType: MySQLSingleUser
        RotationLambdaName: {{name of Lambda function to be created}}
        VpcSecurityGroupIds: !GetAtt {{logical name of AWS::EC2::SecurityGroup resource}}.GroupId
        VpcSubnetIds:
          Fn::Join:
          - ","
          - - Ref: {{logical name of primary subnet}}
            - Ref: {{logical name of secondary subnet}}
      RotationRules:
        ScheduleExpression: cron(0 0 * * ? *)
        Duration: 2h
      RotateImmediatelyOnUpdate: false
```

## 相關資源
<a name="aws-secretsmanager-related-resources"></a>

如需可用來設定秘密輪換的完整 CloudFormation 範本範例，請參閱 `AWS::SecretsManager::RotationSchedule` 資源的[範例](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-secretsmanager-rotationschedule.html#aws-resource-secretsmanager-rotationschedule--examples)一節。

如需有關使用巨集的一般資訊，請參閱《AWS CloudFormation 使用者指南》**中的[使用範本巨集在 CloudFormation 範本上執行自訂處理](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html)。