我尝试在 cloudformation 中启动 EC2 实例时从网络共享驱动器复制文件:
Resources:
MigrationInstance:
Type: 'AWS::EC2::Instance'
Properties:
...
UserData:
Fn::Base64: !Sub |
<powershell>
New-Item "C:\migratedFiles" -itemType Directory
#Copy-Item -Path \\192.168.41.103\shared\* -Destination C:\migratedFiles -Recurse -Force # also don't work
cfn-init.exe --verbose --stack ${AWS::StackName} --resource MigrationInstance --region ${AWS::Region}
</powershell>
<persist>true</persist>
Metadata:
AWS::CloudFormation::Init:
config:
files:
C:\windows\temp\copyfiles.ps1:
content: !Sub |
$sourcePathExist = Test-Path \\192.168.41.103\shared
if($sourcePathExist) #this is always false
{
# Copy-Item -Path \\192.168.41.103\shared\* -Destination C:\migratedFiles -Recurse -Force
xcopy \\192.168.41.103\shared C:\migratedFiles /E /H /C /I
Write-Output "Migrate files success"
}
else
{
Write-Warning "Migrate files failed"
}
commands:
1-RunCopyFiles:
command:
powershell.exe -executionpolicy bypass -file "C:\windows\temp\copyfiles.ps1"
waitAfterCompletion: 'forever'
但似乎在 cfn-init 中无法访问共享文件夹,因为 Test-Path\\192.168.41.103\shared
返回 false
..
但是当我在 EC2 实例中手动运行 Test-Path
时 - 它返回 true
并且 xcopy
命令起作用
xcopy\\192.168.41.103\shared C:\migeratedFiles/E/H/C/I
在我看来,在 UserData
和 cfn-init
中无法访问共享驱动器。但是,当您 SSM 进入实例并手动运行命令时,它是可以访问的(xcopy
工作并且 test-path
返回 true
)。我想知道为什么会这样?
最佳答案
原来实例需要加入AD域。但仍然令人困惑的是为什么我可以从 Ec2 手动访问它,即使它没有加入域。
关于amazon-web-services - AWS 云信息 : Network Share Drive is inaccessible in cfn-init or UserData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74397764/