Terraforming を試してみる
昨日書いた Terraform で既存サーバの情報をインポートしてみる は、結構面倒なので、何か便利なツールがないか探してみました。
そうしたら、こんなツールがあるじゃないですか。
aws にしか対応していないのと、aws でも全部には対応してなさそうなのが残念ですが、ちょっと試してみましょう。
入れてみる
※ gem が入ってることが前提です。
> gem install terraforming
Fetching: jmespath-1.4.0.gem (100%)
Successfully installed jmespath-1.4.0
Fetching: aws-partitions-1.122.0.gem (100%)
Successfully installed aws-partitions-1.122.0
Fetching: aws-sigv4-1.0.3.gem (100%)
Successfully installed aws-sigv4-1.0.3
Fetching: aws-eventstream-1.0.1.gem (100%)
Successfully installed aws-eventstream-1.0.1
Fetching: aws-sdk-core-3.43.0.gem (100%)
Successfully installed aws-sdk-core-3.43.0
Fetching: aws-sdk-autoscaling-1.13.0.gem (100%)
Successfully installed aws-sdk-autoscaling-1.13.0
Fetching: aws-sdk-cloudwatch-1.13.0.gem (100%)
Successfully installed aws-sdk-cloudwatch-1.13.0
Fetching: aws-sdk-ec2-1.62.0.gem (100%)
Successfully installed aws-sdk-ec2-1.62.0
Fetching: aws-sdk-efs-1.7.0.gem (100%)
Successfully installed aws-sdk-efs-1.7.0
Fetching: aws-sdk-elasticache-1.10.0.gem (100%)
Successfully installed aws-sdk-elasticache-1.10.0
Fetching: aws-sdk-elasticloadbalancing-1.8.0.gem (100%)
Successfully installed aws-sdk-elasticloadbalancing-1.8.0
Fetching: aws-sdk-elasticloadbalancingv2-1.18.0.gem (100%)
Successfully installed aws-sdk-elasticloadbalancingv2-1.18.0
Fetching: aws-sdk-iam-1.12.0.gem (100%)
Successfully installed aws-sdk-iam-1.12.0
Fetching: aws-sdk-kms-1.13.0.gem (100%)
Successfully installed aws-sdk-kms-1.13.0
Fetching: aws-sdk-rds-1.41.0.gem (100%)
Successfully installed aws-sdk-rds-1.41.0
Fetching: aws-sdk-redshift-1.16.0.gem (100%)
Successfully installed aws-sdk-redshift-1.16.0
Fetching: aws-sdk-route53-1.16.0.gem (100%)
Successfully installed aws-sdk-route53-1.16.0
Fetching: aws-sdk-s3-1.30.0.gem (100%)
Successfully installed aws-sdk-s3-1.30.0
Fetching: aws-sdk-sns-1.9.0.gem (100%)
Successfully installed aws-sdk-sns-1.9.0
Fetching: aws-sdk-sqs-1.10.0.gem (100%)
Successfully installed aws-sdk-sqs-1.10.0
Fetching: multi_json-1.12.2.gem (100%)
Successfully installed multi_json-1.12.2
Fetching: thor-0.20.3.gem (100%)
Successfully installed thor-0.20.3
Fetching: terraforming-0.16.0.gem (100%)
Successfully installed terraforming-0.16.0
Parsing documentation for jmespath-1.4.0
Installing ri documentation for jmespath-1.4.0
Parsing documentation for aws-partitions-1.122.0
Installing ri documentation for aws-partitions-1.122.0
Parsing documentation for aws-sigv4-1.0.3
Installing ri documentation for aws-sigv4-1.0.3
Parsing documentation for aws-eventstream-1.0.1
Installing ri documentation for aws-eventstream-1.0.1
Parsing documentation for aws-sdk-core-3.43.0
Installing ri documentation for aws-sdk-core-3.43.0
Parsing documentation for aws-sdk-autoscaling-1.13.0
Installing ri documentation for aws-sdk-autoscaling-1.13.0
Parsing documentation for aws-sdk-cloudwatch-1.13.0
Installing ri documentation for aws-sdk-cloudwatch-1.13.0
Parsing documentation for aws-sdk-ec2-1.62.0
Installing ri documentation for aws-sdk-ec2-1.62.0
Parsing documentation for aws-sdk-efs-1.7.0
Installing ri documentation for aws-sdk-efs-1.7.0
Parsing documentation for aws-sdk-elasticache-1.10.0
Installing ri documentation for aws-sdk-elasticache-1.10.0
Parsing documentation for aws-sdk-elasticloadbalancing-1.8.0
Installing ri documentation for aws-sdk-elasticloadbalancing-1.8.0
Parsing documentation for aws-sdk-elasticloadbalancingv2-1.18.0
Installing ri documentation for aws-sdk-elasticloadbalancingv2-1.18.0
Parsing documentation for aws-sdk-iam-1.12.0
Installing ri documentation for aws-sdk-iam-1.12.0
Parsing documentation for aws-sdk-kms-1.13.0
Installing ri documentation for aws-sdk-kms-1.13.0
Parsing documentation for aws-sdk-rds-1.41.0
Installing ri documentation for aws-sdk-rds-1.41.0
Parsing documentation for aws-sdk-redshift-1.16.0
Installing ri documentation for aws-sdk-redshift-1.16.0
Parsing documentation for aws-sdk-route53-1.16.0
Installing ri documentation for aws-sdk-route53-1.16.0
Parsing documentation for aws-sdk-s3-1.30.0
Installing ri documentation for aws-sdk-s3-1.30.0
Parsing documentation for aws-sdk-sns-1.9.0
Installing ri documentation for aws-sdk-sns-1.9.0
Parsing documentation for aws-sdk-sqs-1.10.0
Installing ri documentation for aws-sdk-sqs-1.10.0
Parsing documentation for multi_json-1.12.2
Installing ri documentation for multi_json-1.12.2
Parsing documentation for thor-0.20.3
Installing ri documentation for thor-0.20.3
Parsing documentation for terraforming-0.16.0
Installing ri documentation for terraforming-0.16.0
23 gems installed
結構いっぱい入りますね。
事前設定
環境変素に aws の情報を設定します。
> export AWS_ACCESS_KEY_ID=<access key>
> export AWS_SECRET_ACCESS_KEY=<secret key>
> export AWS_DEFAULT_REGION=ap-northeast-1
では、やってみましょう
S3 にあらかじめ advent-test という bucket を作成しときました。
aws.tf
ファイルについても昨日の記事を参考にして作成しておいてください。
> terraforming s3
resource "aws_s3_bucket" "advent-test" {
bucket = "advent-test"
acl = "private"
}
お、取れたようですね。
これが、tf ファイルの内容です。
s3.tf
として保存しておきましょう。
resource "aws_s3_bucket" "advent-test" {
bucket = "advent-test"
acl = "private"
}
では、次は tfstate を取ってみましょう。
> terraforming s3 --tfstate
{
"version": 1,
"serial": 1,
"modules": [
{
"path": [
"root"
],
"outputs": {
},
"resources": {
"aws_s3_bucket.advent-test": {
"type": "aws_s3_bucket",
"primary": {
"id": "advent-test",
"attributes": {
"acl": "private",
"bucket": "advent-test",
"force_destroy": "false",
"id": "advent-test",
"policy": ""
}
}
}
}
}
]
}
お、これも取れたようですね。
terraform のディレクトリで下記のようにすると、既存の tfstate ファイルにマージしてくれるようです。
> terraforming s3 --tfstate --merge=terraform.tfstate
{
"version": 1,
"serial": 2,
"modules": [
{
"path": [
"root"
],
"outputs": {
},
"resources": {
"aws_vpc.vpc-1768d870": {
"type": "aws_vpc",
"primary": {
"id": "vpc-1768d870",
"attributes": {
"cidr_block": "172.31.0.0/16",
"enable_dns_hostnames": "true",
"enable_dns_support": "true",
"id": "vpc-1768d870",
"instance_tenancy": "default",
"tags.#": "0"
}
}
},
"aws_s3_bucket.advent-test": {
"type": "aws_s3_bucket",
"primary": {
"id": "advent-test",
"attributes": {
"acl": "private",
"bucket": "advent-test",
"force_destroy": "false",
"id": "advent-test",
"policy": ""
}
}
}
}
}
]
}
マージしてくれたやつを、terraform.tfstate
として保存しましょう。
{
"version": 1,
"serial": 2,
"modules": [
{
"path": [
"root"
],
"outputs": {
},
"resources": {
"aws_vpc.vpc-1768d870": {
"type": "aws_vpc",
"primary": {
"id": "vpc-1768d870",
"attributes": {
"cidr_block": "172.31.0.0/16",
"enable_dns_hostnames": "true",
"enable_dns_support": "true",
"id": "vpc-1768d870",
"instance_tenancy": "default",
"tags.#": "0"
}
}
},
"aws_s3_bucket.advent-test": {
"type": "aws_s3_bucket",
"primary": {
"id": "advent-test",
"attributes": {
"acl": "private",
"bucket": "advent-test",
"force_destroy": "false",
"id": "advent-test",
"policy": ""
}
}
}
}
}
]
}
terraform で使えるか試してみる
> ./terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_vpc.vpc-1768d870: Refreshing state... (ID: vpc-1768d870)
aws_s3_bucket.advent-test: Refreshing state... (ID: advent-test)
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
どうやら、ちゃんとできたようですね。
しっかり、No changes
になってくれました。
取得できるもの
現状 aws の下記のコンフィグが取得できます。
ALB
AutoScaling Group
CloudWatch Alarm
Database Parameter Group
Database Security Group
Database Subnet Group
EC2
ElastiCache Cluster
ElastiCache Subnet Group
EFS File System
EIP
ELB
Describe available commands or one specific command
IAM Group
IAM Group Membership
IAM Group Policy
IAM Instance Profile
IAM Policy
IAM Policy Attachment
IAM Role
IAM Role Policy
IAM User
IAM User Policy
Internet Gateway
KMS Key Alias
KMS Key
Launch Configuration
Network ACL
NAT Gateway
Network Interface
Route53 Record
Route53 Hosted Zone
RDS
Redshift
Route Table
Route Table Association
S3
Security Group
Subnet
SNS Topic
SNS Subscription
SQS
VPN Gateway
VPC
まとめ
全部ではないものの aws のかなりの情報が取れるというのは便利ですね。
他のクラウドのやつも取れるようになってくといいですね・・・