在 Cloudformation 中使用用户数据

Using userdata in Cloudformation

我正在尝试在我的用户数据中添加一些简单的 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

您正在使用 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。