

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

# 在 中建立 AWS Secrets Manager 秘密 AWS CloudFormation
<a name="cloudformation"></a>

您可以使用 CloudFormation 範本中的 `[ AWS::SecretsManager::Secret](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-secret.html)` 資源，在 CloudFormation 堆疊中建立秘密，如 [建立秘密](cfn-example_secret.md) 中所示。

若要為 Amazon RDS 或 Aurora 建立管理員密碼，建議您在 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbcluster.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbcluster.html) 中使用 `ManageMasterUserPassword`。Amazon RDS 接著會為您建立秘密並管理輪換。如需詳細資訊，請參閱[受管輪換](rotate-secrets_managed.md)。

針對 Amazon Redshift 和 Amazon DocumentDB 憑證，首先使用 Secrets Manager 產生的密碼建立秘密，然後使用[動態參考](cfn-example_reference-secret.md)從秘密中擷取使用者名稱和密碼，當作新資料庫的憑證。接下來，使用 `[ AWS::SecretsManager::SecretTargetAttachment](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-secrettargetattachment.html)` 資源，將資料庫相關詳細資訊，新增至 Secrets Manager 必須輪換秘密的秘密。最後，若要開啟自動輪換，請使用 `[ AWS::SecretsManager::RotationSchedule](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-rotationschedule.html)` 資源並提供[輪換函數](reference_available-rotation-templates.md)和[排程](rotate-secrets_schedule.md)。請參閱以下範例：
+ [使用 Amazon Redshift 憑證建立秘密](cfn-example_Redshift-secret.md)
+ [使用 Amazon DocumentDB 憑證建立秘密](cfn-example_DocDB-secret.md)

使用 `[ AWS::SecretsManager::ResourcePolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-resourcepolicy.html)` 資源將資源政策連接至秘密。



如需使用 建立資源的資訊 CloudFormation，請參閱 CloudFormation 《 使用者指南》中的[了解範本基本概念](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html)。您也可以使用 AWS Cloud Development Kit (AWS CDK)。如需詳細資訊，請參閱 [AWS Secrets Manager 建構程式庫](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-secretsmanager-readme.html)。

## Secrets Manager 如何使用 AWS CloudFormation
<a name="how-asm-uses-cfn"></a>

當您使用主控台開啟輪換時，Secrets Manager 會使用 AWS CloudFormation 來建立輪換的資源。如果您在該過程中建立新的輪換函數， [https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html)會根據適當的 CloudFormation 建立 [輪換函數範本](reference_available-rotation-templates.md)。然後 CloudFormation 設定 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-rotationschedule.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-secretsmanager-rotationschedule.html)，這會設定秘密的輪換函數和輪換規則。您可以在開啟自動輪換之後，選擇橫幅中的**檢視堆疊**來檢視 CloudFormation 堆疊。

如需瞭解開啟自動輪換功能的相關資訊，請參閱[輪換 AWS Secrets Manager 秘密](rotating-secrets.md)。