2
0

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 3 years have passed since last update.

DENSOAdvent Calendar 2021

Day 4

Terraform 及びTerragruntをDocker 上で実行する

Last updated at Posted at 2021-12-04

なぜDocker上でTerraformを動かしたいのか?

  • 環境やバージョンの違いで動かない問題が起きなくなるので保守性があがる。
  • Terraformの定義ファイル内でバージョン固定しなくてもよくなる*もちろん固定してもいいが余計なエラーに悩まされることがなくなる。

作るもの

  1. Dockerfile
  2. docker-compose.yml
  3. makefileオプション
    の3つを用意

Dockerfile

FROM python:3.6

ARG pip_installer="https://bootstrap.pypa.io/get-pip.py"
ARG AWSCLI_VERSION
ARG TERRAFORM_VERSION
ARG TERRAGRUNT_VERSION

# install aws-cli
RUN pip install awscli==${AWSCLI_VERSION}

# install command.
RUN apt-get update && apt-get install -y less vim wget unzip jq

# install terraform
RUN wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \
  unzip ./terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/local/bin/ \
  && rm ./terraform_${TERRAFORM_VERSION}_linux_amd64.zip

# install terargrunt
RUN wget https://github.com/gruntwork-io/terragrunt/releases/download/v${TERRAGRUNT_VERSION}/terragrunt_linux_amd64 \
  && chmod +x ./terragrunt_linux_amd64 \
  && mv ./terragrunt_linux_amd64 /usr/local/bin/terragrunt

# COPY terraform related files
COPY ./terraform /terraform

docker-compose.yml

docker-compose.yml
version: "2"
services:
  terraform:
    container_name: "terraform"
    build:
      context: ../ #Dockerfileのパスを指定する
      dockerfile: Dockerfile
      args:
        AWSCLI_VERSION: "1.16.168"
        TERRAFORM_VERSION: "1.0.1"
        TERRAGRUNT_VERSION: "0.31.0"
    env_file: ./.env
    working_dir: /terraform
    tty: true

さらにmakefileを用意

.PHONY: build plan apply destroy

default:build

build:
	@docker-compose build

plan: build
	@docker-compose run --rm terraform terragrund plan

apply: build
	@docker-compose run --rm terraform terragrunt apply

destroy: build
	@docker-compose run --rm terraform terragrunt destroy

これでmake apply とかでTerraformの適用ができるようになります。

参考記事

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?