3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Qiita×Findy記事投稿キャンペーン 「自分のエンジニアとしてのキャリアを振り返ろう!」

AWSリソースをTerraformにImportする(Network関連_main.tf)

Last updated at Posted at 2024-02-14

前提

全体の概略はこちらから

構成図

architecture4.drawio.png

👆の図のように、

  • インターネットへの通信はTransit Gatewayを使用した集約型
    (インターネットへの通信の集約化はこちらの記事がおすすめ)
  • Internet Gatewayのあるインターネットへの通信を集約するVPCは1つのみで開発、テスト、ステージングで共有
    (以下、internet VPCと呼びます)
  • 同様にdatabaseがあるVPCは1つのみで開発、テスト、ステージングで共有
    (以下、database VPCと呼びます)
  • マイクロサービスのVPCは開発用、テスト用、ステージング用にそれぞれ構築。
    (以下、micro VPCと呼びます)

Moduleのフォルダ構成

modules/
 └ network/
        └ vpc_database/
            └ main.tf
            └ outputs.tf
            └ variable.tf
        └ vpc_internet/
        └ vpc_endpoint/
        └ vpc_micro/
        └ tgw/
        └ tgw_attach/

👆のフォルダ構成の通り、

  • Software Engineer等のアプリ、データベース担当に素早くdatabase VPCを提供するため、database VPCのmoduleを分離
  • 上記構成でコストがかかるリソースは下記2つ。極力アプリ開発にライフサイクルを合わせるため、internet VPCとTransit Gateway Attachmentのmoduleを分離
    • NAT Gateway
    • Transit Gateway Attachment
  • vpc endpointのmoduleを分けた理由は後述

環境構築用のmain.tf, variable.tf

main.tf

#region取得
data "aws_region" "current" {}
#account_id
data "aws_caller_identity" "now" {}
locals {
  region          = data.aws_region.current.name
  aws_account_id  = data.aws_caller_identity.now.account_id
}

#vpc_database
module "vpc_database"{
  source = "../../modules/network/vpc_database"
  env               = "dev"
  account           = var.account
  project           = var.project
  region            = local.region
  vpc_cidr_database       = var.vpc_cidr_database
  subnet_cidr_database1a  = var.subnet_cidr_database1a
  subnet_cidr_database1c  = var.subnet_cidr_database1c
  subnet_cidr_database1d  = var.subnet_cidr_database1d
  subnet_cidr_compute1a   = var.subnet_cidr_compute1a
  subnet_cidr_compute1c   = var.subnet_cidr_compute1c
  subnet_cidr_eni1a       = var.subnet_cidr_eni1a
  subnet_cidr_eni1c       = var.subnet_cidr_eni1c
}

#vpc_internet
module "vpc_internet"{
  source = "../../modules/network/vpc_internet"
  env               = "dev"
  account           = var.account
  project           = var.project
  region            = local.region
  vpc_cidr_internet =  var.vpc_cidr_internet
  subnet_cidr_public1a  = var.subnet_cidr_public1a
  subnet_cidr_public1c  = var.subnet_cidr_public1c
  subnet_cidr_private1a = var.subnet_cidr_private1a
  subnet_cidr_private1c = var.subnet_cidr_private1c
}

# vpc microservice
module "vpc_micro" {
  source    = "../../modules/network/vpc_micro"
  for_each  = var.micro
  env       = each.key
  account   = var.account
  project   = var.project
  region    = local.region
  vpc_cidr_microservice = each.value.vpc
  subnet_cidr_microservice1a  = each.value.subnet_a
  subnet_cidr_microservice1c  = each.value.subnet_c
}

# tgw
module "tgw" {
  source    = "../../modules/network/tgw"
  env       = "dev"
  account   = var.account
  project   = var.project
  vpc_cidr_database     = var.vpc_cidr_database
  vpc_database_id       = module.vpc_database.vpc_database_id
  subnet_database_id    = [
    module.vpc_database.subnet_eni1a_id, 
    module.vpc_database.subnet_eni1c_id
  ]
  vpc_internet_id       = module.vpc_internet.vpc_internet_id
  subnet_internet_id    = [
    module.vpc_internet.subnet_private1a_id,
    module.vpc_internet.subnet_private1c_id
  ]
  rtb_database_id       = module.vpc_database.rtb_compute_id
  rtb_internet_id       = module.vpc_internet.rtb_public_id
  depends_on = [ 
    module.vpc_database, 
    module.vpc_internet 
  ]
}

