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

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

インフラ構成をコードで管理できる 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/ を指定する必要があります。

https://github.com/hashicorp/docker-hub-images/tree/master/terraform


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 の作成に成功しました。

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)