[Terraform] AWS with Terraform - terraform configuration (테라폼 설정)


Get access_key and secret_key



Add user



Set aws cofigure

set provider

Create aws.tf


1
2
3
4
5
provider "aws" {
  access_key = "ACCESS_KEY"
  secret_key = "SECRET-KEY"
  region     = "ap-northeast-1"
}
cs



set vpc

create vpc.tf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
resource "aws_vpc" "test" {
  cidr_block = "172.10.0.0/20"
  tags {
    Name = "test"
  }
}
resource "aws_subnet" "a" {
  vpc_id = "${aws_vpc.test.id}"
  cidr_block = "172.10.0.0/24"
  availability_zone = "ap-northeast-1a"
}
 
resource "aws_subnet" "c" {
  vpc_id = "${aws_vpc.test.id}"
  cidr_block = "172.10.1.0/24"
  availability_zone = "ap-northeast-1c"
}
cs

Set security group

create security-group.tf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
resource "aws_security_group" "allow-all" {
  name = "allow_all"
  description = "Allow all inbound traffic"
  vpc_id = "${aws_vpc.example.id}"
 
  ingress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
 
  egress {
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
cs


Apply terraform

terraform init


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ terraform init
 
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (1.51.0)...
 
The following providers do not have any version constraints in configuration,
so the latest version was installed.
 
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
 
* provider.aws: version = "~> 1.51"
 
Terraform has been successfully initialized!
 
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
 
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
cs


terraform plan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
$ 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.
 
 
------------------------------------------------------------------------
 
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
 
Terraform will perform the following actions:
 
  + aws_security_group.allow-all
      id:                                   <computed>
      arn:                                  <computed>
      description:                          "Allow all inbound traffic"
      egress.#:                             "1"
      egress.482069346.cidr_blocks.#:       "1"
      egress.482069346.cidr_blocks.0:       "0.0.0.0/0"
      egress.482069346.description:         ""
      egress.482069346.from_port:           "0"
      egress.482069346.ipv6_cidr_blocks.#:  "0"
      egress.482069346.prefix_list_ids.#:   "0"
      egress.482069346.protocol:            "-1"
      egress.482069346.security_groups.#:   "0"
      egress.482069346.self:                "false"
      egress.482069346.to_port:             "0"
      ingress.#:                            "1"
      ingress.482069346.cidr_blocks.#:      "1"
      ingress.482069346.cidr_blocks.0:      "0.0.0.0/0"
      ingress.482069346.description:        ""
      ingress.482069346.from_port:          "0"
      ingress.482069346.ipv6_cidr_blocks.#: "0"
      ingress.482069346.prefix_list_ids.#:  "0"
      ingress.482069346.protocol:           "-1"
      ingress.482069346.security_groups.#:  "0"
      ingress.482069346.self:               "false"
      ingress.482069346.to_port:            "0"
      name:                                 "allow_all"
      owner_id:                             <computed>
      revoke_rules_on_delete:               "false"
      vpc_id:                               "${aws_vpc.test.id}"
 
  + aws_vpc.test
      id:                                   <computed>
      arn:                                  <computed>
      assign_generated_ipv6_cidr_block:     "false"
      cidr_block:                           "172.31.0.0/20"
      default_network_acl_id:               <computed>
      default_route_table_id:               <computed>
      default_security_group_id:            <computed>
      dhcp_options_id:                      <computed>
      enable_classiclink:                   <computed>
      enable_classiclink_dns_support:       <computed>
      enable_dns_hostnames:                 <computed>
      enable_dns_support:                   "true"
      instance_tenancy:                     "default"
      ipv6_association_id:                  <computed>
      ipv6_cidr_block:                      <computed>
      main_route_table_id:                  <computed>
      owner_id:                             <computed>
      tags.%:                               "1"
      tags.Name:                            "test"
 
 
Plan: 2 to add, 0 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.
cs


terraform apply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
$ terraform apply
 
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
 
Terraform will perform the following actions:
 
  + aws_security_group.allow-all
      id:                                   <computed>
      arn:                                  <computed>
      description:                          "Allow all inbound traffic"
      egress.#:                             "1"
      egress.482069346.cidr_blocks.#:       "1"
      egress.482069346.cidr_blocks.0:       "0.0.0.0/0"
      egress.482069346.description:         ""
      egress.482069346.from_port:           "0"
      egress.482069346.ipv6_cidr_blocks.#:  "0"
      egress.482069346.prefix_list_ids.#:   "0"
      egress.482069346.protocol:            "-1"
      egress.482069346.security_groups.#:   "0"
      egress.482069346.self:                "false"
      egress.482069346.to_port:             "0"
      ingress.#:                            "1"
      ingress.482069346.cidr_blocks.#:      "1"
      ingress.482069346.cidr_blocks.0:      "0.0.0.0/0"
      ingress.482069346.description:        ""
      ingress.482069346.from_port:          "0"
      ingress.482069346.ipv6_cidr_blocks.#: "0"
      ingress.482069346.prefix_list_ids.#:  "0"
      ingress.482069346.protocol:           "-1"
      ingress.482069346.security_groups.#:  "0"
      ingress.482069346.self:               "false"
      ingress.482069346.to_port:            "0"
      name:                                 "allow_all"
      owner_id:                             <computed>
      revoke_rules_on_delete:               "false"
      vpc_id:                               "${aws_vpc.test.id}"
 
  + aws_vpc.test
      id:                                   <computed>
      arn:                                  <computed>
      assign_generated_ipv6_cidr_block:     "false"
      cidr_block:                           "172.31.0.0/20"
      default_network_acl_id:               <computed>
      default_route_table_id:               <computed>
      default_security_group_id:            <computed>
      dhcp_options_id:                      <computed>
      enable_classiclink:                   <computed>
      enable_classiclink_dns_support:       <computed>
      enable_dns_hostnames:                 <computed>
      enable_dns_support:                   "true"
      instance_tenancy:                     "default"
      ipv6_association_id:                  <computed>
      ipv6_cidr_block:                      <computed>
      main_route_table_id:                  <computed>
      owner_id:                             <computed>
      tags.%:                               "1"
      tags.Name:                            "test"
 
 
Plan: 2 to add, 0 to change, 0 to destroy.
 
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
 
  Enter a value: yes
 
aws_vpc.test: Creating...
  arn:                              "" => "<computed>"
  assign_generated_ipv6_cidr_block: "" => "false"
  cidr_block:                       "" => "172.31.0.0/20"
  default_network_acl_id:           "" => "<computed>"
  default_route_table_id:           "" => "<computed>"
  default_security_group_id:        "" => "<computed>"
  dhcp_options_id:                  "" => "<computed>"
  enable_classiclink:               "" => "<computed>"
  enable_classiclink_dns_support:   "" => "<computed>"
  enable_dns_hostnames:             "" => "<computed>"
  enable_dns_support:               "" => "true"
  instance_tenancy:                 "" => "default"
  ipv6_association_id:              "" => "<computed>"
  ipv6_cidr_block:                  "" => "<computed>"
  main_route_table_id:              "" => "<computed>"
  owner_id:                         "" => "<computed>"
  tags.%:                           "" => "1"
  tags.Name:                        "" => "test"
aws_vpc.test: Still creating... (10s elapsed)
aws_vpc.test: Creation complete after 17s (ID: vpc-00f405f1ee36ab592)
aws_security_group.allow-all: Creating...
  arn:                                  "" => "<computed>"
  description:                          "" => "Allow all inbound traffic"
  egress.#:                             "" => "1"
  egress.482069346.cidr_blocks.#:       "" => "1"
  egress.482069346.cidr_blocks.0:       "" => "0.0.0.0/0"
  egress.482069346.description:         "" => ""
  egress.482069346.from_port:           "" => "0"
  egress.482069346.ipv6_cidr_blocks.#:  "" => "0"
  egress.482069346.prefix_list_ids.#:   "" => "0"
  egress.482069346.protocol:            "" => "-1"
  egress.482069346.security_groups.#:   "" => "0"
  egress.482069346.self:                "" => "false"
  egress.482069346.to_port:             "" => "0"
  ingress.#:                            "" => "1"
  ingress.482069346.cidr_blocks.#:      "" => "1"
  ingress.482069346.cidr_blocks.0:      "" => "0.0.0.0/0"
  ingress.482069346.description:        "" => ""
  ingress.482069346.from_port:          "" => "0"
  ingress.482069346.ipv6_cidr_blocks.#: "" => "0"
  ingress.482069346.prefix_list_ids.#:  "" => "0"
  ingress.482069346.protocol:           "" => "-1"
  ingress.482069346.security_groups.#:  "" => "0"
  ingress.482069346.self:               "" => "false"
  ingress.482069346.to_port:            "" => "0"
  name:                                 "" => "allow_all"
  owner_id:                             "" => "<computed>"
  revoke_rules_on_delete:               "" => "false"
  vpc_id:                               "" => "vpc-00f405f1ee36ab592"
aws_security_group.allow-all: Creation complete after 5s (ID: sg-0e2900727ccb690da)
 
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
cs


Check created vpc and security group



Destroy terraform

it turn off all of terraform configuration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ terraform destroy
aws_vpc.test: Refreshing state... (ID: vpc-00f405f1ee36ab592)
aws_security_group.allow-all: Refreshing state... (ID: sg-0e2900727ccb690da)
 
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy
 
Terraform will perform the following actions:
 
  - aws_security_group.allow-all
 
  - aws_vpc.test
 
 
Plan: 0 to add, 0 to change, 2 to destroy.
 
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.
 
  Enter a value: yes
 
aws_security_group.allow-all: Destroying... (ID: sg-0e2900727ccb690da)
aws_security_group.allow-all: Destruction complete after 1s
aws_vpc.test: Destroying... (ID: vpc-00f405f1ee36ab592)
aws_vpc.test: Destruction complete after 1s
 
Destroy complete! Resources: 2 destroyed.
cs




Posted by 사용자 guru_k

댓글을 달아 주세요