如何在cloudformation中将参数从主模板发送到子模板
how to send the parameters from main template to child template in cloudformation
我正在尝试从用户那里获取两个参数,然后将其传递到我在主模板中指定的模板。在我的主模板中,我有:
{
"Parameters": {
"appName": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
},
"appEnv": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
}
},
"Resources": {
"CodeDeployEC2InstancesStack": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/............../beanstalk.json",
"TimeoutInMinutes": "60"
}
},
"myS3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": "PublicRead",
"BucketName": "ssssssss-test"
}
}
}
}
如您所见,我正在寻址 beantalk 模板,我得到了我将要创建的 beantalk 的环境和应用程序的名称,因此我需要将这些变量传递给 beantalk 模板。这是我的豆茎模板:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"appName": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
},
"appEnv": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
}
},
"Resources": {
"sampleApplication": {
"Type": "AWS::ElasticBeanstalk::Application",
"Properties": {
"Description": "AWS Elastic Beanstalk Sample Application",
"ApplicationName": {
"Ref": "appName"
}
}
},
"sampleApplicationVersion": {
"Type": "AWS::ElasticBeanstalk::ApplicationVersion",
"Properties": {
"ApplicationName": {
"Ref": "sampleApplication"
},
"Description": "AWS ElasticBeanstalk Sample Application Version",
"SourceBundle": {
"S3Bucket": "hamed-test-war",
"S3Key": "deployment.war"
}
}
},
"sampleConfigurationTemplate": {
"Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
"Properties": {
"ApplicationName": {
"Ref": "sampleApplication"
},
"Description": "AWS ElasticBeanstalk Sample Configuration Template",
"OptionSettings": [{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MinSize",
"Value": "2"
},
{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MaxSize",
"Value": "3"
},
{
"Namespace": "aws:elasticbeanstalk:environment",
"OptionName": "EnvironmentType",
"Value": "LoadBalanced"
}
],
"SolutionStackName": "64bit Amazon Linux 2017.03 v2.6.1 running Tomcat 8 Java 8"
}
},
"sampleEnvironment": {
"Type": "AWS::ElasticBeanstalk::Environment",
"Properties": {
"ApplicationName": {
"Ref": "sampleApplication"
},
"Description": "AWS ElasticBeanstalk Sample Environment",
"TemplateName": {
"Ref": "sampleConfigurationTemplate"
},
"VersionLabel": {
"Ref": "sampleApplicationVersion"
},
"EnvironmentName": {
"Ref": "appEnv"
}
}
}
},
"Outputs": {
"applicationName11": {
"Description": "The application chosen by user is :",
"Value": {
"Ref": "appEnv"
}
}
}
}
如您所见,我正在尝试使用 Ref appname 来获取变量,但这不起作用,因为 appname 仅在父模板中可用。所以我正在寻找一种将参数发送到 beanstalk.template 的方法。可以吗?
您似乎在使用嵌套堆栈来拉入模板。如果是这样,可以使用 Parameters
属性 将您的参数传递给子模板,如下所示:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Resources" : {
"myStackWithParams" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2ChooseAMI.template",
"Parameters" : {
"InstanceType" : "t1.micro",
"KeyName" : "mykey"
}
}
}
}
}
我正在尝试从用户那里获取两个参数,然后将其传递到我在主模板中指定的模板。在我的主模板中,我有:
{
"Parameters": {
"appName": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
},
"appEnv": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
}
},
"Resources": {
"CodeDeployEC2InstancesStack": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/............../beanstalk.json",
"TimeoutInMinutes": "60"
}
},
"myS3": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": "PublicRead",
"BucketName": "ssssssss-test"
}
}
}
}
如您所见,我正在寻址 beantalk 模板,我得到了我将要创建的 beantalk 的环境和应用程序的名称,因此我需要将这些变量传递给 beantalk 模板。这是我的豆茎模板:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"appName": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
},
"appEnv": {
"Description": "enter the app name",
"Type": "String",
"Default": "bn-test-jun"
}
},
"Resources": {
"sampleApplication": {
"Type": "AWS::ElasticBeanstalk::Application",
"Properties": {
"Description": "AWS Elastic Beanstalk Sample Application",
"ApplicationName": {
"Ref": "appName"
}
}
},
"sampleApplicationVersion": {
"Type": "AWS::ElasticBeanstalk::ApplicationVersion",
"Properties": {
"ApplicationName": {
"Ref": "sampleApplication"
},
"Description": "AWS ElasticBeanstalk Sample Application Version",
"SourceBundle": {
"S3Bucket": "hamed-test-war",
"S3Key": "deployment.war"
}
}
},
"sampleConfigurationTemplate": {
"Type": "AWS::ElasticBeanstalk::ConfigurationTemplate",
"Properties": {
"ApplicationName": {
"Ref": "sampleApplication"
},
"Description": "AWS ElasticBeanstalk Sample Configuration Template",
"OptionSettings": [{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MinSize",
"Value": "2"
},
{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MaxSize",
"Value": "3"
},
{
"Namespace": "aws:elasticbeanstalk:environment",
"OptionName": "EnvironmentType",
"Value": "LoadBalanced"
}
],
"SolutionStackName": "64bit Amazon Linux 2017.03 v2.6.1 running Tomcat 8 Java 8"
}
},
"sampleEnvironment": {
"Type": "AWS::ElasticBeanstalk::Environment",
"Properties": {
"ApplicationName": {
"Ref": "sampleApplication"
},
"Description": "AWS ElasticBeanstalk Sample Environment",
"TemplateName": {
"Ref": "sampleConfigurationTemplate"
},
"VersionLabel": {
"Ref": "sampleApplicationVersion"
},
"EnvironmentName": {
"Ref": "appEnv"
}
}
}
},
"Outputs": {
"applicationName11": {
"Description": "The application chosen by user is :",
"Value": {
"Ref": "appEnv"
}
}
}
}
如您所见,我正在尝试使用 Ref appname 来获取变量,但这不起作用,因为 appname 仅在父模板中可用。所以我正在寻找一种将参数发送到 beanstalk.template 的方法。可以吗?
您似乎在使用嵌套堆栈来拉入模板。如果是这样,可以使用 Parameters
属性 将您的参数传递给子模板,如下所示:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Resources" : {
"myStackWithParams" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2ChooseAMI.template",
"Parameters" : {
"InstanceType" : "t1.micro",
"KeyName" : "mykey"
}
}
}
}
}