2017-01-24 12 views
0

私たちは私たちの組織でかなり広くterraformを使用しています。他の人がVPCピアリングを行っているかどうかについていくつか質問がありました。接続の最初の作成は簡単です。ちょうど作成したVPCから別のVPCを参照し、ルートテーブルなどを設定します。問題はVPCの問題です。これで、手動で別のネットワークスタックに移動し、CIDR/PCX IDを変数として手動で追加する必要があります。私はこれを少し簡単に処理するスクリプトを書いていましたが、誰かが既存のVPCのAWSに対して動的に検索を実行していて、既存のPCXをそのVPCのルーティングテーブルに自動的に追加するかどうか尋ねました。TerraformとVPCピアリング

OPS VPCでは、これが役に立つ場所の例があります。私たちはOPSを持っていて、次にdev、prod、qa、stg、uat、cteなどがあります。CTE vpcを作成すると自動的にpcxが作成され、opsとopsにリンクされます。しかし、オプスはこの新しいpcxについて知りません。したがって、手動で追加する必要があります。 opsが自分自身でリソースルックアップを実行できるようにしたいと思います。新しいVPC/PCXが見つかった場合は、自分のリソースをプロビジョニングします。

TLDR;双方向VPCピアリングがよりダイナミックになる方法

答えて

0

これで、ラッパースクリプトを作成しました。新しいVPCを追加するたびに、ops VPCディレクトリに移動してこのスクリプトを実行すると、variables.tfファイルにOPS vpcピアリング接続/ルートを設定するために必要なすべての変数が動的に設定されます。

スクリプト例:

#!/bin/bash 
region=$(find . -name "*vars.tf"|cut -d/ -f2|cut -d- -f1-3) 
profile=$(find . -name "*vars.tf" -exec grep 'variable "profile"' {} \; |awk '{print $6}'|tr -d '"') 
account=$(pwd|cut -d/ -f5|cut -d- -f1) 

getData(){ 
    for id in ${ids[@]}; do 
     output=$(aws ec2 describe-vpc-peering-connections --region $region --profile $account --vpc-peering-connection-ids $id) 
     cidr=$(echo "$output"|jq '.VpcPeeringConnections[].RequesterVpcInfo.CidrBlock'|tr -d '"') 
     if [[ $1 == cidr ]]; then 
      echo $cidr 
     elif [[ $1 == id ]]; then 
      echo $id 
     fi 
    done 
} 
checkOps() { 
    pwd|grep 'ops' &>/dev/null 
} 
populateRoutes() { 
    if ! checkOps; then 
     echo "Must be run from the ops directory" 
     exit 1 
    fi 
    ids=($(aws ec2 describe-vpc-peering-connections --region $region --profile $account --filters "Name=status-code,Values=active"|jq '.VpcPeeringConnections[].VpcPeeringConnectionId'|tr -d '"')) 
    if ((${#ids[@]} == 0)); then 
     echo "No update necessary" 
     exit 0 
    fi 

    cidr_list=($(getData cidr)) 
    cidr_format=$(echo "${cidr_list[@]}"|tr ' ' ',') 
    echo $cidr_format 

    id_list=($(getData id)) 
    id_format=$(echo "${id_list[@]}"|tr ' ' ',') 
    echo $id_format 

    if ((${#cidr_list[@]} != ${#id_list[@]})); then 
     echo "CIDR List and ID List do not match" 
     exit 1 
    fi 

    sed -i "/pcx_count/c\variable\ \"pcx_count\"\ \{\ default \=\ \"${#ids[@]}\" \}" ./variables.tf 
    sed -i "/ops_cidrs/c\variable\ \"ops_cidrs\"\ \{\ default\ \=\ \"$cidr_format\"\ \}" ./variables.tf 
    sed -i "/pcx_ids/c\variable\ \"pcx_ids\"\ \{\ default\ \=\ \"$id_format\"\ \}" ./variables.tf 
} 

populateRoutes 
0

あなたがremote state backendを使用していると仮定すると、あなたはremote state data sourceとしてOPSのネットワークスタックに引き、その後、あなたはそれになりたい方はかないスタックからそのルーティングテーブルに変更を加えることができますにルーティングすることができます。

は、(明らかにボイラープレートの多くが欠落しているもの)を、最小限の例を試してみてくださいウィル:

# Run in the same folder as my_ops_stack.tf 
terraform remote config \ 
    -backend=s3 \ 
    -backend-config="bucket=my-state-bucket" \ 
    -backend-config="key=ops-stack/terraform.tfstate" \ 
    -backend-config="region=eu-west-1" 
:テラフォームのCLI( this will soon be possible in config)を使用して

# my_ops_stack.tf 

provider "aws" { 
    region = "eu-west-1" 
} 

module "ops_stack" { 
    source = "/my/modules/ops_stack" 
    cidr = "10.1.0.0/16" 
    // other vars probably 
} 

// the outputs which will be accessible 
// via the remote state data source: 
output "routing_table_id" { 
    value = "${module.ops_stack.routing_table_id}" 
} 
output "vpc_id" { 
    value = "${module.ops_stack.vpc_id}" 
} 
output "vpc_cidr" { 
    value = "10.1.0.0/16" 
} 

私はよ今configureこのスタック用のリモート状態のバックエンド

terraform apply 
# the usual stuff... but now synced with s3! 
:あなたはスタックに適用

今の状態のバックエンドが設定されている、すべての変更は、そのバックエンドに同期します 今

、新しい一時的なスタック(DEV、PROD、QA、STG、UAT、CTEなど)のテンプレートで:あなたは短命スタックで完了したら

# my_dev_stack.tf 

provider "aws" { 
    region = "eu-west-1" 
} 

// Pull in your ops stack from the remote backend: 
data "terraform_remote_state" "ops_stack" { 
    backend = "s3" 
    config { 
     bucket = "my-state-bucket" 
     key = "ops-stack/terraform.tfstate" 
     region = "eu-west-1" 
    } 
} 

// Create your dev stack 
module "dev_stack" { 
    source   = "/my/modules/dev_stack" 
    cidr    = "10.2.0.0/16" 
    // The ops_stack vpc id for creating the peering connection: 
    ops_vpc_id  = "${data.terraform_remote_state.ops_stack.vpc_id}" 
    // Maybe some security group rules you wanna setup 
    allow_access_from = "${data.terraform_remote_state.ops_stack.vpc_cidr}" 
    // other vars probably 
} 

// And use its outputs to add a route to the 
// ops vpc routing table from the dev stack! 
resource "aws_route" "ops_to_dev" { 
    route_table_id = "${data.terraform_remote_state.ops_stack.routing_table_id}" 
    destination_cidr_block = "10.2.0.0/16" // dev_stack's cidr 
    vpc_peering_connection_id = "${module.dev_stack.vpcx_id}" 
} 

、あなたは安全に破壊することができますそれはopsスタックのルートをクリーンアップします。

これはあなたの後ろだったのですか?

関連する問題