[英]How to pass parameters between one cloud formation to another cloud formation template
如何在aws中將一個雲形成模板文件之間的參數傳遞給另一個雲形成模板文件? 我不是在討論嵌套模板,因為我知道它們只是一個CFT中的單獨資源塊,如果我錯了請糾正我。
在堆棧之間傳遞參數是有意義的,而不是在模板之間傳遞 - 模板只是JSON結構。
你可以這樣做:
在第一個模板中,創建如下輸出值:
"Outputs" : {
"ParentVPC" : {
"Value" : {"Ref":"VPC"},
"Description" : "VPC ID"
},
...
}
然后,在第二個模板中,創建如下參數:
"Parameters" : {
"ParentVPC" : {
"Type" : "AWS::EC2::VPC::Id",
},
...
}
從第二個模板創建堆棧時,在從第一個模板創建describe-stack
上調用describe-stack
以獲取輸出值,並將它們作為參數傳遞給create-stack
。
查看有關跨堆棧引用的官方文檔以及如何使用它。
基本思想是,您將共享資源放入自己的堆棧中,以便其他堆棧可以引用這些資源。 這樣可以減少所有雲形成模板中的復制/粘貼。
例如,您可能有多個相關的Web應用程序對其服務器使用相同的安全組規則。 您可以將該安全組放入其自己的模板/堆棧中,將安全組ID聲明為輸出,然后在所有Web應用程序中,而不是在每個Web應用程序的每個雲編隊模板中多次定義相同的確切安全組。 'cf templates,您可以導入該安全組ID並為您的實例的安全組引用它。 由於網絡工程師可以管理VPC /子網/安全組周圍的安全性,因此可以減少重復代碼和關注點分離,您只需要擔心服務器/應用程序。
我能夠將參數從一個CFT傳遞到另一個CFT:我在子CFT中定義了參數,並在父CFT中傳遞了它們的值。 這是一個例子:
家長CFT(CFT1)
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Chef HA AWS Template",
"Parameters" : {
"ChefServerAWSRegion" : {
"Description" : "Choose deployment target from list",
"Type" : "String",
"AllowedValues" : ["deveast1", "qaeast1", "prodeast1","prodwest1","prodwest2"],
"Default": "deveast1"
}
},
"Mappings": {
"deveast1":
{
"chefstackurl":{
"frontend" : "https://s3.amazonaws.com/tbdchef/frontendinstance.json",
},
"a":
{
"sgBlueStripe": "sg-81c09fe5",
"sgSharedServices": "sg-82c09fe6",
}
}
}
}
子模板(定義值的地方,我保留了構建基礎結構的邏輯:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Chef HA AWS Template",
"Parameters": {
"sgBlueStripe": {
"Description": "sgBlueStripe",
"Type": "String"
},
"sgSharedServices": {
"Description": "sgSharedServices",
"Type": "String"
}
}
"Resources": {
"FrontendInstance":
{
"Type": "AWS::EC2::Instance",
"Properties": {
"DisableApiTermination": "true",
"SecurityGroupIds": [
{
"Ref": "sgBlueStripe"
},
{
"Ref": "sgSharedServices"
}
],
}
}
}
我認為你真正想要的是創建一個Lambda,它將檢索另一個cloudformation的輸出,並在你正在運行的雲形成中暴露它們。
這里有很好的解釋: http : //docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/walkthrough-custom-resources-lambda-cross-stack-ref.html
然后,您可以從NetworkStack
檢索輸出。
"NetworkInfo": {
"Type": "Custom::NetworkInfo",
"Properties": {
"ServiceToken": { "Fn::GetAtt" : ["LookupStackOutputs", "Arn"] },
"StackName": {
"Ref": "NetworkStackName"
}
}
}
通過這樣做來使用它們:
"SubnetId" : { "Fn::GetAtt": [ "NetworkInfo", "PublicSubnet" ] }
我自己使用它,它非常適合每月為Lambda提供的免費執行時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.