0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Terraform AWS 基礎

Posted at

#IAMユーザ作成
●サービス検索から【IAM】と入力
●左のダッシュボードからユーザークリック
●「ユーザーを追加」を選択
●ユーザー名を「Terraform」設定
●「プログラムによるアクセス」のチェックボックスにチェックをいれる
●「既存のポリシーを直接アタッチ」を選択し、「AdministratorAccess」ポリシー(管理者権限)を選択。
●タグは入力しない。
●「ユーザーの作成」 クリック
●アクセスキー のcsvをダウンロードして保存

#terraformをDockerで起動
vagrantへログイン

$ mkdir terraform && cd terraform

ホストのカレントディレクトリをコンテナ上へマウント( -v $(pwd):/terraform )してファイルの共有

$ docker run \
    -e AWS_ACCESS_KEY_ID=AWS ACCESS KEY \
    -e AWS_SECRET_ACCESS_KEY=AWS SECRET ACCESS KEY \
    -v $(pwd):/terraform \
    -w /terraform \
    -it \
    --entrypoint=ash \
    hashicorp/terraform:0.11.13

Docker内でTerraform がインストールできてるか確認

$ terraform version
Terraform v0.11.13

#VPC作成
新しくパワーシェル起動してvagrant sshでホストOSに接続

& cd terraform
$ mkdir vpc && cd vpc

コンテナ内で同期できてるか確認

/terraform # ls

vpc
HCLを書く
VPCを立ち上げるためのコード

$ touch main.tf
$ vi main.tf
provider "aws" {
  region = "us-west-2"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "vpc"
  }
}

下記コンテナ内で実行

$ cd vpc
$ terraform init
$ terraform apply
Enter a value: と表示されたら yes と入力

awsコンソールへアクセスしてvpcが作成されていることを確認する

#リソースの変更

provider "aws" {
  region = "us-west-2"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "vpc_aaaa"
  }
}

applyの前にplanで確認

$ 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.main: Refreshing state... (ID: vpc-0832c28bc984a91c4)

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  ~ aws_vpc.main
      tags.Name: "vpc" => "vpc_aaaa"


Plan: 0 to add, 1 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
$ terraform apply

awsコンソールで確認

#Terraformが管理しているリソース

$ terraform show
aws_vpc.main:
  id = vpc-0832c28bc984a91c4
  arn = arn:aws:ec2:us-west-2:491186103049:vpc/vpc-0832c28bc984a91c4
  assign_generated_ipv6_cidr_block = false
  cidr_block = 10.0.0.0/16
  default_network_acl_id = acl-069cbe3977c81ffce
  default_route_table_id = rtb-02832eeb3e6b31aee
  default_security_group_id = sg-068d9e09d0f03d3c5
  dhcp_options_id = dopt-a6e172de
  enable_classiclink = false
  enable_classiclink_dns_support = false
  enable_dns_hostnames = false
  enable_dns_support = true
  instance_tenancy = default
  ipv6_association_id =
  ipv6_cidr_block =
  main_route_table_id = rtb-02832eeb3e6b31aee
  owner_id = 491186103049
  tags.% = 1
  tags.Name = vpc_aaaa

Terraformが管理しているリソースは terraform.tfstate というJSONファイルに格納
コードの適用を行う際はこのファイルを参照し、差分の確認

/terraform/vpc # ls
main.tf                   terraform.tfstate         terraform.tfstate.backup
/terraform/vpc # cat terraform.tfstate
{
    "version": 3,
    "terraform_version": "0.11.13",
    "serial": 2,
    "lineage": "15d630e2-a839-c136-8a0b-a5edb07f01ef",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "aws_vpc.main": {
                    "type": "aws_vpc",
                    "depends_on": [],
                    "primary": {
                        "id": "vpc-0832c28bc984a91c4",
                        "attributes": {
                            "arn": "arn:aws:ec2:us-west-2:491186103049:vpc/vpc-0832c28bc984a91c4",
                            "assign_generated_ipv6_cidr_block": "false",
                            "cidr_block": "10.0.0.0/16",
                            "default_network_acl_id": "acl-069cbe3977c81ffce",
                            "default_route_table_id": "rtb-02832eeb3e6b31aee",
                            "default_security_group_id": "sg-068d9e09d0f03d3c5",
                            "dhcp_options_id": "dopt-a6e172de",
                            "enable_classiclink": "false",
                            "enable_classiclink_dns_support": "false",
                            "enable_dns_hostnames": "false",
                            "enable_dns_support": "true",
                            "id": "vpc-0832c28bc984a91c4",
                            "instance_tenancy": "default",
                            "ipv6_association_id": "",
                            "ipv6_cidr_block": "",
                            "main_route_table_id": "rtb-02832eeb3e6b31aee",
                            "owner_id": "491186103049",
                            "tags.%": "1",
                            "tags.Name": "vpc_aaaa"
                        },
                        "meta": {
                            "schema_version": "1"
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                }
            },
            "depends_on": []
        }
    ]
}

削除

$ terraform destroy

awsコンソールで削除されてるか確認

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?