本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Parameter Store 演练
本节中的演练介绍如何在Parameter Store测试环境中使用 创建、存储和运行参数。以下演练说明如何将 Parameter Store 与其他 Systems Manager 功能结合使用。此外,您还可以将 Parameter Store 与其他 AWS 服务结合使用。有关更多信息,请参阅什么是参数?.
创建 SecureString 参数并将实例加入域 (PowerShell)
本演练演示了如何使用 Windows Server Systems Manager 参数和 SecureString
将 Run Command. 实例加入域。演练中使用了典型的域参数,如域名和域用户名。这些值作为未加密的字符串值传递。将使用 AWS 托管客户主密钥 (CMK)
加密域密码并将其作为加密字符串传递。
Prerequisites
本演练假定您已在与 关联的 DHCP 选项集中指定域名和 DNS 服务器 IP 地址。Amazon VPC. 有关信息,请参阅 中的使用 DHCP Amazon VPC 用户指南选项集。
创建 SecureString
参数并将实例加入到域
-
使用 将参数输入到系统中。适用于 Windows PowerShell 的 AWS 工具.
Write-SSMParameter -Name "
domainName
" -Value "DOMAIN-NAME
" -Type String Write-SSMParameter -Name "domainJoinUserName
" -Value "DOMAIN\USERNAME
" -Type String Write-SSMParameter -Name "domainJoinPassword
" -Value "PASSWORD
" -Type SecureString重要 仅对
SecureString
参数的值 进行加密。参数的名称、描述和其他属性不会被加密。 -
将以下 IAM 策略附加到实例的 IAM 角色权限:
-
AmazonSSMManagedInstanceCore – 必需。该 AWS 托管策略允许托管实例使用 Systems Manager 服务核心功能。
-
AmazonSSMDirectoryServiceAccess – 必需。该 AWS 托管策略允许 SSM 代理 代表您访问 AWS Directory Service,以处理托管实例加入域的请求。
-
用于 S3 存储桶访问的自定义策略 – 必需。SSM 代理 位于您的实例上并执行 Systems Manager 任务,它需要访问 Amazon 拥有的特定 S3 存储桶。在您创建的自定义 S3 存储桶策略中,您还会提供执行 Systems Manager 操作所需的您自己的 S3 存储桶的访问权限。
示例:您可以将 Run Command 命令或 Session Manager 会话输出写入到一个 S3 存储桶中,以后将使用该输出进行审核或故障排除。您将访问脚本或自定义补丁基准列表存储在一个 S3 存储桶中,并在运行命令或应用补丁基准时引用该脚本或列表。
有关为 Amazon S3 存储桶访问创建自定义策略的信息,请参阅。为实例配置文件创建自定义 S3 存储桶策略
注意 在 S3 存储桶中保存输出日志数据是可选功能,但如果决定使用该功能,建议在 Systems Manager 配置过程开始时对其进行设置。有关更多信息,请参阅 https://docs.amazonaws.cn/AmazonS3/latest/gsg/CreatingABucket.html 中的Amazon Simple Storage Service 入门指南创建存储桶.
-
CloudWatchAgentServerPolicy – 可选。该 AWS 托管策略允许您在托管实例上运行 CloudWatch 代理 通过使用该策略,可以读取实例上的信息并将其写入到 中。Amazon CloudWatch. 只有在使用 EventBridge 或 等服务时,您的实例配置文件才需要此策略CloudWatch Logs。
注意 使用 CloudWatch 和 EventBridge 功能是可选的,但如果您决定使用它们,我们建议您在Systems Manager配置过程开始时对其进行设置。有关更多信息,请参阅 Amazon EventBridge 用户指南 和 Amazon CloudWatch Logs User Guide.
-
-
编辑附加到实例的 IAM 角色并添加以下策略。此策略授予实例调用
kms:Decrypt
API 的权限。{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":[ "kms:Decrypt" ], "Resource":[ "arn:aws:kms:
region
:account-id
:key/kms-key-id
" ] } ] } -
复制以下 json 示例并粘贴到简单文本编辑器中,并将文件保存为以下位置中的
JoinInstanceToDomain.json
c:\temp\JoinInstanceToDomain.json
.{ "schemaVersion": "2.2", "description": "Run a PowerShell script to securely domain-join a Windows Server instance", "mainSteps": [ { "action": "aws:runPowerShellScript", "name": "runPowerShellWithSecureString", "precondition": { "StringEquals": [ "platformType", "Windows" ] }, "inputs": { "runCommand": [ "$domain = (Get-SSMParameterValue -Name domainName).Parameters[0].Value", "if ((gwmi Win32_ComputerSystem).domain -eq $domain){write-host \"Computer is part of $domain, exiting\"; exit 0}", "$username = (Get-SSMParameterValue -Name domainJoinUserName).Parameters[0].Value", "$password = (Get-SSMParameterValue -Name domainJoinPassword -WithDecryption $True).Parameters[0].Value | ConvertTo-SecureString -asPlainText -Force", "$credential = New-Object System.Management.Automation.PSCredential($username,$password)", "Add-Computer -DomainName $domain -Credential $credential -ErrorAction SilentlyContinue -ErrorVariable domainjoinerror", "if($?){Write-Host \"Domain join succeeded, restarting\"; exit 3010}else{Write-Host \"Failed to join domain with error:\" $domainjoinerror; exit 1 }" ] } } ] }
-
在 适用于 Windows PowerShell 的 AWS 工具 中运行以下命令来创建新 SSM 文档。
$json = Get-Content C:\temp\JoinInstanceToDomain | Out-String New-SSMDocument -Name JoinInstanceToDomain -Content $json -DocumentType Command
-
在 适用于 Windows PowerShell 的 AWS 工具 中运行以下命令将实例加入到域
Send-SSMCommand -InstanceId
instance-id
-DocumentName JoinInstanceToDomain如果命令成功,系统将返回类似于以下内容的信息。
WARNING: The changes will take effect after you restart the computer EC2ABCD-EXAMPLE. Domain join succeeded, restarting Computer is part of example.local, exiting
如果命令失败,系统将返回类似于以下内容的信息。
Failed to join domain with error: Computer 'EC2ABCD-EXAMPLE' failed to join domain 'example.local' from its current workgroup 'WORKGROUP' with following error message: The specified domain either does not exist or could not be contacted.