サイトアイコン 協栄情報ブログ

AWS CLIでSSMドキュメント作成時に「text contents could not be decoded」が出たときの解決策(Windows)

WindowsOSのローカルPCからawscliでSSMドキュメントを作成しようとした際に、以下のエラーが出ました。

 

YAML Error parsing parameter '--content': Unable to load paramfile (C:/Users/csnp0001/Documents/fuchukaiwai/ops-automation/os/user-provisioning/doc-init-users.yml), text contents could not be decoded. If this is a binary file, please use the fileb:// prefix instead of the file:// prefix.

 

解決法がWebになかったので、SSM Documentを作れない問題の原因と解決策を紹介します。

 

 

WindowsのAWS CLI v2のエンコーディングエラー

■前提(環境)

 

 

■発生したエラー

SSM DocumentをYAMLから作成しようとして、以下のエラーが発生しました。

 

【実行コマンド】

aws ssm create-document \
  --name DOC-INIT-OS-USERS \
  --document-type Command \
  --content file://C:/Users/csnp0001/Documents/.../doc-init-users.yml \
  --document-format YAML

 

【出力】

Error parsing parameter '--content': Unable to load paramfile (...doc-init-users.yml),
text contents could not be decoded.
If this is a binary file, please use the fileb:// prefix instead of the file:// prefix.

 

 

■まず確認したこと

「ファイルの文字コードが壊れてるのでは?」と思い、fileコマンドで確認しました。

 

【実行コマンド】

file -bi /c/Users/csnp0001/Documents/.../doc-init-users.yml

 

【コマンド結果】

text/plain; charset=utf-8

 

ファイルはUTF-8。それでもAWS CLIが読めずに落ちていました。

 

 

■原因

AWS CLIはテキストファイルを読むとき、デフォルトではロケールに合わせたエンコーディングで開きます。Windowsだとここがズレやすく、UTF-8のファイルでもdecodeに失敗することがあるようです。

 

 

■解決策

AWS公式ドキュメントにもある通り、環境変数AWS_CLI_FILE_ENCODINGを設定すると、AWS CLI がテキストファイルを読むときのエンコーディングを指定できます。
https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cliv2-migration-changes.html

 

●Git Bashの場合

 

【実行コマンド】

export AWS_CLI_FILE_ENCODING=UTF-8
echo $AWS_CLI_FILE_ENCODING

 

【出力】

UTF-8

 

 

■再実行

同じcreate-documentを再実行すると、今度は成功しました。

 

【実行コマンド】

aws ssm create-document \
  --name DOC-INIT-OS-USERS-001 \
  --document-type Command \
  --content file://C:/Users/csnp0001/Documents/.../doc-init-users.yml \
  --document-format YAML

 

【出力】

DOCUMENTDESCRIPTION     2025-12-16T19:24:46.316000+09:00        1       Create OS users, deploy SSH keys from S3, grant sudo, and disable password auth YAML    Command 1       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx        Sha256  1       DOC-INIT-OS-USERS-001   xxxxxxxxxxxx    2.2     Creating
PARAMETERS              S3 bucket name where SSH public keys are stored SshKeyBucket    String
PARAMETERS              S3 key prefix (e.g. env/prod)   SshKeyPrefix    String
PARAMETERS              OS user names to create (comma-separated, e.g. sysuser,appuser) UserNames       String
PARAMETERS              Users to grant passwordless sudo (comma-separated, e.g. sysuser,appuser). Empty to skip.        SudoUsers       String
PARAMETERS      yes     Disable SSH password authentication (yes/no)    DisablePasswordAuth     String
PLATFORMTYPES   Linux
PLATFORMTYPES   MacOS

DOCUMENTDESCRIPTION ... Creatingが表示され、Document が作成できました。

 

 

まとめ

aws ssm create-documenttext contents could not be decodedが出たら、まずAWS_CLI_FILE_ENCODING=UTF-8を設定してみましょう。

 

ファイルがUTF-8でも、AWS CLI側がロケールの既定エンコーディングで開こうとして失敗するケースがあります。

 

参考リンク:AWS CLI バージョン 2 の新機能と変更点AWS CLI でのファイルからのパラメータの読み込み

 

 

↓ほかの協栄情報メンバーのAWS CLIについての記事を公開しています。ぜひ参考にしてみてください。
 

 
AWS CLI新コマンド”aws login”試してみた(齊藤弘樹)

 

 

モバイルバージョンを終了