2017-06-16 14 views
0

渡っCFN-INITを使用して私はそのようなように、インスタンスのリソースを持っている:AWS CloudFormation - 共有ユーザーデータスクリプトがEC2s

testservice: 
    Type: 'AWS::EC2::Instance' 
    Metadata: 
    AWS::CloudFormation::Init: 
     configSets: 
     serviceConfigSet: 
      - serviceConfig 
     serviceConfig: 
     packages: 
      yum: 
      salt-minion: [] 
     commands: 
      hostnameConfig: 
      command: hostname test-service-1.0 
     services: 
      sysvinit: 
      salt-minion: 
       enabled: 'true' 
       ensureRunning: 'true' 
    Properties: 
    ImageId: !Ref BaseServiceAmi 
    KeyName: test_ssh_key 
    SubnetId: !Ref ServiceSubnetId 
    UserData: 
     'Fn::Base64': 
     'Fn::Sub': | 
      #!/bin/bash 

      yum update -y 

      yum install -y aws-cfn-bootstrap 

      /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource testservice --configsets serviceConfigSet --region ${AWS::Region} 

しかし、私はパラメータにUserDataスクリプトを移動し、複数のインスタンス間でそれを再利用したいですリソース。これは簡単なはずですが、ユーザーデータスクリプト内で現在のリソースの名前を取得する方法は見つけられません(この部分に明示的に設定しました:--resource testservice)。

私はそれを一般化することができるので、私はユーザーデータのスクリプトの内部から参照を介してリソース名を取得する方法はありますか?擬似パラメータリストには表示されません。

答えて

0

ネストされたスタックを使用することをお勧めします。 「AWS :: EC2 :: Instance」リソースのすべての設定をパラメータに変換できる場合は、そのリソースのテンプレートスタックを作成し、別のテンプレートから呼び出すことができます。

クイックインフォ:使用する方法 http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html#stackpolicy

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stack.html