简体   繁体   English

如何从AWS CloudFormation模板为特定资源类型创建堆栈

[英]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.

相关问题 AWS Cloudformation-如何依赖另一个嵌套堆栈中的DependsOn资源 - AWS Cloudformation - how to DependsOn resource from another nested stack AWS Cloudformation 创建堆栈模板错误 - AWS Cloudformation Create-Stack Template Error 如何自定义AWS Codestar / Cloudformation模板以创建特定的代码构建项目? - How to customize AWS Codestar / Cloudformation template to create specific codebuild project? 如何将 AWS 资源转换为 cloudformation 堆栈或模板? - How to convert AWS resources to a cloudformation stack or template? 如何为特定 CloudFormation 堆栈创建“aws.cloudformation”CloudWatch 事件类型? - How can I create an "aws.cloudformation" CloudWatch event type for a specific CloudFormation stack? 已解决 AWS CloudFormation 创建堆栈。 未解决的资源依赖 - SOLVED AWS CloudFormation Create stack. Unresolved resource dependencies 如何在单个CloudFormation堆栈上创建2个AWS Lambda? - How to create 2 AWS lambdas on a single CloudFormation stack? 如何在 CloudFormation 模板中将 AWS::ApiGateway::Resource 添加到 AWS::Serverless::Api - How to add an AWS::ApiGateway::Resource to an AWS::Serverless::Api in CloudFormation template 如何保护AWS CloudFormation堆栈不被删除? - How to protect an AWS CloudFormation stack from deletion? 如何确定 AWS 资源属于哪个 CloudFormation 堆栈? - How to determine what CloudFormation stack an AWS resource belongs to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM