본문 바로가기
Programming/AWS

[Terraform] AWS with Terraform - ec2 instance (테라폼 ec2 인스턴스 생성)

by guru_k 2018. 12. 10.
728x90
반응형

[Terraform] AWS with Terraform - ec2 instance (테라폼 ec2 인스턴스 생성) 




EC2 instance

create aws-ec2.tf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
  }
  filter {
    name = "virtualization-type"
    values = ["hvm"]
  }
  owners = ["099720109477"] # Canonical
}
 
resource "aws_instance" "ubuntu-server" {
    ami = "${data.aws_ami.ubuntu.id}" # ami id
    instance_type = "t2.micro" # instance type
subnet_id = "${aws_subnet.a.id}"
    vpc_security_group_ids = ["${aws_security_group.allow-all.id}"]
    count = 1 # the number of EC2 instance
    tags { # tags
        Name = "test"
        Group = "terraform"
    }
}
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
$ 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.
 
data.aws_ami.ubuntu: Refreshing state...
 
------------------------------------------------------------------------
 
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_instance.ubuntu-server
      id:                                   <computed>
      ami:                                  "ami-032f516e93380b8e6"
      arn:                                  <computed>
      associate_public_ip_address:          <computed>
      availability_zone:                    <computed>
      cpu_core_count:                       <computed>
      cpu_threads_per_core:                 <computed>
      ebs_block_device.#:                   <computed>
      ephemeral_block_device.#:             <computed>
      get_password_data:                    "false"
      instance_state:                       <computed>
      instance_type:                        "t2.micro"
      ipv6_address_count:                   <computed>
      ipv6_addresses.#:                     <computed>
      key_name:                             <computed>
      network_interface.#:                  <computed>
      network_interface_id:                 <computed>
      password_data:                        <computed>
      placement_group:                      <computed>
      primary_network_interface_id:         <computed>
      private_dns:                          <computed>
      private_ip:                           <computed>
      public_dns:                           <computed>
      public_ip:                            <computed>
      root_block_device.#:                  <computed>
      security_groups.#:                    <computed>
      source_dest_check:                    "true"
      subnet_id:                            "${aws_subnet.a.id}"
      tags.%:                               "2"
      tags.Group:                           "terraform"
      tags.Name:                            "test"
      tenancy:                              <computed>
      volume_tags.%:                        <computed>
      vpc_security_group_ids.#:             <computed>
 
  + 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_subnet.a
      id:                                   <computed>
      arn:                                  <computed>
      assign_ipv6_address_on_creation:      "false"
      availability_zone:                    "ap-northeast-1a"
      availability_zone_id:                 <computed>
      cidr_block:                           "172.10.0.0/24"
      ipv6_cidr_block:                      <computed>
      ipv6_cidr_block_association_id:       <computed>
      map_public_ip_on_launch:              "false"
      owner_id:                             <computed>
      vpc_id:                               "${aws_vpc.test.id}"
 
  + aws_subnet.c
      id:                                   <computed>
      arn:                                  <computed>
      assign_ipv6_address_on_creation:      "false"
      availability_zone:                    "ap-northeast-1c"
      availability_zone_id:                 <computed>
      cidr_block:                           "172.10.1.0/24"
      ipv6_cidr_block:                      <computed>
      ipv6_cidr_block_association_id:       <computed>
      map_public_ip_on_launch:              "false"
      owner_id:                             <computed>
      vpc_id:                               "${aws_vpc.test.id}"
 
  + aws_vpc.test
      id:                                   <computed>
      arn:                                  <computed>
      assign_generated_ipv6_cidr_block:     "false"
      cidr_block:                           "172.10.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: 5 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
