LoginSignup
0
0

More than 1 year has passed since last update.

terraform init で AWS VPC をインポートしてTerraform管理下に置く

Posted at

AWSでVPCを作成しておく

tf ファイルを定義

中身は空で良いので定義しておく

vpc.tf

resource "aws_vpc" "example" {
}

VPC ID を確認

image

import する

terraform import aws_vpc.example vpc-0e4bdfa5f98f4136a

これでVPCをTerafform管理下に置けるわけだが、別に今の状態からtfファイルを作成してくれるわけではないようだ

生成されたStateファイルの例

{
  "version": 4,
  "terraform_version": "1.3.7",
  "serial": 1,
  "lineage": "2f0d4879-1b5c-f056-fc90-0789baa23c7a",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "aws_vpc",
      "name": "example",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
          "schema_version": 1,
          "attributes": {
            "arn": "arn:aws:ec2:ap-northeast-1:842696858454:vpc/vpc-0e4bdfa5f98f4136a",
            "assign_generated_ipv6_cidr_block": false,
            "cidr_block": "10.0.0.0/16",
            "default_network_acl_id": "acl-0bb942c98f06a1128",
            "default_route_table_id": "rtb-0ed5c6d1d0739abcb",
            "default_security_group_id": "sg-09189d0b06f959c26",
            "dhcp_options_id": "dopt-896f56ee",
            "enable_classiclink": false,
            "enable_classiclink_dns_support": false,
            "enable_dns_hostnames": true,
            "enable_dns_support": true,
            "enable_network_address_usage_metrics": false,
            "id": "vpc-0e4bdfa5f98f4136a",
            "instance_tenancy": "default",
            "ipv4_ipam_pool_id": null,
            "ipv4_netmask_length": null,
            "ipv6_association_id": "",
            "ipv6_cidr_block": "",
            "ipv6_cidr_block_network_border_group": "",
            "ipv6_ipam_pool_id": "",
            "ipv6_netmask_length": 0,
            "main_route_table_id": "rtb-0ed5c6d1d0739abcb",
            "owner_id": "842696858454",
            "tags": {
              "Name": "eex"
            },
            "tags_all": {
              "Name": "eex"
            }
          },
          "sensitive_attributes": [],
          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjEifQ=="
        }
      ]
    }
  ],
  "check_results": null
}

Apply

VPCに新しい名前をつけて terraform apply するとAWS上でもVPC名が変わるのが分かる

resource "aws_vpc" "example" {
  tags = {
    Name = "exexex"
  }
}
image

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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