0
0

GitHub Actionsで実行しているTerraformをモジュール化してみた

Last updated at Posted at 2024-07-22

背景・目的

前回、GitHubActionsとTerraformでS3バケットを作成してみたで構築した環境は、S3バケットを作成するものでした。

こちらは、main.tfファイルにベタで書いていましたが、今後の拡張性を見越してモジュールに書き換えておきます。

実践

今回は、下記のような構成に変更します。

  • 変更前

    .
    ├── .github
    │   └── workflows
    │       └── terraform.yml
    ├── backend.hcl
    ├── backend.tf
    ├── main.tf
    └── provider.tf
    
  • 変更後

    terraform-modules/
    └── s3-bucket/
        ├── main.tf
        ├── variables.tf
        ├── outputs.tf
    root/
    ├── main.tf
    ├── variables.tf
    ├── terraform.tfvars
    ├── backend.tf
    

Terraformモジュール

terraform-modulesに、各モジュールのフォルダを作成し、その配下にコードを配置します。

  1. terraform-modules フォルダを作成します

  2. s3 フォルダを作成します
    image.png

  3. terraform-modules/s3 配下に、main.tf を作成します

    resource "aws_s3_bucket" "this" {
      bucket = var.bucket_name
      tags   = var.tags
    }
    
    resource "aws_s3_bucket_policy" "this" {
      bucket = var.bucket_name
    
      policy = jsonencode({
        Version = "2012-10-17"
        Statement = [
          {
            Sid       = "AllowSSLRequestsOnly"
            Effect    = "Deny"
            Principal = "*"
            Action    = "s3:*"
            Resource = [
              "arn:aws:s3:::${var.bucket_name}",
              "arn:aws:s3:::${var.bucket_name}/*"
            ]
            Condition = {
              Bool = {
                "aws:SecureTransport" = "false"
              }
            }
          }
        ]
      })
    }
    
    
  4. terraform-modules/s3 配下に、variables.tf を作成します

    variable "bucket_name" {
      description = "The name of the S3 bucket"
      type        = string
    }
    variable "tags" {
      description = "A map of tags to assign to the resource"
      type        = map(string)
      default     = {}
    }
    
  5. terraform-modules/s3 配下に、outputs.tfを作成します

    output "bucket_id" {
      description = "The ID of the bucket"
      value       = aws_s3_bucket.this.id
    }
    
    output "bucket_arn" {
      description = "The ARN of the bucket"
      value       = aws_s3_bucket.this.arn
    }
    
    output "bucket_region" {
      description = "The region of the bucket"
      value       = aws_s3_bucket.this.region
    }
    

ルートモジュール

  1. ルートにあるmain.tfをs3モジュールを呼び出すように修正します

    data "aws_caller_identity" "this" {}
    
    module "s3" {
      source      = "./terraform-modules/s3"
      bucket_name = format("%s-%s", var.bucket_name, "${data.aws_caller_identity.this.account_id}")
      tags        = var.tags
    }
    
    output "bucket_id" {
      value = module.s3.bucket_id
    }
    
    output "bucket_arn" {
      value = module.s3.bucket_arn
    }
    
    output "bucket_region" {
      value = module.s3.bucket_region
    }
    
  2. variable.tfを追加します

    variable "bucket_name" {
      description = "The name of the S3 bucket"
      type        = string
    }
    
    variable "tags" {
      description = "A map of tags to assign to the resource"
      type        = map(string)
      default     = {}
    }
    
    
  3. terraform.tfvarsを追加します

    bucket_name = "my-unique-bucket-name"
    tags = {
      "Environment" = "prod"
      "Project"     = "TerraformExample"
    }
    

実行確認

  1. Pushします。成功しました
    image.png

考察

今回、Terraformをモジュール化しました。今後は、これを前提にNW等を構築します。

参考

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