Shuchita Tripathi
Shuchita Tripathi

Reputation: 23

cfn-init stops working after adding cfn-signal

I am trying to execute few powershell commands in Amazon EC2 Windows instance using cfn-init. If I add cfn-signal and CreationPolicy, cfn-init is not getting executed. Without adding cfn-signal and Creation Policy, everything is getting executed and working fine. Here is part of my script:

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

Where am I going wrong?

Upvotes: 2

Views: 1279

Answers (1)

Ahmad Khatib
Ahmad Khatib

Reputation: 9

I had a similar issue but the cause was due to reboots during cfn-init. I found this helpful: https://aws.amazon.com/premiumsupport/knowledge-center/create-complete-bootstrapping/. You might try putting the cfn-signal in a command within your configset instead of in userData.

Upvotes: 1

Related Questions