$ terraform apply
data.aws_ami.ubuntu: Refreshing state...
 
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_instance.ubuntu-server
      id:                                   <computed>
      ami:                                  "ami-032f516e93380b8e6"
      arn:                                  <computed>
      associate_public_ip_address:          <computed>
      availability_zone:                    <computed>
      cpu_core_count:                       <computed>
      cpu_threads_per_core:                 <computed>
      ebs_block_device.#:                   <computed>
      ephemeral_block_device.#:             <computed>
      get_password_data:                    "false"
      instance_state:                       <computed>
      instance_type:                        "t2.micro"
      ipv6_address_count:                   <computed>
      ipv6_addresses.#:                     <computed>
      key_name:                             <computed>
      network_interface.#:                  <computed>
      network_interface_id:                 <computed>
      password_data:                        <computed>
      placement_group:                      <computed>
      primary_network_interface_id:         <computed>
      private_dns:                          <computed>
      private_ip:                           <computed>
      public_dns:                           <computed>
      public_ip:                            <computed>
      root_block_device.#:                  <computed>
      security_groups.#:                    <computed>
      source_dest_check:                    "true"
      subnet_id:                            "${aws_subnet.a.id}"
      tags.%:                               "2"
      tags.Group:                           "terraform"
      tags.Name:                            "test"
      tenancy:                              <computed>
      volume_tags.%:                        <computed>
      vpc_security_group_ids.#:             <computed>
 
  + 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_subnet.a
      id:                                   <computed>
      arn:                                  <computed>
      assign_ipv6_address_on_creation:      "false"
      availability_zone:                    "ap-northeast-1a"
      availability_zone_id:                 <computed>
      cidr_block:                           "172.10.0.0/24"
      ipv6_cidr_block:                      <computed>
      ipv6_cidr_block_association_id:       <computed>
      map_public_ip_on_launch:              "false"
      owner_id:                             <computed>
      vpc_id:                               "${aws_vpc.test.id}"
 
  + aws_subnet.c
      id:                                   <computed>
      arn:                                  <computed>
      assign_ipv6_address_on_creation:      "false"
      availability_zone:                    "ap-northeast-1c"
      availability_zone_id:                 <computed>
      cidr_block:                           "172.10.1.0/24"
      ipv6_cidr_block:                      <computed>
      ipv6_cidr_block_association_id:       <computed>
      map_public_ip_on_launch:              "false"
      owner_id:                             <computed>
      vpc_id:                               "${aws_vpc.test.id}"
 
  + aws_vpc.test
      id:                                   <computed>
      arn:                                  <computed>
      assign_generated_ipv6_cidr_block:     "false"
      cidr_block:                           "172.10.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: 5 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.10.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: Creation complete after 5s (ID: vpc-0fdafeb4b10fa2f5f)
aws_subnet.c: Creating...
  arn:                             "" => "<computed>"
  assign_ipv6_address_on_creation: "" => "false"
  availability_zone:               "" => "ap-northeast-1c"
  availability_zone_id:            "" => "<computed>"
  cidr_block:                      "" => "172.10.1.0/24"
  ipv6_cidr_block:                 "" => "<computed>"
  ipv6_cidr_block_association_id:  "" => "<computed>"
  map_public_ip_on_launch:         "" => "false"
  owner_id:                        "" => "<computed>"
  vpc_id:                          "" => "vpc-0fdafeb4b10fa2f5f"
