[英]How to create stack for specific resource types from aws cloudformation template
I have cloudformation template for provisioning EC2,VPC,S3 resources but I want to create stack for specific resource type (eg for EC2 only) from that template. 我有用于配置EC2,VPC,S3资源的cloudformation模板,但我想从该模板为特定资源类型(例如,仅针对EC2)创建堆栈。 I have used aws cli and mentioned --resource-types "AWS::EC2::Instance" but I am getting error "An error occurred (ValidationError) when calling the CreateStack operation: Resource type AWS::S3::Bucket is not allowed by parameter ResourceTypes [AWS::EC2::Instance]" .
我已经使用过aws cli并提到了--resource-types“ AWS :: EC2 :: Instance”,但是我遇到了错误“调用CreateStack操作时发生错误(ValidationError):资源类型AWS :: S3 :: Bucket不是由参数ResourceTypes [AWS :: EC2 :: Instance]允许 。 Could you please let me know how can I create stack resource wise ?
您能否让我知道如何才能明智地创建堆栈资源?
Method 1: 方法1:
Add an Input parameter called ResourceType as shown below. 添加一个称为ResourceType的Input参数,如下所示。 Pass the Resource-Type which you want to create as an input to the CFN template.
将要创建的资源类型作为输入传递到CFN模板。
Parameters:
ResourceType:
Description: Resource Types
Type: String
AllowedValues:
- EC2
- RDS
- VPC
- S3
Add corresponding conditions: 添加相应条件:
Conditions:
CheckCreateEC2:
Fn::Equals: [ Ref: ResourceType, "EC2" ]
CheckCreateRDS:
Fn::Equals: [ Ref: ResourceType, "RDS" ]
CheckCreateVPC:
Fn::Equals: [ Ref: ResourceType, "VPC" ]
CheckCreateS3:
Fn::Equals: [ Ref: ResourceType, "S3" ]
Then create the resource-type accordingly and the corressponding condition checks into it. 然后相应地创建资源类型,并在相应的条件中进行检查。
Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Condition: CheckCreateEC
Properties:
.
.
.
MyRDSInstance:
Type: AWS::RDS::DBInstance
Condition: CheckCreateRDS
Properties:
.
.
.
MyS3Bucket:
Type: AWS::S3::Bucket
Condition: CheckCreateS3
Properties:
.
.
.
This way only the resources corresponding to the resource-type which you pass as InputParameter will be created. 这样,只有对应于传递作为InputParameter将要创建的资源类型的资源 。
Method 2: 方法2:
You can AWS Nested Stacks . 您可以使用AWS Nested Stacks 。 Using this, You can maintain an individual common templates for each resource types, but still maintain dependency between them so that entire Stack is created.
使用此方法,您可以为每种资源类型维护一个单独的通用模板,但仍保持它们之间的依赖性,以便创建整个堆栈。
This Method is more apt, if you can maintain separate templates for each resource-type. 如果您可以为每种资源类型维护单独的模板,则此方法更合适。 This provides more flexibility as well as isolation( If you need to modify condition/parameters of a particular resource-Type, You just need to update that specific template ) and will reduce human errors in other section of the templates.
这提供了更大的灵活性和隔离性( 如果您需要修改特定资源类型的条件/参数,则只需更新该特定模板 ),并且可以减少模板其他部分中的人为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.