簡體   English   中英

如何檢查資源是否由 CloudFormation 創建?

[英]How can I check if a resource was created by CloudFormation?

我繼承了一個擁有大量資源的 AWS 賬戶。 其中一些是手動創建的,另一些是由 CloudFormation 創建的。

如何檢查資源(在我的例子中是安全組)是否由 CloudFormation 創建並屬於堆棧?

對於某些安全組aws ec2 describe-security-groups --group-ids real_id結果:

...
"Tags": [
            {
                "Value": "REAL_NAME",
                "Key": "aws:cloudformation:logical-id"
            },
            {
                "Value": "arn:aws:cloudformation:<REAL_ID>",
                "Key": "aws:cloudformation:stack-id"
            },
]
...

其他安全組沒有任何標簽。

它是唯一的指標嗎? 我的意思是,有人可以輕松地從 CloudFormation 創建的 SG 中刪除標簽。

根據官方文檔 ,除您定義的任何標簽外,AWS CloudFormation還會自動創建以下帶有前綴aws的堆棧級標簽:

aws:cloudformation: 邏輯ID

aws:cloudformation: 堆棧ID

aws:cloudformation: 堆棧名稱

所有堆棧級標簽(包括自動創建的標簽)都將傳播到AWS CloudFormation支持的資源。 當前,標簽不會傳播到通過塊設備映射創建的Amazon EBS卷

-

這應該是一個很好的起點,但是由於CF不會強制執行堆棧狀態,因此如果有人手動刪除了某些內容,您將永遠不會知道。

如果您是我,我將通過Cloudformer導出所有內容(受支持),然后以自己的方式重新設計整個設置。

其他方式:

您可以將資源的PhysicalResourceId傳遞給describe_stack_resources並獲取堆棧信息(如果它屬於CF堆棧)。 這是一個例子:

cf = boto3.client('cloudformation') cf.describe_stack_resources(PhysicalResourceId="i-0xxxxxxxxxxxxxxxx")

https://boto3.readthedocs.io/en/latest/reference/services/cloudformation.html#CloudFormation.Client.describe_stack_resources

我遇到過同樣的問題。 在沒有找到答案后,我做了一個快速的 PowerShell 腳本,它只會在所有堆棧中查找資源名稱。
當 CF 被引入時,堆棧沒有標記資源,即使是現在我對 CloudFormation 可靠地標記資源有問題,有時它仍然會標記一個資源而不標記另一個資源,即使資源類型相同且在同一堆棧中。 此外,CloudWatch Alarms 等一些資源沒有標簽。

$resourceName = "*MyResource*" #Part of the resource name, surrounded by asterisks (*)
$awsProfile = "Dev" #AWS Profile to use
$awsRegion = "us-east-1" #Region to query
Get-CFNStack -ProfileName $awsProfile -Region $awsRegion | Get-CFNStackResourceList -ProfileName $awsProfile -Region $awsRegion | Where-Object {$_.PhysicalResourceId -ilike $resourceName} | Select-Object StackName,PhysicalResourceId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM