AWS:没有明显原因,简单的cfn-init在Amazon Linux 2上失败

时间:2019-01-02 08:02:23

标签: amazon-web-services amazon-cloudformation cloud-init

我正在供应一个cloudformation堆栈。我只是想在使用基于Amazon Linux 2的自定义AMI开始的实例上运行有史以来最简单的cfn-init

 EC2ESMasterNode1:
    Type: AWS::EC2::Instance
    Metadata:
      Comment: ES Cluster Master 1 instance
      AWS::CloudFormation::Init:
        config:
          commands:
            01_template_elastic:
              command:
                !Sub |
                  echo "'Hello World'"
    Properties:
      ImageId: ami-09693313102a30b2c
      InstanceType: !Ref MasterInstanceType
      SubnetId: !Ref Subn1ID
      SecurityGroupIds: [!Ref SGES]
      KeyName: mykey
      UserData:
        "Fn::Base64":
          !Sub |
            #!/bin/bash -xe
            # Start cfn-init
            /opt/aws/bin/cfn-init -s ${AWS::StackName} --resource EC2ESMasterNode1 --region ${AWS::Region}
            # Send the respective signal to Cloudformation
            /opt/aws/bin/cfn-signal -e 0 --stack ${AWS::StackName} --resource EC2ESMasterNode1 --region ${AWS::Region}
      Tags:
        - Key: "Name"
          Value: !Ref Master1NodeName

/var/log/cloud-init-output.log的打印如下

No packages needed for security; 15 packages available
Resolving Dependencies
Cloud-init v. 18.2-72.amzn2.0.6 running 'modules:final' at Wed, 02 Jan 2019 12:41:26 +0000. Up 14.42 seconds.
+ /opt/aws/bin/cfn-init -s test-elastic --resource EC2ESMasterNode1 --region eu-west-1
+ /opt/aws/bin/cfn-signal -e 0 --stack test-elastic --resource EC2ESMasterNode1 --region eu-west-1
ValidationError: Stack arn:aws:cloudformation:eu-west-1:248059334340:stack/test-elastic/9fc79150-0e8b-11e9-b135-503ac9e74cfd is in CREATE_COMPLETE state and cannot be signaled
Jan 02 12:41:27 cloud-init[2575]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Jan 02 12:41:27 cloud-init[2575]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Jan 02 12:41:27 cloud-init[2575]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 18.2-72.amzn2.0.6 finished at Wed, 02 Jan 2019 12:41:27 +0000. Datasource DataSourceEc2.  Up 15.30 seconds

/var/log/cloud-init.log具有以下错误:

    Jan 02 12:41:26 cloud-init[2575]: handlers.py[DEBUG]: start: modules-final/config-scripts-user: running config-scripts-user with frequency once-per-instance
Jan 02 12:41:26 cloud-init[2575]: util.py[DEBUG]: Writing to /var/lib/cloud/instances/i-0c10a5ff1be475b99/sem/config_scripts_user - wb: [644] 20 bytes
Jan 02 12:41:26 cloud-init[2575]: helpers.py[DEBUG]: Running config-scripts-user using lock (<FileLock using file '/var/lib/cloud/instances/i-0c10a5ff1be475b99/sem/config_scripts_user'>)
Jan 02 12:41:26 cloud-init[2575]: util.py[DEBUG]: Running command ['/var/lib/cloud/instance/scripts/part-001'] with allowed return codes [0] (shell=True, capture=False)
Jan 02 12:41:27 cloud-init[2575]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Jan 02 12:41:27 cloud-init[2575]: util.py[DEBUG]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 860, in runparts
    subp(prefix + [exe_path], capture=False, shell=True)
  File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 2053, in subp
    cmd=args)
ProcessExecutionError: Unexpected error while running command.
Command: ['/var/lib/cloud/instance/scripts/part-001']
Exit code: 1
Reason: -
Stdout: -
Stderr: -
Jan 02 12:41:27 cloud-init[2575]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Jan 02 12:41:27 cloud-init[2575]: handlers.py[DEBUG]: finish: modules-final/config-scripts-user: FAIL: running config-scripts-user with frequency once-per-instance
Jan 02 12:41:27 cloud-init[2575]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Jan 02 12:41:27 cloud-init[2575]: util.py[DEBUG]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/cloudinit/stages.py", line 798, in _run_modules
    freq=freq)
  File "/usr/lib/python2.7/site-packages/cloudinit/cloud.py", line 54, in run
    return self._runners.run(name, functor, args, freq, clear_on_fail)
  File "/usr/lib/python2.7/site-packages/cloudinit/helpers.py", line 187, in run
    results = functor(*args)
  File "/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.py", line 45, in handle
    util.runparts(runparts_path)
  File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 867, in runparts
    % (len(failed), len(attempted)))
RuntimeError: Runparts: 1 failures in 1 attempted commands
Jan 02 12:41:27 cloud-init[2575]: stages.py[DEBUG]: Running module ssh-authkey-fingerprints (<module 'cloudinit.config.cc_ssh_authkey_fingerprints' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_ssh_authkey_fingerprints.pyc'>) with frequency once-per-instance

_

cat /var/log/cfn-init-cmd.log
2019-01-02 12:50:54,777 P2582 [INFO] ************************************************************
2019-01-02 12:50:54,777 P2582 [INFO] ConfigSet default
2019-01-02 12:50:54,778 P2582 [INFO] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2019-01-02 12:50:54,778 P2582 [INFO] Config config
2019-01-02 12:50:54,778 P2582 [INFO] ============================================================
2019-01-02 12:50:54,778 P2582 [INFO] Command 01_template_elastic
2019-01-02 12:50:54,782 P2582 [INFO] -----------------------Command Output-----------------------
2019-01-02 12:50:54,782 P2582 [INFO]     'Hello World'
2019-01-02 12:50:54,783 P2582 [INFO] ------------------------------------------------------------
2019-01-02 12:50:54,783 P2582 [INFO] Completed successfully.

有人知道错误的原因吗?

此外,为什么成功创建堆栈? (还有特定资源?)

1 个答案:

答案 0 :(得分:1)

/var/log/cloud-init.log中的错误消息表示您的UserData脚本以error status 1(而不是预期的0)退出。

与此同时,您的/var/log/cloud-init-output.log包含以下行:

ValidationError: Stack arn:aws:cloudformation:eu-west-1:248059334340:stack/test-elastic/9fc79150-0e8b-11e9-b135-503ac9e74cfd
  is in CREATE_COMPLETE state and cannot be signaled

对于其他问题:

  

此外,为什么成功创建堆栈? (还有特定资源?)

一旦创建资源,进入CREATE_COMPLETE状态是堆栈的正常行为。默认情况下,UserData脚本的运行不会延迟此状态。

因为您使用的是cfn-signal,所以我假设您需要将CREATE_COMPLETE状态推迟到在UserData中发送信号之前。

关于here的所有设置,有一篇不错的博客文章。

但是tl; dr-

您可能只需要向您的EC2实例资源中添加一个CreationPolicy,如下所示:

Resources:
  EC2ESMasterNode1:
    ...
    CreationPolicy:
      ResourceSignal:
        Count: 1
        Timeout: PT10M

表示等待1个信号,然后在10分钟后超时。显然,请根据您的要求进行设置。