我的嵌套堆栈需要位于主堆栈中的资源。例如:嵌套堆栈中的lambda函数需要DB配置
"ProjectsusgetProjectFinancialsLF": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "dev",
"S3Key": "test-lamda.zip",
"S3ObjectVersion": "9eNYbcI5EOuuut9igX2xpgbGCtKD1D4K"
},
"Environment": {
"Variables": {
"MYSQLDB_USER": {
"Ref": "DBuser"
},
"MYSQLDB_HOST": {
"Fn::GetAtt": [
"testDB",
"Endpoint.Address"
]
},
"MYSQLDB_DATABASE": {
"Ref": "DBname"
},
"MYSQLDB_PASSWORD": {
"Ref": "DBpass"
}
}
},
"Description": "A get project financials function",
"FunctionName": {
"Fn::Join": [
"-",
[
{
"Ref": "EnvType"
},
"getProjectFinancials"
]
]
},
"Handler": "src/controllers/projects.geFinancials",
"Role": {
"Fn::GetAtt": [
"LambdaExecutionRole",
"Arn"
]
},
"Runtime": "nodejs6.10"
},
"DependsOn": [
"LambdaExecutionRole"
]
},
所以我将所需的参数从我的主堆栈传递给嵌套的使用参数:
"FinancialStack": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/dev/child-cft.json",
"TimeoutInMinutes": "5",
"Parameters": {
"DBuser": {
"Ref": "DBuser",
"Type": "String"
},
"epmoliteDB": {
"Ref": "testDB",
"Type": "AWS::RDS::DBInstance"
},
"DBname": {
"Ref": "DBname",
"Type": "String"
},
"DBPass": {
"Ref": "DBpass",
"Type": "String"
},
"EnvType": {
"Ref": "EnvType",
"Type": "String"
},
"LambdaExecutionRole": {
"Ref": "LambdaExecutionRole",
"Type": "AWS::IAM::Role"
},
"ApiGatewayRestApi": {
"Ref": "ApiGatewayRestApi",
"Type": "AWS::ApiGateway::RestApi"
}
}
}
}
这就是我在嵌套堆栈中接收它们的方式:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation to generate testone shot deployment",
"Parameters": {
"DBuser": {
"Ref": "DBuser",
"Type": "String"
},
"epmoliteDB": {
"Ref": "testDB",
"Type": "AWS::RDS::DBInstance"
},
"DBname": {
"Ref": "DBname",
"Type": "String"
},
"DBPass": {
"Ref": "DBpass",
"Type": "String"
},
"EnvType": {
"Ref": "EnvType",
"Type": "String"
},
"LambdaExecutionRole": {
"Ref": "LambdaExecutionRole",
"Type": "AWS::IAM::Role"
},
"ApiGatewayRestApi": {
"Ref": "ApiGatewayRestApi",
"Type": "AWS::ApiGateway::RestApi"
}
},
然而,当我运行cloudformation脚本时,它无法创建嵌套堆栈。我是否将资源从主堆栈错误地传递到嵌套堆栈?
我应该在主堆栈的输出中导出参数并使用“Fn:ImportValue”将它们导入嵌套堆栈中吗?
答案 0 :(得分:1)
阻止这些模板发挥作用的因素有很多。
让我们从嵌套的堆栈模板开始。您不能在输入参数中使用"Ref"
内在函数。只是类型就够了。此外,并非所有内容都支持作为参数类型(here's the list),例如,"Type": "AWS::ApiGateway::RestApi"
不是有效的参数类型。如果不直接支持某些内容,请使用"String"
类型。事实上,对于嵌套堆栈,您可以使您的生活更轻松,只需使用"String"
类型。
下一步要修复的是AWS::CloudFormation::Stack
资源块。在这里,您为每个传递的"Type"
使用了"Parameters"
属性,但实际上您无法在那里指定类型。嵌套模板的工作是指定它期望的输入类型。
我强烈建议您花点时间阅读CloudFormation documentation。更好的是,阅读AWS制作的一些示例。 Here's a good example of nested stacks,请看看master.yaml。