weixin_39717598
2021-01-12 13:08 阅读 6

cloud-init failures for aws_launch_template when user data compressed and encoded using template_cloudinit_config

This issue was originally opened by as hashicorp/terraform#20955. It was migrated here as a result of the provider split. The original body of the issue is below.

cloud-init fails for me every time under the following circumstances: an aws_launch_template using template_cloudinit_config user data with base64_encode = true and gzip = true

In /var/log/cloud-init.log this is the only WARNING message (there are no ERROR messages):

2019-04-08 11:22:40,097 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'b"\\x1f\\x8b\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\xffdUk\\x8f\\xaaJ\\x16\\xfdn\\xd2\\xff\\xa1\\xa6'"...'

Terraform Version:


Terraform v0.11.13
+ provider.aws v1.60.0
+ provider.template v2.1.0

Affected Resource(s): aws_launch_template template_cloudinit_config

Terraform Configuration Files:

hcl
data "template_cloudinit_config" "default_cloud_config" {
  gzip          = true
  base64_encode = true

  part {
    filename     = "demo-main-fast"
    content_type = "text/cloud-config"
    content      = "${file("${path.module}/../../resources/demo-main-fast")}"
  }
}

resource "aws_launch_template" "i3_test_launch_template" {
  name                    = "main-test-fast"
  count                   = "1"
  description             = "test"
  disable_api_termination = false
  ebs_optimized           = true

  iam_instance_profile {
    arn = "${data.aws_iam_instance_profile.node_profile.arn}"
  }

  image_id                             = "${data.aws_ami.ubuntu_bionic.id}"
  instance_initiated_shutdown_behavior = "terminate"
  instance_type                        = "i3.xlarge"
  key_name                             = "${var.key_pair}"

  monitoring {
    enabled = true
  }

  vpc_security_group_ids = ["${var.security_group}"]

  tag_specifications {
    resource_type = "instance"

    tags {
      Name      = "test"
      Creator   = "Terraform"
      Network   = "main"
      Autoscale = 1
      Type      = "demo"
      SyncType  = "fast"
    }
  }

  user_data = "${data.template_cloudinit_config.default_cloud_config.rendered}"
}

Cloud-init File:

yaml
#cloud-config
package_update: false

write_files:
  - path: /test.txt
    content: |
      Here is a line.
      Another line is here.

users:
  - name: demo
    groups: sudo
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDf0q4PyG0doiBQYV7OlOxbRjle026hJPBWD+eKHWuVXIpAiQlSElEBqQn0pOqNJZ3IBCvSLnrdZTUph4czNC4885AArS9NkyM7lK27Oo8RV888jWc8hsx4CD2uNfkuHL+NI5xPB/QT3Um2Zi7GRkIwIgNPN5uqUtXvjgA+i1CS0Ku4ld8vndXvr504jV9BMQoZrXEST3YlriOb8Wf7hYqphVMpF3b+8df96Pxsj0+iZqayS9wFcL8ITPApHi0yVwS8TjxEtI3FDpCbf7Y/DmTGOv49+AWBkFhS2ZwwGTX65L61PDlTSAzL+rPFmHaQBHnsli8U9N6E4XHDEOjbSMRX user.com

runcmd:
  - 'echo "Checking if Internet is available. Retrying 30 mins..." ; (curl -f -s -o /dev/null --retry 15 --retry-max-time 1800 http://www.google.com && echo "Google is reachable.") || echo "Google is unreachable - rest of bootup might be broken!"'
  - 'apt-get update ; /usr/bin/cloud-init -d single -n cc_package_update_upgrade_install'
  - touch /test.txt

该提问来源于开源项目:hashicorp/terraform-provider-aws

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • weixin_39571403 weixin_39571403 2021-01-12 13:08

    the same issue

    点赞 评论 复制链接分享
  • weixin_39574708 weixin_39574708 2021-01-12 13:08

    The issue might be related to the fact that my terraform code is inside a module that is being imported by another plan. However if my code is just a stand-alone plan then it works.

    点赞 评论 复制链接分享
  • weixin_39571403 weixin_39571403 2021-01-12 13:08

    The issue might be related to the fact that my terraform code is inside a module that is being imported by another plan. However if my code is just a stand-alone plan then it works.

    Well, actually we’re using this inside a module, so that’s happening...

    点赞 评论 复制链接分享
  • weixin_39720516 weixin_39720516 2021-01-12 13:08

    Did anyone find a solution to this? I'm seeing the same when trying to use a YAML-based Cloud-Config file against DigitalOcean, rendered in 0.12 via the templatefile() function

    点赞 评论 复制链接分享
  • weixin_39614675 weixin_39614675 2021-01-12 13:08

    I am also encountering this issue. When it isn't base64 encoded and gzipped the file works as expected. The syntax is also correct for my cloud-config when I test it.

    I can even pull the file from the created VM and it can be extracted and shows to be proper formatting and everything:

    curl -L http://169.254.169.254/latest/user-data/ | base64 --decode | gunzip

    gives me:

    
    Content-Type: multipart/mixed; boundary="MIMEBOUNDARY"
    MIME-Version: 1.0
    
    --MIMEBOUNDARY
    Content-Transfer-Encoding: 7bit
    Content-Type: text/cloud-config
    Mime-Version: 1.0
    
    #cloud-config
    # set locale
    locale: en_GB.UTF-8
    # ensure time sync between all nodes
    ntp:
      enabled: true
      ntp_client: chrony
    # hides ssh keys in console
    ssh_fp_console_blacklist: [ssh-dss, ssh-dsa, ssh-ed25519]
    ssh_key_console_blacklist: [ssh-dss, ssh-dsa, ssh-ed25519]
    
    # upgrade all packages and install necessary ones
    package_upgrade: true
    package_reboot_if_required: true
    packages:
    - apt-transport-https
    - ca-certificates
    - curl
    - gnupg-agent
    - software-properties-common
    - build-essential
    - libssl-dev
    - make
    
    # set random root password and disable password login for ssh
    chpasswd:
      expire: false
      list: |
          root:RANDOM
    ssh_pwauth: no
    
    # create sre user with sudo privs and set autrhorized key
    users:
    - name: sre
      groups: sudo
      lock_passwd: true
      ssh_authorized_keys:
       - censored
      sudo: ['ALL=(ALL) NOPASSWD:ALL']
      shell: /bin/bash
    
    
    --MIMEBOUNDARY
    Content-Transfer-Encoding: 7bit
    Content-Type: text/cloud-config
    Mime-Version: 1.0
    
    #cloud-config
    # Configure Floating IP (Ubuntu 20.04 LTS)
    # Not required when using https://github.com/costela/hcloud-ip-floater
    #write_files:
    #  - content: |
    #      network:
    #         version: 2
    #         ethernets:
    #           eth0:
    #             addresses:
    #             - ${floating_ip}/32
    #    path: /etc/netplan/60-floating-ip.yaml
    # Install Keepalived
    runcmd:
    - cd /root/
    - wget http://www.keepalived.org/software/keepalived-2.1.2.tar.gz
    - tar xvf keepalived-2.1.2.tar.gz
    - cd keepalived-2.1.2
    - ./configure
    - make
    - sudo make install
    
    final_message: "The system is finally up, after $UPTIME seconds"
    

    Starting to suspect maybe this is a cloud-init bug not terraform one, but I am unsure. I will open an issue there as well.

    点赞 评论 复制链接分享

相关推荐