问题描述
1。
在我的代码中,字符串“ HelloWorldApi”使用很多作为引用。
是否可以使用定义的参数“ APIName”替换重复字符串“ HelloWorldApi”,以免在我创建另一个堆栈并重命名API时不需要将字符串1换成1?
tap
2。
可以说我已经通过下面的代码创建了一个堆栈,如果APIName的默认值意外地设置为不同于原始版本的HelloWorld,那么在运行updateStack之后错误会立即显示吗?
那会发生什么?正在创建另一个API HelloWorld吗?
{
"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"
}
}
}
}
}
解决方法
要重用此模板,您应该将名为true
的资源重命名为更通用的名称。
如果您当前已重命名资源并尝试重新部署它,则将在重新部署API的同时删除与HelloWorldApi
API以及API本身相关的所有资源。
您使用的字符串引用的是HelloWorldApi
资源,而不是AWS::ApiGateway::RestApi
参数中的值。如果您现在更新此参数值,它将不会影响堆栈,因为它看起来好像没有被使用。
总而言之,在APIName
中对字符串HelloWorldApi
的引用是指Resources
资源的逻辑名。
确保资源的名称与参数不同。
模板如下所示
AWS::ApiGateway::RestApi