

これは新しい 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::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) プロパティの値に基づいて、[AWS Secrets Manager ローテーション Lambda 関数](https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas) リポジトリの関数テンプレートを使用します。

## 使用方法
<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` プロパティと一緒に使用すると、`DependsOn` 属性を含む `AWS::SecretsManager::RotationSchedule` リソースタイプの追加のリソース属性がサポートされます。
**注記**  
どちらのバージョンも `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 関数が自動的に生成されます。

シークレットは、毎日午前 0 時 (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)」を参照してください。