3
3

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.

circleciでterraformを扱う

Posted at

参考

ディレクトリ構成

 yuta   add-subnet  ~  aws-terraform  tree -a -L 2
.
├── .circleci
│   └── config.yml
├── .git
│   ├── COMMIT_EDITMSG
│   ├── FETCH_HEAD
│   ├── HEAD
│   ├── ORIG_HEAD
│   ├── branches
│   ├── config
│   ├── description
│   ├── hooks
│   ├── index
│   ├── info
│   ├── logs
│   ├── objects
│   ├── packed-refs
│   └── refs
├── .gitignore
├── .terraform
│   └── providers
├── .terraform.lock.hcl
├── README.md
└── main.tf

  • terraformはVPCを作るだけのもの
  • s3にremote stateをセットできるようにしておく
main.tf
provider "aws" {
  region  = "ap-northeast-1"
  profile = "yuta"
}

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 3.36.0"
    }
  }
  backend "s3" {
    bucket  = "vamdemic-circleci"
    region  = "ap-northeast-1"
    profile = "yuta"
    key     = "terraform.tfstate"
    encrypt = true
  }
}

resource "aws_vpc" "main" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "circleci-test-vpc"
  }
}

resource "aws_subnet" "main" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Main"
  }
}

準備すること

CircleCI用のコンフィグを作る

.circleci/config.yml
version: 2.1
orbs:
  terraform: "circleci/terraform@2.1.0"
workflows:
  deploy_infrastructure:
    jobs:
      - terraform/fmt:
          checkout: true
          context: terraform
      - terraform/validate:
          checkout: true
          context: terraform
          requires:
            - terraform/fmt
      - terraform/plan:
          checkout: true
          context: terraform
          persist-workspace: true
          requires:
            - terraform/validate
      - terraform/apply:
          attach-workspace: true
          context: terraform
          filters:
            branches:
              only: main
          requires:
            - terraform/plan

AWSCredencialをCircleCIの環境変数にセットする

  • CircleCI上で作成したプロジェクトでProject Settingsを選び入力する
  • それぞれ次の通りそのまま入力する
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
  • 今回の場合、指定した資格情報がterraform plan,applyのときに利用される

Contextをセットしておく

  • Organaization SettingsでContext→terraformを作る
  • このContextはyamlファイルから指定されていて、これがないと、NotFoundみたいな感じのメッセージが出て失敗する
    image.png

試す

main.tfに足す

resource "aws_subnet" "private" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"

  tags = {
    Name = "private"
  }
}

remoteブランチへpushする

git checkout -b add-subnet 
git commit -m "init"
git push 

実行結果

ちゃんと動いている
image.png

mainブランチへマージする

この際に、fmt,validate,planのCIが通過していることがわかる
image.png

CircleCIが再度動作する

  • といのは、以下の記述があるから
    • ブランチがmainのときのみterraform applyが実行されるということ
      - terraform/apply:
          attach-workspace: true
          context: terraform
          filters:
            branches:
              only: main

完了

image.png

サブネットも出来上がっている
image.png

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?