添加cfn信号后cfn-init停止工作

时间:2019-02-19 23:47:00

标签: amazon-web-services amazon-cloudformation

我正在尝试使用cfn-init在Amazon EC2 Windows实例中执行一些powershell命令。 如果添加cfn-signal和CreationPolicy,则不会执行cfn-init。 如果不添加cfn信号和创建策略,一切都将执行并正常工作。 这是我脚本的一部分:

EC2Instance1:
 Type: AWS::EC2::Instance
 Metadata:
  AWS::CloudFormation::Init:
    config:
      files:
        'c:\cfn\cfn-hup.conf':
          content: !Join
            - ''
            - - |
                [main]
              - stack=
              - !Ref 'AWS::StackId'
              - |+

              - region=
              - !Ref 'AWS::Region'
              - |+

        'c:\cfn\hooks.d\cfn-auto-reloader.conf':
          content: !Join
            - ''
            - - |
                [cfn-auto-reloader-hook]
              - |
                triggers=post.update
              - >
                path=Resources.EC2Instance1.Metadata.AWS::CloudFormation::Init
              - 'action=cfn-init.exe -v -s '
              - !Ref 'AWS::StackId'
              - ' -r EC2Instance1'
              - ' --region '
              - !Ref 'AWS::Region'
              - |+

      commands:
        1-createFile:
          command: !Sub |
            powershell.exe New-Item ${PathOfFile} -ItemType file
        2-addContenttoFile:
          command: !Sub |
            powershell.exe Set-Content -path ${PathOfFile} -Value 'Ip of webserver1 here'
        3-replaceText:
          command: !Sub |
            powershell.exe (Get-Content ${PathOfFile}).replace('webserver1', (Invoke-WebRequest ifconfig.me/ip).Content.Trim()) ^| Set-Content ${PathOfFile}
 Properties:
  ImageId: !Ref AMI1
  InstanceType: !Ref EC2InstanceType
  KeyName:  !Ref KeyName
  UserData: !Base64
    'Fn::Join':
      - ''
      - - |
          <script>
        - 'cfn-init.exe -v -s '
        - !Ref 'AWS::StackId'
        - ' -r EC2Instance1'
        - ' --region '
        - !Ref 'AWS::Region'
        - |+

        - |
        - 'cfn-signal.exe -e %ERRORLEVEL% -s --stack '
        - !Ref 'AWS::StackName'
        - ' --resource EC2Instance1'
        - ' --region '
        - !Ref 'AWS::Region'
        - |+

        - </script>

 CreationPolicy:
  ResourceSignal:
    Count: "1"
    Timeout: PT10M

我要去哪里错了?

1 个答案:

答案 0 :(得分:1)

我有一个类似的问题,但是原因是由于在cfn-init期间重新启动。我发现这很有帮助:https://aws.amazon.com/premiumsupport/knowledge-center/create-complete-bootstrapping/。您可以尝试将cfn信号放在配置集中的命令中,而不是放在userData中。