2017-04-21 19 views
0

我有一個EC2實例從AWS運行一個老的AMI,它在我當前的設置中是從WINDOWS_2016_BASE。現在我知道了,但實例並不知道。AWS最新的ami imageId來自舊的imageid?

使用PowerShell我希望EC2實例創建更像自己,但不是從我創建的AMI(因爲這當然總是在舊版本中),而不是從實際的AMI ImageId「直接」創建由...創建。由於AWS不允許,如果它不是最新版本的(因此我需要獲得AMI ImageId的間接途徑,即使事實證明它是相同的,即如果它已經是最新版本的話)。

所以我需要找到,使用PowerShell的,我已經知道了AMI的最新版本(這是我從元數據了),我不想指定AMI例如WINDOWS_2016_BASE的名字,因爲我想機器創造更多像自己一樣,而不必硬編碼腳本中的內容。

我不知道如何做到這一點,因爲使用Get-EC2ImageByName -Names WINDOWS_2016_BASE不是我想要做的,因爲我不想硬編碼該名稱參數或通過userdata傳遞它。

Get-EC2Image -ImageId <an old imageid>由於AMI不再是最新而返回null。

+0

不幸的是,一旦AMI被棄用,就不可能檢索AMI的名稱。因此,您需要提供搜索的名稱。 –

回答

2

如果沒有在你的腳本可以提示的地方隱藏這個名字(你聲明你不想這樣做)或者依靠名稱/ amis(即Get -EC2ImageByName已經爲你做了)。

AMI元數據的設計使得可以很容易地檢索相關AMI的「家族樹」,假設它們的名稱相似,除了可能的細微差別,例如版本或日期。這是一種常見模式,相關AMI的前綴將保持一致,這使得它們更容易搜索和聚合。

這一點使得你的方法很困難,因爲你的腳本將會丟失那一點數據 - AMI名稱前綴。

您可以從metadata service動態檢索您的實例的AMI ID,並將其傳遞到Get-EC2Image以獲取AMI詳細信息,但是如果沒有某種名稱前綴匹配並用於搜索相關的更新的AMIs。

也許應重新考慮這種方法,並通過用戶數據將名稱前綴保存在標記或實例中?例如,我剛查過,英文Windows Server 2016 Base AMI都共享這個前綴:Windows_Server-2016-English-Full-Base。如果存儲了您的實例或作爲您的標籤之一,你的腳本可以檢索並運行以下PowerShell來獲取最新的Windows Server 2016 AMI:從系統日誌中

@(Get-EC2Image -Owner amazon -Filter @{ Name="name"; Values="Windows_Server-2016-English-Full-Base*" } | Sort CreationDate -Desc)[0].ImageId 
+0

等價的CLI:'aws ec2 describe-images --query images [*]。[ImageId,Name] --filters Name = name,Values = Windows_Server-2016-English-Full-Base- *',然後對結果進行排序。 –

+0

謝謝,這是一個很好的答案,但它是在問題停止的同一點,在那正是我不想做的事情成爲答案;-)我相信我已經找到了答案,我會現在發佈。 –

+0

@cineammispelt對,我想這個動力是我挑戰你的問題的前提,並促使你考慮爲什麼這個解決方案 - 或多或少是常態 - 不適合你使用案例,在深入兔子洞之前。 :) –

1

實例(這是我從拿到AWS控制檯視圖)

2016年12月26日14:36:12Z:AMI產地名稱:Windows_Server-2016 - 英語 - 全基地

等效PowerShell是:

GET-EC2ConsoleOutput

所以在這裏低於滿(我是新來的PowerShell,但我敢肯定有人可以換到這一點不知何故一個字符)

 


    # read the system console log 
    $consoleLog = Get-EC2ConsoleOutput $currentInstanceObj.InstanceId 
    $consoleLogOutput = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($consoleLog.Output)); 

    # extract the lines that contain AMI Origin in them (there should be 2) - sort them so name i first and version is second 
    $originLines = $consoleLogOutput -split '[\r\n]' | Where-Object {$_.Contains("AMI Origin")} | Sort-Object 

    # get the running name 
    $originLine = $originLines[0] 
    $originLineParts = $originLine.Split(':') 
    $originName = $originLineParts[$originLineParts.Length - 1].Trim() 
    "The origin name is $originName" 

    # get the running version (slighly pointless since the code below doesn't care as we want the latest - but it's for verbosity) 
    $originLine = $originLines[1] 
    $originLineParts = $originLine.Split(':') 
    $originVersion = $originLineParts[$originLineParts.Length - 1].Trim() 
    "The origin version is $originVersion" 

    # concatenate to get the original origin name (note: amazon have a naming pattern here - (name-version) 
    $amiName = $originName + "-" + $originVersion 
    "The original origin ami name is $amiName" 

    #find the latest of the same name and report the difference 
    $latestOriginObj = (Get-EC2Image -Filter @{ Name="name"; Values=($originName + "*")} | Sort-Object CreationDate -Descending | Select-Object -First 1) 

    if($latestOriginObj.ImageId -ne $currentInstanceObj.ImageId) 
    { 
     "The ami has been upgraded from " + ($currentInstanceObj.ImageId) + " to " + ($latestOriginObj.ImageId) 
    } 

    #....so go ahead and use the $latestOriginObj.ImageId when you create a new instance  

 

而知識來源,從這些amazons docs

彈簧摘錄如下:

AWS管理控制檯提供了有關用於創建Amazon EC2實例的AMI的詳細信息。 「描述」選項卡上的AMI ID字段包含Windows Server SKU,體系結構(32位或64位),AMI創建日期和AMI ID等信息。

如果AMI已被私有化或被更高版本取代並且不再列在目錄中,則AMI ID字段指出「無法加載ami-xxxxx的詳細信息,因此您可能不允許查看它。 「要確定使用哪個AMI創建實例,您必須打開系統日誌。在EC2控制檯中,選擇一個實例,然後從上下文菜單(右鍵單擊)中選擇實例設置,然後選擇獲取系統日誌。 AMI創建日期和SKU在AMI原始版本和AMI原始名稱字段中列出。