gpt4 book ai didi

amazon-web-services - AWS cloudformation - 如何使用字符串参数来防止重复使用同一字符串

转载 作者:行者123 更新时间:2023-12-03 07:15:29 24 4
gpt4 key购买 nike

1.
在我的代码中,字符串“HelloWorldApi”使用很多作为引用。
是否可以使用我定义的参数“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"}
}
}
}
}
}
}

关于amazon-web-services - AWS cloudformation - 如何使用字符串参数来防止重复使用同一字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64591243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com