Terraformのaws_s3_bucket_objectのdeprecated対応

概要

aws_s3_bucket_object はdeprecatedになっていて、aws_s3_objectへ置き換えるようにWarningが出ることがあります。

terraform バージョン: 1.3.0 で確認できましたが、他の1系のバージョンでも発生するかと思われます。

│ Warning: Argument is deprecated
│
│   with aws_s3_bucket_object.example,
│   on s3.tf line 25, in resource "aws_s3_bucket_object" "example":
│   25:   bucket  = aws_s3_bucket.example.id
│
│ Use the aws_s3_object resource instead

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object

NOTE:
The aws_s3_bucket_object resource is DEPRECATED and will be removed in a future version! Use aws_s3_object instead, where new features and fixes will be added. When replacing aws_s3_bucket_object with aws_s3_object in your configuration, on the next apply, Terraform will recreate the object. If you prefer to not have Terraform recreate the object, import the object using aws_s3_object.

対応

上記の場合、terraform import で解消する可能性があります。

> terraform import aws_s3_object.example S3バケット名/example.json

しかし、dataを使って、contentにレンダリングさせている場合には、importではcontent部分は解消できず差分が発生します。
その場合は、applyするか、手動でstateファイルを修正する必要があります。
手動で対応する場合の手順としては下記の通り。

  1. aws_s3_bucket_object をコピーして、aws_s3_objectにする
  2. 上記コマンドで import する
  3. tfstate をバックアップする
  4. tfstate を修正(aws_s3_bucket_objectと全く同じ値をaws_s3_objectへコピー)して、tfsate をアップロードする
  5. planで差分が発生しないことを確認する
  6. aws_s3_bucket_object で書いていた古い state を rm する
  7. 上記のコードを削除する
  8. planでwarningと差分が発生しないことを確認する