vagrant-awsでmkdir -p /vagrantで失敗する

だいぶはまったのでメモ。

aws用の設定書いてvagrant upすると以下のようなエラーが出てしまう。これ出てても、けっこうスルーしてる人が多い気がする。。。

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mkdir -p '/vagrant'

原因は、使ってるAmazon LinuxのAMIがデフォルトでsudoするのにrequirettyになっているから。
以下の例のようにuser_data渡すと、cloud initっていうの使って初期化することができるので、ec2-userは!requirettyにしてみた。
試行錯誤してる中でこれだとcloud initよりmkdirのが先に走るからだめかと思ったけど、整理してみたらこれでいけたっぽい。

$ cat Vagrantfile
Vagrant.configure("2") do |config|
  # ...
  config.vm.define :ec2_test do |ec2_config|
    # ...
    ec2_config.vm.provider :aws do |aws, override|
      # ...
      aws.user_data = File.read("user_data.txt")
  # ...

$ cat user_data.txt
#cloud-config
runcmd:
  - echo 'Defaults:ec2-user !requiretty' > /etc/sudoers.d/999-vagrant-cloud-init-requiretty && chmod 440 /etc/sudoers.d/999-vagrant-cloud-init-requiretty

[追記]
SyncFoldersがどうやらcloud initの処理の終了を待たないらしく、たまにしか成功しない。ので、!requirettyなAMIを作って、そこから作るようにした。