AWS CloudFormation模板中的用户数据脚本未被执行

3

我试图创建一个CloudFormation堆栈,其中包含UserData脚本,以在启动EC2实例时安装java、tomcat、httpd和java应用程序。 然而,尽管该堆栈成功创建了所有资源,但当我连接到EC2实例以检查上述应用程序的配置时,却发现没有任何内容。我的用例是使用自动化方式启动一个带有所有上述应用程序/软件安装的实例。

UserData:
   Fn::Base64: 
    Fn::Join: 
    - ' '
    - - '#!/bin/bash -xe\n'

      - 'sudo yum update && install pip && pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n'
      - 'date > /home/ec2-user/starttime\n'
      - 'sudo yum update -y aws-cfn-bootstrap\n'

        # Initialize CloudFormation bits\n
      - ' ' 
      - '/opt/aws/bin/cfn-init -v\n'
      - '             --stack\n'
      - '!Ref AWS::StackName\n'
      - '             --resource LaunchConfig\n'
      - 'ACCESS_KEY=${HostKeys}&SECRET_KEY=${HostKeys.SecretAccessKey}\n'

       # Start servers\n
      - 'service tomcat8 start\n'
      - '/etc/init.d/httpd start\n'

      - 'date > /home/ec2-user/stoptime\n'
Metadata: 
 AWS::CloudFormation::Init:
  config: 
   packages: 
    yum:
    - java-1.8.0-openjdk.x86_64: []   
    - tomcat8: []
    - httpd: []
   services:
    sysvinit:
     httpd:
      enabled: 'true'
      ensureRunning: 'true'
  files: 
  - /usr/share/tomcat8/webapps/sample.war:
    - source: https://s3-eu-west-1.amazonaws.com/testbucket/sample.war
    - mode: 000500
    - owner: tomcat
    - group: tomcat
   CfnUser:
    Type: AWS::IAM::User
    Properties: 
     Path: '/'  
     Policies: 
     - PolicyName: Admin
       PolicyDocument: 
        Statement:
        - Effect: Allow
          Action: '*'
          Resource: '*'
   HostKeys:
    Type: AWS::IAM::AccessKey
    Properties: 
      UserName: !Ref CfnUser

不清楚问题出在哪里。登录后会发生什么?那句话不完整。你看到了什么错误信息? - Alex Harvey
1
无论您遇到什么问题,也不要在实例中注入访问密钥和秘密密钥。相反,请创建一个角色并将该角色分配给您的EC2实例。https://aws.amazon.com/blogs/aws/iam-roles-for-ec2-instances-simplified-secure-access-to-aws-service-apis-from-ec2/ - Sébastien Stormacq
问题是在堆栈创建时软件未被安装。查看 /var/log/cloud-init.log 文件,我得到了以下信息:cloud-init[3369]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1] 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) - Madhur Asati
1个回答

6
问题在于您格式化UserData的方式。建议您先启动EC2实例并手动测试脚本,因为它有很多问题。
尝试将UserData格式化为以下格式:
UserData:
  Fn::Base64:
    !Sub |
      #!/bin/bash -xe

      # FIXME. This won't work either.
      # sudo yum update && install pip && pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz

      date > /home/ec2-user/starttime
      sudo yum update -y aws-cfn-bootstrap

      # Initialize CloudFormation bits
      /opt/aws/bin/cfn-init -v \
        --stack ${AWS::StackName} \
        --resource LaunchConfig

      # FIXME. Not sure why these are here.
      # ACCESS_KEY=${HostKeys}
      # SECRET_KEY=${HostKeys.SecretAccessKey}

      # Start servers\n
      service tomcat8 start
      /etc/init.d/httpd start

      date > /home/ec2-user/stoptime

需要注意的事项:

  • 在此处无法使用!Ref符号进行插值。请注意我将其更改为${AWS::StackName},并且整个块都在!Sub中。
  • 正如我的注释所示,yum update行中有无效命令。
  • 正如注释中所述,向脚本注入访问密钥是一种不好的做法。而且,这些密钥似乎在此脚本中没有任何用途。

还要注意,在MetaData中,文件部分的指定方式不正确,应该是哈希键而不是数组。

正确的写法如下:

  files: 
    /usr/share/tomcat8/webapps/sample.war:
      source: https://s3-eu-west-1.amazonaws.com/testbucket/sample.war
      mode: '000500'
      owner: tomcat
      group: tomcat

非常感谢您的回答,@Alex。我通过编辑您建议的脚本启动了一个实例。我不再在 /var/log/cloud-init.log 中收到错误信息,并且这为我提供了一个良好的开端。然而,该实例仍未安装应用程序。我在上面提到的主要问题的元数据部分是否有遗漏?请给予建议。 - Madhur Asati
你的 cfn-init 调用是否指向了正确的资源名称?https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html 它应该是“包含元数据的资源的逻辑资源 ID。” - Alex Harvey
我更新了,注意到你的模板中还有另一个语法错误。 - Alex Harvey
你的元数据块所在的资源名称是什么?它真的是“LaunchConfig”吗? - Alex Harvey
您可能需要指定一个区域吗?cfn-init区域默认为us-east-1。 - Alex Harvey
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接