[英]How to use !Sub or !Join in "AWS::CloudFormation::Init", file key
[英]AWS Cloudformation: Network Share Drive is inaccessible in cfn-init or UserData
在 cloudformation 中啟動 EC2 實例時,我試圖從 a.network 共享驅動器復制文件:
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:\migratedFiles /E /H /C /I
在我看來,共享驅動器在UserData
和cfn-init
中不可訪問。 但是,當您通過 SSM 進入實例並在那里手動運行命令時,它是可以訪問的( xcopy
有效並且test-path
返回true
)。 我想知道為什么會這樣?
所以事實證明實例需要加入 AD 域。 但仍然令人困惑為什么我可以手動從 Ec2 訪問它,即使它沒有加入域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.