

这是新的《CloudFormation 模板参考指南》**。请更新您的书签和链接。有关开始使用 CloudFormation 的帮助，请参阅《AWS CloudFormation 用户指南》[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

# 转换参考
<a name="transform-reference"></a>

转换是由 CloudFormation 托管的宏。与自定义宏不同，转换不需要任何特殊权限即可使用它，因为它由 CloudFormation 托管。转换可以在 CloudFormation 中的任何账户的模板中使用。此外，使用转换时不会产生任何费用。CloudFormation 在评估顺序和范围方面将转换视为与任何其他宏相同。

有关宏的工作原理的更多信息，请参阅《AWS CloudFormation 用户指南》**中的[使用 CloudFormation 宏对模板执行自定义处理](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html)。

您可以在 CloudFormation 模板中使用以下转换。

**Topics**
+ [`AWS::CodeDeployBlueGreen` 转换](transform-aws-codedeploybluegreen.md)
+ [`AWS::Include` 转换](transform-aws-include.md)
+ [`AWS::LanguageExtensions` 转换](transform-aws-languageextensions.md)
+ [`AWS::SecretsManager` 转换](transform-aws-secretsmanager.md)
+ [`AWS::Serverless` 转换](transform-aws-serverless.md)
+ [`AWS::ServiceCatalog` 转换](transform-aws-servicecatalog.md)

# `AWS::CodeDeployBlueGreen` 转换
<a name="transform-aws-codedeploybluegreen"></a>

本主题介绍如何使用 `AWS::CodeDeployBlueGreen` 转换功能，来通过 CodeDeploy 在堆栈上执行 ECS 蓝绿部署。

有关更多信息，请参阅《AWS CloudFormation 用户指南》中的[通过 CodeDeploy 使用 CloudFormation 执行 ECS 蓝绿部署](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/blue-green.html)**。

## 用法
<a name="aws-codedeploybluegreen-usage"></a>

要使用 `AWS::CodeDeployBlueGreen` 转换，您必须在 CloudFormation 模板的顶层对其进行声明。`AWS::CodeDeployBlueGreen` 不能用作嵌入在任何其他模板部分中的转换。

转换声明的值必须为文字字符串。您无法使用参数或函数来指定转换值。

### 语法
<a name="aws-codedeploybluegreen-syntax"></a>

要在 CloudFormation 模板中声明该转换，请使用以下语法：

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

```
{
  "Transform":[
    "AWS::CodeDeployBlueGreen"
  ],
  "Resources":{
    ...
  }
}
```

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

```
Transform:
  - 'AWS::CodeDeployBlueGreen'
Resources:
  ...
```

`AWS::CodeDeployBlueGreen` 转换是一个独立的声明，没有其他参数。

## 相关资源
<a name="aws-codedeploybluegreen-related-resources"></a>

有关可用于在堆栈上进行 ECS 蓝绿部署的完整的 CloudFormation 模板示例，请参阅《AWS CloudFormation 用户指南》中的[蓝绿部署模板示例](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/blue-green-template-example.html)**。

有关使用宏的一般信息，请参阅《AWS CloudFormation 用户指南》**中的 [使用模板宏对 CloudFormation 模板执行自定义处理](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html)。

# `AWS::Include` 转换
<a name="transform-aws-include"></a>

本主题介绍如何使用 `AWS::Include` 转换将样板文件内容插入到 CloudFormation 模板中。

`AWS::Include` 是一个 CloudFormation 宏，如果您在堆栈模板中引用了该宏，则会在您使用更改集创建或更新堆栈时在该转换在模板中的位置插入指定文件的内容。`AWS::Include` 函数的功能类似于编程语言中的 `include`、`copy` 或 `import` 指令。

## 用法
<a name="aws-include-usage"></a>

除模板参数部分和模板版本外，您可以在 CloudFormation 模板中的任意位置使用 `AWS::Include` 转换。例如，您可以在映射部分中使用 `AWS::Include`。

### 模板顶层的语法
<a name="aws-include-syntax-top-level"></a>

要在 CloudFormation 模板的顶层声明此转换，以作为 `Transform` 部分，请使用以下语法：

#### JSON
<a name="aws-include-syntax-top-level.json"></a>

```
{
  "Transform":{
    "Name":"AWS::Include",
    "Parameters":{
      "Location":"s3://amzn-s3-demo-bucket/MyFileName.json"
    }
  },
  "Resources":{
    ...
  }
}
```

#### YAML
<a name="aws-include-syntax-top-level.yaml"></a>

```
Transform:
  Name: AWS::Include
  Parameters:
    Location: 's3://amzn-s3-demo-bucket/MyFileName.yaml'
Resources:
  ...
```

### 将转换嵌入到模板中某个部分内的语法
<a name="aws-include-syntax-within-section"></a>

要在 CloudFormation 模板的某个部分中声明此转换，请使用 `Fn::Transform` 内置函数和以下语法：

#### JSON
<a name="aws-include-syntax-within-section.json"></a>

```
{
  "Fn::Transform":{
    "Name":"AWS::Include",
    "Parameters":{
      "Location":"s3://amzn-s3-demo-bucket/MyFileName.json"
    }
  }
}
```

#### YAML
<a name="aws-include-syntax-within-section.yaml"></a>

```
Fn::Transform:
  Name: AWS::Include
  Parameters:
    Location: s3://amzn-s3-demo-bucket/MyFileName.yaml
```

有关更多信息，请参阅 [`Fn::Transform`](intrinsic-function-reference-transform.md)。

### 参数
<a name="aws-include-parameters"></a>

`Location`

位置是 Amazon S3 URI，包含 S3 存储桶中的特定文件名。例如 `s3://amzn-s3-demo-bucket/MyFile.yaml`。

## 注意事项
<a name="aws-include-considerations"></a>

在使用 `AWS::Include` 时，请记住以下注意事项：有关使用宏的更多注意事项，请参阅《AWS CloudFormation 用户指南》中的[宏注意事项](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros-overview.html#template-macros-considerations)**。
+ 目前，我们支持 Amazon S3 URI，不支持其他 Amazon S3 格式（如 Amazon S3 ARN）。它必须是 Amazon S3 存储桶，而不是 GitHub 存储库等其他存储。
+ 任何有权访问 Amazon S3 URL 的人都可以在其模板中包含该代码段。
+ 模板代码段必须是有效的 JSON。
+ 模板代码段必须是有效的键值对象，例如 `"KeyName": "keyValue"`。
+ 不能使用 `AWS::Include` 来引用同时也使用 `AWS::Include` 的模板代码段。
+ 如果代码段发生更改，堆栈不会自动获取这些更改。要获取这些更改，您必须使用更新的代码段更新堆栈。更新堆栈时，请确保所包含的代码段未在您不知情的情况下发生更改。要在更新堆栈前进行验证，请检查更改集。
+ 创建模板和代码段时，您可以混用 YAML 和 JSON 模板语言。
+ 目前，我们不支持为 YAML 代码段使用简写形式。
+ 您可以提供具有 `AWS::Include` 的跨区域复制 Amazon S3 URI。确保在访问跨区域复制对象时检查 Amazon S3 存储桶名称。有关更多信息，请参阅*《Amazon S3 用户指南》*中的[在区域内和跨区域复制对象](https://docs.aws.amazon.com/AmazonS3/latest/userguide/replication.html)。

## 示例
<a name="aws-include-examples"></a>

以下示例演示如何使用 `AWS::Include` 转换来执行等待条件句柄。将 *amzn-s3-demo-bucket* 替换为实际存储桶名称。

首先，在您的 S3 存储桶中保存一个名为 `single_wait_condition.yaml` 的 YAML 文件，其中应包含以下内容：

```
MyWaitCondition:
  Type: AWS::CloudFormation::WaitCondition
  Properties:
    Handle: !Ref MyWaitHandle
    Timeout: '4500'
```

然后，您可以用 JSON 或 YAML 格式引用此文件。

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

```
{
   "Resources": {
      "MyWaitHandle": {
         "Type": "AWS::CloudFormation::WaitConditionHandle"
      },
      "Fn::Transform": {
         "Name": "AWS::Include",
         "Parameters": {
            "Location": "s3://amzn-s3-demo-bucket/single_wait_condition.yaml"
         }
      }
   }
}
```

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

```
Resources:
  MyWaitHandle:
    Type: AWS::CloudFormation::WaitConditionHandle
  Fn::Transform:
    Name: AWS::Include
    Parameters:
      Location: "s3://amzn-s3-demo-bucket/single_wait_condition.yaml"
```

有关更多信息，请参阅《AWS CloudFormation 用户指南》中的[在 CloudFormation 模板中创建等待条件](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-waitcondition.html)**。

# `AWS::LanguageExtensions` 转换
<a name="transform-aws-languageextensions"></a>

本主题介绍如何使用 `AWS::LanguageExtensions` 转换来启用默认情况下不可用的其他函数和功能。

`AWS::LanguageExtensions` 转换是一个 CloudFormation 宏，如果您在堆栈模板中引用了该宏，则会在您使用更改集创建或更新堆栈时将该转换定义的任何内置函数更新为解析的值。

通过在 CloudFormation 模板中包含此转换，您可以使用 `Fn::ForEach` 等额外的功能，从而支持迭代等更高级的操作。您还可以在通常不允许使用内置函数的位置使用内置函数，例如在 `Ref` 和 `Fn::GetAtt` 函数中。

## 用法
<a name="aws-languageextensions-usage"></a>

要使用 `AWS::LanguageExtensions` 转换，您必须在 CloudFormation 模板的顶层对其进行声明。`AWS::LanguageExtensions` 不能用作嵌入在任何其他模板部分中的转换。

该声明的值必须为文本字符串 `AWS::LanguageExtensions`。您无法使用参数或函数来指定转换值。

### 语法
<a name="aws-languageextensions-syntax"></a>

要在 CloudFormation 模板中声明该转换，请使用以下语法：

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

```
{
  "Transform":"AWS::LanguageExtensions",
  "Resources":{
    ...
  }
}
```

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

```
Transform: AWS::LanguageExtensions
Resources:
  ...
```

`AWS::LanguageExtensions` 转换是一个独立的声明，没有其他参数。

## 支持额外的函数
<a name="aws-languageextensions-supported-functions"></a>

`AWS::LanguageExtensions` 转换支持以下额外的函数：
+ [`Fn::ForEach`](intrinsic-function-reference-foreach.md)
+ [`Fn::Length`](intrinsic-function-reference-length.md)
+ [`Fn::ToJsonString`](intrinsic-function-reference-ToJsonString.md)

## 注意事项
<a name="aws-languageextensions-considerations"></a>

使用 `AWS::LanguageExtensions` 转换时，请记住以下注意事项：
+ 更新使用 `AWS::LanguageExtensions` 转换的堆栈时，建议不要使用 CloudFormation 控制台中的**使用现有模板**选项或等效的命令行选项 `--use-previous-template`。`AWS::LanguageExtensions` 转换会在处理过程中将参数解析为字面值。当您使用 `--use-previous-template` 时，CloudFormation 会将此已处理的模板与旧字面值一起使用，从而阻止应用新的参数值和 Systems Manager 参数更新。请改为提供原始模板，以确保使用当前值重新解析参数。
+ 仅在 `AWS::LanguageExtensions` 转换中可用的内置函数模板中不支持短格式 YAML 语法。使用对这些函数的显式引用。例如，使用 `Fn::Length` 而不是 `!Length`。
+ AWS SAM CLI 目前不支持 `AWS::LanguageExtensions` 转换的 `Fn::ForEach` 内置函数。
+ 如果您使用多个转换，请使用列表格式。如果您使用自定义宏，请将 AWS 提供的转换放在自定义宏之后。如果同时使用 `AWS::LanguageExtensions` 和 `AWS::Serverless` 转换，则 `AWS::LanguageExtensions` 转换必须位于列表中的 `AWS::Serverless` 转换之前。
+ `AWS::LanguageExtensions` 转换提供的函数和属性仅在模板的 `Resources`、`Conditions` 和 `Outputs` 部分中受支持。

## 示例
<a name="aws-languageextensions-examples"></a>

以下示例展示了如何使用 `AWS::LanguageExtensions` 转换来使用由该转换定义的 `Fn::Length` 内置函数。

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

```
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Transform": "AWS::LanguageExtensions",
    "Parameters": {
        "QueueList": {
            "Type": "CommaDelimitedList"
        },
        "QueueNameParam": {
            "Description": "Name for your SQS queue",
            "Type": "String"
        }
    },
    "Resources": {
        "Queue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "QueueName": {
                    "Ref": "QueueNameParam"
                },
                "DelaySeconds": {
                    "Fn::Length": {
                        "Ref": "QueueList"
                    }
                }
            }
        }
    }
}
```

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

```
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::LanguageExtensions
Parameters:
  QueueList:
    Type: CommaDelimitedList
  QueueNameParam:
    Description: Name for your SQS queue
    Type: String
Resources:
  Queue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Ref QueueNameParam
      DelaySeconds:
        'Fn::Length': !Ref QueueList
```

## 相关资源
<a name="aws-languageextensions-related-resources"></a>

有关更多示例，请参阅以下主题。
+ [`Fn::ForEach`](intrinsic-function-reference-foreach.md)
+ [`Fn::Length`](intrinsic-function-reference-length.md)
+ [`Fn::ToJsonString`](intrinsic-function-reference-ToJsonString.md)
+ [`Ref`](intrinsic-function-reference-ref.md)
+ [`Fn::GetAtt`](intrinsic-function-reference-getatt.md)
+ [`Fn::FindInMap enhancements`](intrinsic-function-reference-findinmap-enhancements.md)

有关使用宏的一般信息，请参阅《AWS CloudFormation 用户指南》**中的 [使用模板宏对 CloudFormation 模板执行自定义处理](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html)。

# `Fn::FindInMap enhancements`
<a name="intrinsic-function-reference-findinmap-enhancements"></a>

`AWS::LanguageExtensions` 转换增强了 CloudFormation 模板中 `Fn::FindInMap` 内置函数的功能。

`Fn::FindInMap` 函数用于从 CloudFormation 模板 `Mappings` 部分中定义的映射中检索值。但是，标准的 `Fn::FindInMap` 函数存在一些局限，例如无法处理缺失的映射，也无法使用嵌套了某些内置函数的 `Fn::FindInMap` 函数。

`AWS::LanguageExtensions` 转换通过引入以下增强功能来解决这些限制：
+ **默认值支持** – 您可以指定在找不到映射时返回默认值。
+ **内置函数支持** – 您还可以使用更广泛的内置函数来定义 `Fn::FindInMap` 的字段，而不是使用标准的 `Fn::FindInMap` 函数。

## 声明
<a name="intrinsic-function-reference-findinmap-enhancements-declaration"></a>

### JSON
<a name="intrinsic-function-reference-findinmap-enhancements-syntax.json"></a>

```
{ "Fn::FindInMap" : [
    "MapName",
    "TopLevelKey",
    "SecondLevelKey",
    {"DefaultValue": "DefaultValue"}
  ]
}
```

### YAML
<a name="intrinsic-function-reference-findinmap-enhancements-syntax.yaml"></a>

完整函数名称的语法：

```
Fn::FindInMap:
  - MapName
  - TopLevelKey
  - SecondLevelKey
  - DefaultValue: DefaultValue
```

短格式的语法：

```
!FindInMap
  - MapName
  - TopLevelKey
  - SecondLevelKey
  - DefaultValue: DefaultValue
```

## 参数
<a name="intrinsic-function-reference-findinmap-enhancements-parameters"></a>

DefaultValue  <a name="DefaultValue"></a>
如果在 `MapName` 映射上找不到 `TopLevelKey` 和/或 `SecondLevelKey`，则 `Fn::FindInMap` 将解析为的值。该字段是可选的。

所有参数 `MapName`、`TopLevelKey`、`SecondLevelKey` 和 `DefaultValue` 都可以是内部函数，只要其能够在转换期间解析为有效值。

## 示例
<a name="w2aac28c16c20c15"></a>

以下示例演示如何在添加 `AWS::LanguageExtensions` 转换时定义 `Fn::FindInMap` 的字段。

### 使用默认值
<a name="intrinsic-function-reference-findinmap-enhancements-example"></a>

以下是在 `Fn::FindInMap` 函数中使用默认值的示例：

#### JSON
<a name="intrinsic-function-reference-findinmap-default-value-example.json"></a>

```
{
  //...
    "Transform": "AWS::LanguageExtensions",
    //...
    "Fn::FindInMap": [
      "RegionMap",
      { "Ref": "AWS::Region" },
      "InstanceType",
      { "DefaultValue": "t3.micro" }
    ]
  //...
}
```

#### YAML
<a name="intrinsic-function-reference-findinmap-default-value-example.yaml"></a>

```
Transform: 'AWS::LanguageExtensions'
#...
    !FindInMap 
        - 'RegionMap'
        - !Ref 'AWS::Region'
        - 'InstanceType'
        - DefaultValue: t3.micro
#...
```

#### 使用内置函数定义顶层键
<a name="intrinsic-function-reference-findinmap-enhancements-example"></a>

以下示例使用 `Fn::FindInMap` 函数以及其中嵌入的 `Fn::Select` 和 `Fn::Split` 内置函数定义顶层键。

##### JSON
<a name="intrinsic-function-reference-findinmap-enhancement-example.json"></a>

```
{
  //...
  "Transform": "AWS::LanguageExtensions",
  //...
      "Fn::FindInMap": [
        "MyMap",
        {
          "Fn::Select": [
            0,
            {
              "Fn::Split": [
                "|",
                { "Ref": "InputKeys" }
              ]
            }
          ]
        },
        "SecondKey"
      ]
//...
}
```

##### YAML
<a name="intrinsic-function-reference-findinmap-enhance-example.yaml"></a>

```
Transform: 'AWS::LanguageExtensions'
#...
    !FindInMap: [MyMap, !Select [0, !Split [|, !Ref InputKeys]], SecondKey]
#...
```

## 支持的函数
<a name="intrinsic-function-reference-findinmap-enhancements-supported-functions"></a>

您可以在 `Fn::FindInMap:` 增强参数中使用以下函数：
+ ``Fn::FindInMap``
+ ``Fn::Join``
+ ``Fn::Sub``
+ ``Fn::If``
+ ``Fn::Select``
+ ``Fn::Length``
+ ``Fn::ToJsonString``
+ ``Fn::Split`` – 除非将其用作默认值，否则 `Fn::Split` 必须与生成字符串（例如 ``Fn::Join`` 或 ``Fn::Select``）的内部函数结合使用。
+ ``Ref``

## 相关资源
<a name="w2aac28c16c20c19"></a>

有关如何使用 `Fn::FindInMap` 内置函数的更多信息和示例，请参阅[`Fn::FindInMap`](intrinsic-function-reference-findinmap.md)。

有关 `AWS::LanguageExtensions` 转换的更多信息，请参阅 [`AWS::LanguageExtensions` 转换](transform-aws-languageextensions.md)。

# `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 轮换 Lambda 函数](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) 属性的值。

## 用法
<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 安全博客中的 [Introducing an enhanced version of the AWS Secrets Manager transform: 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)。

# `AWS::Serverless` 转换
<a name="transform-aws-serverless"></a>

本主题介绍如何使用 `AWS::Serverless` 转换处理用 AWS Serverless Application Model（AWS SAM）语法编写的模板处理并将其转换为符合规范的 CloudFormation 模板。

有关使用 `AWS::Serverless` 转换的更多信息，请参阅 GitHub 上的 [AWS SAM transform](https://github.com/aws/serverless-application-model)。

## 用法
<a name="aws-serverless-usage"></a>

要使用 `AWS::Serverless` 转换，您必须在 CloudFormation 模板的顶层对其进行声明。`AWS::Serverless` 不能用作嵌入在任何其他模板部分中的转换。

该声明的值必须为文本字符串 `AWS::Serverless-2016-10-31`。您无法使用参数或函数来指定转换值。

### 语法
<a name="aws-serverless-syntax"></a>

要在 CloudFormation 模板中声明该转换，请使用以下语法：

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

```
{
  "Transform":"AWS::Serverless-2016-10-31",
  "Resources":{
    ...
  }
}
```

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

```
Transform: AWS::Serverless-2016-10-31
Resources:
  ...
```

`AWS::Serverless` 转换是一个独立的声明，没有其他参数。

## 示例
<a name="aws-serverless-examples"></a>

以下示例展示了如何使用 `AWS::Serverless` 转换和 AWS SAM 语法，来简化 Lambda 函数及其执行角色的声明。

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

```
{
  "Transform":"AWS::Serverless-2016-10-31",
  "Resources":{
    "MyFunction":{
      "Type":"AWS::Serverless::Function",
      "Properties":{
        "Handler":"index.handler",
        "Runtime":"nodejs20.x",
        "CodeUri":"s3://amzn-s3-demo-bucket/MySourceCode.zip"
      }
    }
  }
}
```

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

```
Transform: AWS::Serverless-2016-10-31
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs20.x
      CodeUri: 's3://amzn-s3-demo-bucket/MySourceCode.zip'
```

从模板创建更改集时，CloudFormation 会按照该转换的定义展开 AWS SAM 语法。处理后的模板会展开 `AWS::Serverless::Function` 资源，并声明一个 Lambda 函数和一个执行角色。

```
{
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Handler": "index.handler",
        "Code": {
          "S3Bucket": "amzn-s3-demo-bucket",
          "S3Key": "MySourceCode.zip"
        },
        "Role": {
          "Fn::GetAtt": ["MyFunctionRole", "Arn"]
        },
        "Runtime": "nodejs20.x"
      }
    },
    "MyFunctionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "ManagedPolicyArns": ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"],
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",		 	 	 
          "Statement": [{
            "Action": ["sts:AssumeRole"],
            "Effect": "Allow",
            "Principal": {
              "Service": ["lambda.amazonaws.com"]
            }
          }]
        }
      }
    }
  }
}
```

## 将 `AWS::Serverless` 与 `AWS::LanguageExtensions` 搭配使用
<a name="aws-serverless-language-extensions"></a>

如果同时使用 `AWS::Serverless` 和 `AWS::LanguageExtensions`，当阶段名称作为非 `NoEcho` 参数值传递时，引用 `AWS::ApiGateway::Stage` 等资源需要特殊语法。

请使用 `Fn::Sub` 生成逻辑 ID 引用，而不要对引用 (`MyApi.Stage`) 使用 AWS SAM 语法。例如 `"Ref": {"Fn::Sub": "${MyApi}${StageName}Stage"}`。这将在运行时生成正确的逻辑 ID。

之所以使用此特殊格式是因为这两种转换对值的处理方式不同：
+ `AWS::LanguageExtensions` 将内置函数解析为其实际值。
+ `AWS::Serverless` 会创建不同的逻辑 ID，具体取决于它接收的是静态值还是内置函数。

## 相关资源
<a name="aws-serverless-related-resources"></a>

有关无服务器应用程序和 AWS Serverless Application Model（AWS SAM）的更多信息，请参阅 [AWS Serverless Application Model 开发人员指南](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html)。

有关特定于 AWS SAM 的资源和属性类型，请参阅《AWS Serverless Application Model 开发人员指南》中的 [AWS SAM resources and properties](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-resources-and-properties.html)**。

有关使用宏的一般信息，请参阅《AWS CloudFormation 用户指南》**中的 [使用模板宏对 CloudFormation 模板执行自定义处理](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html)。

# `AWS::ServiceCatalog` 转换
<a name="transform-aws-servicecatalog"></a>

本主题介绍了如何在 CloudFormation 模板中使用 `AWS::ServiceCatalog` 转换来引用现有 AWS Service Catalog 预置产品的输出。

## 用法
<a name="aws-servicecatalog-usage"></a>

要使用 `AWS::ServiceCatalog` 转换，您必须在 CloudFormation 模板的顶层对其进行声明。`AWS::ServiceCatalog` 不能用作嵌入在任何其他模板部分中的转换。

如果需要输出值，则需要提供预配置产品的名称和输出键名称。

您可以在模板中引用多个预配置产品和密钥名称，每个模板最多可引用 20 个预配置产品和密钥名称。在配置过程中，转换操作会从每个引用的预配置产品和密钥中检索值，并替换 CloudFormation 模板中的输出值。

该声明的值必须为文本字符串 `AWS::ServiceCatalog`。您无法使用参数或函数来指定转换值。

### 语法
<a name="aws-servicecatalog-syntax"></a>

要在 CloudFormation 模板中声明该转换，请使用以下语法：

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

```
{
  "Transform":"AWS::ServiceCatalog",
  "Resources":{
    ...
  }
}
```

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

```
Transform: AWS::ServiceCatalog
Resources:
  ...
```

`AWS::ServiceCatalog` 转换是一个独立的声明，没有其他参数。

## 示例
<a name="aws-servicecatalog-examples"></a>

以下示例展示了如何在 CloudFormation 模板中引用现有 Service Catalog 预置产品的输出。

在这些示例中，`SampleProvisionedProduct` 是先前创建的预配置产品。`SampleOutputKey` 是此预配置产品的输出键。

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

此示例为有效版本。

不将值包装为字符串文字的模板版本将会失败。

```
{
  "AWSTemplateFormatVersion":"2010-09-09",
  "Transform":"AWS::ServiceCatalog",
  "Resources":{
    "ExampleParameter":{
      "Type":"AWS::SSM::Parameter",
      "Properties":{
        "Type":"String",
        "Value":"[[servicecatalog:provisionedproduct:SampleProvisionedProduct:SampleOutputKey]]"
      }
    }
  }
}
```

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

示例 1–4 是有效模板。在示例 1 和 2 中，转换和值是字符串文字。

示例 5 是无效模板。值必须包装在字符串 `'`、`"` 或 `>-` 中。否则，会向用户发出错误。

```
// Example 1 
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::ServiceCatalog'
Resources:
  ExampleParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Type: String
      Value: '[[servicecatalog:provisionedproduct:SampleProvisionedProduct:SampleOutputKey]]'
     
// Example 2
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::ServiceCatalog
Resources:
  ExampleParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Type: String
      Value: '[[servicecatalog:provisionedproduct:SampleProvisionedProduct:SampleOutputKey]]'
     
     
// Example 3 
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::ServiceCatalog
Resources:
  ExampleParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Type: String
      Value: "[[servicecatalog:provisionedproduct:SampleProvisionedProduct:SampleOutputKey]]"
     
     
// Example 4 
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::ServiceCatalog
Resources:
  ExampleParameter:
    Type: AWS::SSM::Parameter
    Properties:
      Type: String
      Value: >-
        [[servicecatalog:provisionedproduct:SampleProvisionedProduct:SampleOutputKey]]
     
     
// Example 5 
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::ServiceCatalog
Resources:
  ExampleParameter2:
    Type: AWS::SSM::Parameter
    Properties:
      Type: String
      Value: [[servicecatalog:provisionedproduct:SSMProductProvisionedProduct:SampleOutputKey]]
```