Amazon web services Aws cloudformation-如何使用字符串参数来防止重复使用同一字符串
1.Amazon web services Aws cloudformation-如何使用字符串参数来防止重复使用同一字符串,amazon-web-services,amazon-cloudformation,Amazon Web Services,Amazon Cloudformation,1. 在我的代码中,字符串“HelloWorldApi”使用lots作为引用。 是否可以使用我定义的参数“APIName”来替换重复字符串“HelloWorldApi”以便在创建另一个堆栈并重命名API时不需要将这些字符串逐个更新? { "AWSTemplateFormatVersion": "2010-09-09", "Parameters": { "APIName": {
在我的代码中,字符串“HelloWorldApi”使用lots作为引用。
是否可以使用我定义的参数“APIName”来替换重复字符串“HelloWorldApi”
以便在创建另一个堆栈并重命名API时不需要将这些字符串逐个更新?
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"APIName": {
"Type": "String",
"Default": "HelloWorldApi"
}
},
"Resources": {
"HelloWorldApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Name": "hello-api",
"Description": "API used for practice",
"FailOnWarnings": true
}
},
"APIAuthorizer" :{
"Type" : "AWS::ApiGateway::Authorizer",
"Properties" : {
"RestApiId" : {
"Ref": "HelloWorldApi"
}
}
},
"BannerDBModel": {
"Type" : "AWS::ApiGateway::Model",
"Properties" : {
"Name" : "postBannerModel",
"RestApiId" : {
"Ref": "HelloWorldApi"
},
"Schema" : {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "ProductsInputModel",
"type": "object",
"properties": {
"url": {"type": "string"}
}
}
}
},
"PostRequestValidator": {
"Type" : "AWS::ApiGateway::RequestValidator",
"Properties" : {
"Name" : "PostRequestValidator",
"RestApiId" : {
"Ref": "HelloWorldApi"
}
}
},
"BannerResource": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"RestApiId": {
"Ref": "HelloWorldApi"
},
"ParentId": {
"Fn::GetAtt": [
"HelloWorldApi",
"RootResourceId"
]
},
"PathPart": "banner"
}
},
"getBannerMethod": {
"Type": "AWS::ApiGateway::Method",
"DependsOn": ["HelloWorldApi"],
"Properties": {
"RestApiId": {
"Ref": "HelloWorldApi"
},
"ResourceId": {
"Ref": "BannerResource"
},
"HttpMethod": "GET",
"AuthorizationType": "NONE"
}
},
"Deployment": {
"DependsOn": ["HelloWorldApi"],
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HelloWorldApi"
}
}
}
}
}
2.假设我已经通过下面的代码创建了一个堆栈,如果APIName的默认值意外设置为HelloWorld(与原始版本不同),那么在我运行updateStack后会立即显示错误吗?
接下来会发生什么?正在创建另一个API HelloWorld?
根据注释更新了代码
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"RestAPI": {
"Type": "String",
"Default": "HelloWorldApi"
}
},
"Resources": {
"RestAPI": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Name": "hello-api",
"Description": "API used for practice",
"FailOnWarnings": true
}
},
"APIAuthorizer" :{
"Type" : "AWS::ApiGateway::Authorizer",
"Properties" : {
"RestApiId" : {
"Ref": "RestAPI"
}
}
},
"BannerDBModel": {
"Type" : "AWS::ApiGateway::Model",
"Properties" : {
"Name" : "postBannerModel",
"RestApiId" : {
"Ref": "RestAPI"
},
"Schema" : {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "ProductsInputModel",
"type": "object",
"properties": {
"url": {"type": "string"}
}
}
}
}
}
}
要重用此模板,应将名为
HelloWorldApi
的资源重命名为更通用的资源
如果当前重命名了资源并尝试重新部署,则在重新部署API时,将删除与HelloWorldApi
API关联的所有资源以及API本身
您使用的字符串引用的是AWS::ApiGateway::RestApi
资源,而不是APIName
参数中的值。如果此时更新此参数值,则不会影响堆栈,因为它看起来好像未使用
总之,在Resources
中引用字符串HelloWorldApi
是指AWS::ApiGateway::RestApi
资源逻辑名称
确保资源与参数不共享相同的名称
模板如下所示
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"RestAPIName": {
"Type": "String",
"Default": "HelloWorldApi"
}
},
"Resources": {
"RestAPI": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Name": { "Ref": "RestAPIName" },
"Description": "API used for practice",
"FailOnWarnings": true
}
},
"APIAuthorizer" :{
"Type" : "AWS::ApiGateway::Authorizer",
"Properties" : {
"RestApiId" : {
"Ref": "RestAPI"
}
}
},
"BannerDBModel": {
"Type" : "AWS::ApiGateway::Model",
"Properties" : {
"Name" : "postBannerModel",
"RestApiId" : {
"Ref": "RestAPI"
},
"Schema" : {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "ProductsInputModel",
"type": "object",
"properties": {
"url": {"type": "string"}
}
}
}
}
}
}
那么,我应该如何修改代码以达到重用的目的呢?@CCCC我建议您按照资源的用途命名资源。例如,
HelloWorldApi
resource将变成RestAPI。在参数中设置的任何可配置的内容我也应该在下面更改吗?“RestApiId”:{“Ref”:“HelloWorldApi”}?此处的Ref
将成为Rest API资源的名称。因此,如果您的资源逻辑名称是RestApi
i,那么我将是“RestApiId”:{“Ref”:“RestApi”},我更新了上面的代码。请帮忙看看这是不是你的意思