6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker Hub に terraform の Image があったので使ってみた。

Last updated at Posted at 2018-12-10

インフラ構成をコードで管理できる terraform ですが、
Docker Hub に Image があったので使ってみることにしました。

今回はとりあえず s3 を作ってみることにします。

example.tf

provider "aws" {}

resource "aws_s3_bucket" "example" {
  bucket = "my-bucket"
  force_destroy = true
}

HCLのシンタックスハイライト効くようにしてほしい...。

credentials は環境変数から読ませるので省略しています。

本記事とはあまり関係ないですがforce_destroy は bucket にファイルが残っていても、
強制的に削除できるようにするオプションです。
今回は有効にしときます。

terraform init

$ docker run -it -v $PWD:/app/ -w /app/ hashicorp/terraform:light init

init は terraform プロジェクトをイニシャライズするコマンドです。

コンテナにホストマシンが持っている tf ファイルの情報を与えてあげる必要があるので、
オプションに -v $PWD:/app/ -w /app/ を指定する必要があります。

terraform plan

$ docker run -e AWS_ACCESS_KEY_ID \
  -e AWS_SECRET_ACCESS_KEY \
  -e AWS_DEFAULT_REGION \
  -it -v $PWD:/app/ -w /app/ \
  hashicorp/terraform:light plan

同様に credentials をコンテナに注入します。
試してないですが $AWS_PROFILE でもいけると思います。

terraform apply

$ docker run -e AWS_ACCESS_KEY_ID \
  -e AWS_SECRET_ACCESS_KEY \
  -e AWS_DEFAULT_REGION \
  -it -v $PWD:/app/ -w /app/ \
  hashicorp/terraform:light plan

結果

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_s3_bucket.example
      id:                          <computed>
      acceleration_status:         <computed>
      acl:                         "private"
      arn:                         <computed>
      bucket:                      "my-bucket"
      bucket_domain_name:          <computed>
      bucket_regional_domain_name: <computed>
      force_destroy:               "true"
      hosted_zone_id:              <computed>
      region:                      "ap-northeast-1"
      request_payer:               <computed>
      versioning.#:                <computed>
      website_domain:              <computed>
      website_endpoint:            <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_s3_bucket.example: Creating...
  acceleration_status:         "" => "<computed>"
  acl:                         "" => "private"
  arn:                         "" => "<computed>"
  bucket:                      "" => "my-bucket"
  bucket_domain_name:          "" => "<computed>"
  bucket_regional_domain_name: "" => "<computed>"
  force_destroy:               "" => "true"
  hosted_zone_id:              "" => "<computed>"
  region:                      "" => "ap-northeast-1"
  request_payer:               "" => "<computed>"
  versioning.#:                "" => "<computed>"
  website_domain:              "" => "<computed>"
  website_endpoint:            "" => "<computed>"
aws_s3_bucket.example: Creation complete after 2s (ID: my-bucket)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

無事、bucket の作成に成功しました。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?