aws_subnet.a: Creating...
  arn:                             "" => "<computed>"
  assign_ipv6_address_on_creation: "" => "false"
  availability_zone:               "" => "ap-northeast-1a"
  availability_zone_id:            "" => "<computed>"
  cidr_block:                      "" => "172.10.0.0/24"
  ipv6_cidr_block:                 "" => "<computed>"
  ipv6_cidr_block_association_id:  "" => "<computed>"
  map_public_ip_on_launch:         "" => "false"
  owner_id:                        "" => "<computed>"
  vpc_id:                          "" => "vpc-0fdafeb4b10fa2f5f"
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-0fdafeb4b10fa2f5f"
aws_subnet.a: Creation complete after 1s (ID: subnet-090c8cdb3797e0a1a)
aws_subnet.c: Creation complete after 1s (ID: subnet-089dcad09f9fd58b9)
aws_security_group.allow-all: Creation complete after 5s (ID: sg-051fd4f9d85a75731)
aws_instance.ubuntu-server: Creating...
  ami:                               "" => "ami-032f516e93380b8e6"
  arn:                               "" => "<computed>"
  associate_public_ip_address:       "" => "<computed>"
  availability_zone:                 "" => "<computed>"
  cpu_core_count:                    "" => "<computed>"
  cpu_threads_per_core:              "" => "<computed>"
  ebs_block_device.#:                "" => "<computed>"
  ephemeral_block_device.#:          "" => "<computed>"
  get_password_data:                 "" => "false"
  instance_state:                    "" => "<computed>"
  instance_type:                     "" => "t2.micro"
  ipv6_address_count:                "" => "<computed>"
  ipv6_addresses.#:                  "" => "<computed>"
  key_name:                          "" => "<computed>"
  network_interface.#:               "" => "<computed>"
  network_interface_id:              "" => "<computed>"
  password_data:                     "" => "<computed>"
  placement_group:                   "" => "<computed>"
  primary_network_interface_id:      "" => "<computed>"
  private_dns:                       "" => "<computed>"
  private_ip:                        "" => "<computed>"
  public_dns:                        "" => "<computed>"
  public_ip:                         "" => "<computed>"
  root_block_device.#:               "" => "<computed>"
  security_groups.#:                 "" => "<computed>"
  source_dest_check:                 "" => "true"
  subnet_id:                         "" => "subnet-090c8cdb3797e0a1a"
  tags.%:                            "" => "2"
  tags.Group:                        "" => "terraform"
  tags.Name:                         "" => "test"
  tenancy:                           "" => "<computed>"
  volume_tags.%:                     "" => "<computed>"
  vpc_security_group_ids.#:          "" => "1"
  vpc_security_group_ids.2562857549"" => "sg-051fd4f9d85a75731"
aws_instance.ubuntu-server: Still creating... (10s elapsed)
aws_instance.ubuntu-server: Still creating... (20s elapsed)
aws_instance.ubuntu-server: Still creating... (30s elapsed)
aws_instance.ubuntu-server: Creation complete after 36s (ID: i-0b2b2dba8d886928e)
 
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
cs


Check created server




Destroy terraform

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
$ terraform destroy
aws_vpc.test: Refreshing state... (ID: vpc-0fdafeb4b10fa2f5f)
data.aws_ami.ubuntu: Refreshing state...
aws_subnet.c: Refreshing state... (ID: subnet-089dcad09f9fd58b9)
aws_security_group.allow-all: Refreshing state... (ID: sg-051fd4f9d85a75731)
aws_subnet.a: Refreshing state... (ID: subnet-090c8cdb3797e0a1a)
aws_instance.ubuntu-server: Refreshing state... (ID: i-0b2b2dba8d886928e)
 
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_instance.ubuntu-server
 
  - aws_security_group.allow-all
 
  - aws_subnet.a
 
  - aws_subnet.c
 
  - aws_vpc.test
 
 
Plan: 0 to add, 0 to change, 5 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_subnet.c: Destroying... (ID: subnet-089dcad09f9fd58b9)
aws_instance.ubuntu-server: Destroying... (ID: i-0b2b2dba8d886928e)
aws_subnet.c: Destruction complete after 1s
aws_instance.ubuntu-server: Still destroying... (ID: i-0b2b2dba8d886928e, 10s elapsed)
aws_instance.ubuntu-server: Still destroying... (ID: i-0b2b2dba8d886928e, 20s elapsed)
aws_instance.ubuntu-server: Still destroying... (ID: i-0b2b2dba8d886928e, 30s elapsed)
aws_instance.ubuntu-server: Still destroying... (ID: i-0b2b2dba8d886928e, 40s elapsed)
aws_instance.ubuntu-server: Still destroying... (ID: i-0b2b2dba8d886928e, 50s elapsed)
aws_instance.ubuntu-server: Still destroying... (ID: i-0b2b2dba8d886928e, 1m0s elapsed)
aws_instance.ubuntu-server: Destruction complete after 1m2s
aws_security_group.allow-all: Destroying... (ID: sg-051fd4f9d85a75731)
aws_subnet.a: Destroying... (ID: subnet-090c8cdb3797e0a1a)
aws_subnet.a: Destruction complete after 1s
aws_security_group.allow-all: Destruction complete after 1s
aws_vpc.test: Destroying... (ID: vpc-0fdafeb4b10fa2f5f)
aws_vpc.test: Destruction complete after 1s
 
Destroy complete! Resources: 5 destroyed.
cs


728x90
반응형

댓글