# tgw attachment
module "tgw_attachment_micro" {
  source    = "../../modules/network/tgw_attach"
  for_each  = var.micro
  env       = each.key
  account   = var.account
  project   = var.project
  tgw_id    = module.tgw.tgw_id
  tgw_rtb_to_internet_id    = module.tgw.tgw_rtb_to_internet_id
  tgw_rtb_from_internet_id  = module.tgw.tgw_rtb_from_internet_id
  vpc_micro_id              = module.vpc_micro[each.key].vpc_microservice_id
  vpc_cidr_micro            = each.value.vpc
  subnet_micro_id           = [
    module.vpc_micro[each.key].subnet_microservice1a_id,
    module.vpc_micro[each.key].subnet_microservice1c_id
  ]
  prefixlist_id             = module.tgw.prefixlist_id
  depends_on = [
    module.vpc_micro
  ]
}

variable.tf

VPC, SubnetのCIDRを定義。

# common variable 
variable "project" {
    type        = string
    default     = "ramen"
    description = "project name"
}
variable "account" {
    type        = string
    default     = "no-prod"
    description = "prod or no-prod"
}
variable "env" {    
    type        = string
    default     = "dev"
    description = "dev, test, stage or prod"
}

# variables of cidr
## cidr of vpc associated with database
variable "vpc_cidr_database" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_database1a" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_database1c" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_database1d" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_compute1a" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_compute1c" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_eni1a" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_eni1c" {
    type    = string
    default = "**.***.**.**/**"
}
## cidr of proxy vpc
variable "vpc_cidr_internet" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_public1a" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_public1c" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_private1a" {
    type    = string
    default = "**.***.**.**/**"
}
variable "subnet_cidr_private1c" {
    type    = string
    default = "**.***.**.**/**"
}

# variables of microservice
variable "micro" {
    default = {
        dev ={
            vpc      = "**.***.**.**/**"
            subnet_a = "**.***.**.**/**"
            subnet_c = "**.***.**.**/**"
            storage = 512
            memory  = 128
        }
        test = {
            vpc      = "**.***.**.**/**"
            subnet_a = "**.***.**.**/**"
            subnet_c = "**.***.**.**/**"
            storage = 512
            memory  = 128
        }
        stage = {
            vpc      = "**.***.**.**/**"
            subnet_a = "**.***.**.**/**"
            subnet_c = "**.***.**.**/**"
            storage = 1024
            memory  = 256
        }
    }
}

for_each

開発、テスト、ステージング環境の繰り返しはfor_eachで実施。
対象はmicro VPCとmicro VPCにアタッチするTransit Gateway Attachment.

# vpc microservice
module "vpc_micro" {
  source    = "../../modules/network/vpc_micro"
  for_each  = var.micro
  env       = each.key
  // ... 略
  vpc_cidr_microservice = each.value.vpc
  // ... 略
}
# variables of microservice
variable "micro" {
    default = {
        dev ={
            vpc      = "**.***.**.**/**"
            subnet_a = "**.***.**.**/**"
            subnet_c = "**.***.**.**/**"
            storage = 512
            memory  = 128
        }
        test = {
            vpc      = "**.***.**.**/**"
            subnet_a = "**.***.**.**/**"
            subnet_c = "**.***.**.**/**"
            storage = 512
            memory  = 128
        }
        stage = {
            vpc      = "**.***.**.**/**"
            subnet_a = "**.***.**.**/**"
            subnet_c = "**.***.**.**/**"
            storage = 1024
            memory  = 256
        }
    }
}

for_eachの変数は開発のライフサイクルが同じもの同士で共有すると、別のfor_eachで繰り返されているmoduleに出力を変数として渡すことが可能になります。以下の例だと、vpc_microとfor_eachに同じ変数microを使用するtgw_attachment_microvpc_microの出力を使用可能になります。

# tgw attachment
module "tgw_attachment_micro" {
  source    = "../../modules/network/tgw_attach"
  for_each  = var.micro
  // ... 略
  vpc_micro_id              = module.vpc_micro[each.key].vpc_microservice_id
  // ... 略
  subnet_micro_id           = [
    module.vpc_micro[each.key].subnet_microservice1a_id,
    module.vpc_micro[each.key].subnet_microservice1c_id
  ]
  // ... 略
}

Import するコマンド

Importはimport コマンドで実施。moduleで定義されたterraformにリソースをimportする場合

$ terraform import module.{module}.{resourceタイプ}.{resource} {importするresourceID}

例えば、database VPCのmoduleで定義されたVPCvpc_databaseをimportする場合、

$ terraform import module.vpc_database.aws_vpc.vpc_database vpc-***

for_eachで繰り返されたmodule化terraformにimportする場合、

$ terraform import module.{module}["each_key"].{resourceタイプ}.{resource} {imporyするresourceID}

例えば、開発環境用のmicro VPCのmoduleで定義されtたVPCvpc_microserviceをimportする場合、

$ terraform import module.vpc_micro[\"dev\"].aws_vpc.vpc_microservice vpc-***

importコマンドでfor_eachで繰り返されたmodule化terraformにimportする場合、ターミナルでコマンドの細部が異なるので注意されたし。
Example: Import into Resource configured with for_each

各moduleの設計思想

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?