terraformで特定のmoduleだけapplyする方法メモ

結論

$ terraform apply -target=module.s3
  • module追加しているときにはモジュールのインストールしないといけない(エラーメッセージでる)
$ terraform init

おまけ : S3の静的ホスティングのterraform

  • main.tf
module "s3" {
  source = "./modules/s3"

  app_name = "app-name"
}
  • modules/s3.tf
resource "aws_s3_bucket" "bucket" {
  bucket = "${var.app_name}-app-s3-endpoint"
  acl    = "public-read"

  website {
    index_document = "index.html"
  }
}

resource "aws_s3_bucket_policy" "source" {
    bucket = aws_s3_bucket.bucket.id
    policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "${aws_s3_bucket.bucket.arn}/*",
            "Condition": {}
        }
    ]
}
EOF
}
  • /module/variables.tf
variable "app_name" {}

おまけ2 : s3へのコピー

$ aws s3 ls
  • フォルダ内のファイル全部コピー
$ aws s3 cp build s3://<bucket-name>/ --recursive
$ aws s3 rm s3://<bucket-name>/ --recursive
  • 最初は以下のコマンドでやった・・・(xargs)
$ ls | xargs -I{} aws s3 cp {} s3://<bucket-name>/

おまけ3 : S3の静的ホスティングについて