【问题标题】:Using userdata in Cloudformation在 Cloudformation 中使用用户数据
【发布时间】:2021-03-02 04:20:39
【问题描述】:

我正在尝试在我的用户数据中添加一些简单的 bash 命令,但看起来它们没有运行?

AWSTemplateFormatVersion: '2010-09-09'
Resources:
RHELInstance:
Type: AWS::EC2::Instance
Properties: 
  IamInstanceProfile: Super-Agent
  ImageId: ami-26ebbc5c
  KeyName: Super-Agent
  InstanceType: m4.large
  SecurityGroupIds:
    - sg-XXXXXX
  SubnetId: subnet-XXXXXXX
  BlockDeviceMappings:
  -
    DeviceName: "/dev/sda1"
    Ebs:
      VolumeSize: 24
      VolumeType: gp2
  UserData:
    Fn::Base64: 
      !Sub |
        #!/bin/bash -xe
        yum update -y
        cd /tmp
        wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
        sudo dpkg -i amazon-ssm-agent.deb
        sudo start amazon-ssm-agent
        mkdir /tmp/folder/
  Tags:
  - Key: Name
    Value: RHEL07102.00

输出: 私有IP: 值:!GetAtt RHELInstance.PrivateIp

【问题讨论】:

    标签: bash amazon-web-services amazon-cloudformation


    【解决方案1】:

    您使用的是ami-26ebbc5c,即RHEL-7.4_HVM-20180103-x86_64-2-Hourly2-GP2

    很可能(但我可能错了)cloud-init(处理用户数据)未安装在此 AMI 上。结果,没有任何东西在查看用户数据。

    您可以使用以下方式安装 cloud-init:

    yum install cloud-init
    

    但是,这必须手动完成,然后生成一个新的 AMI。

    或者,您可以使用 Amazon Linux 或 Ubuntu,它们都默认安装了 cloud-init。

    【讨论】:

    • 你说得对,当我使用 Amazon Linux 映像时它可以工作,但我仍然很难弄清楚它为什么不工作。如果我将脚本添加到适用于 RHEL 的 AWS 控制台中的 UserData,它可以工作 - 这意味着它必须在启动期间安装或安装 cloud-init。
    • 您可以检查/var/log/cloud-init-output.log 看看是否有任何运行。
    • 不错的收获!没想到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2020-07-10
    相关资源
    最近更新 更多