[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


Posted by 사용자 guru_k

댓글을 달아 주세요

[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

댓글을 달아 주세요

[Terraform] AWS with Terraform - install terraform (테라폼 설치)



download terraform from the official download page.



unzip the file and move Terraform binary to /usr/bin.


1
$ sudo mv terraform /usr/bin && sudo chmod +x
cs


test `terraform` command


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
$ terraform
Usage: terraform [-version] [-help] <command> [args]
 
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    push               Upload this Terraform module to Atlas to run
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management
All other commands:
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    state              Advanced state management
cs


Posted by 사용자 guru_k

댓글을 달아 주세요

aws ec2 describe-instances --filter

--filter 옵션을 이용하여 tag를 생성한 instances 조회



위와 같이 태그의 키를 Key로 값을 TEST-AWS로 설정하였을 경우 해당 instances 조회


Name=tag:Key

Values=TEST-AWS


로 입력하면 키를 Key로 값을 TEST-AWS로 가진 instances 조회 가능


1
$ aws ec2 describe-instances --filter "Name=tag:Key,Values=TEST-AWS"
cs

 


위와 같이 조회 시 태그의 키가 Key 값이 TEST-AWS로 설정된 모든 instances가 조회된다.

이때 instances의 상태(pending | running | shutting-down | terminated | stopping | stopped)에 따른 조회를 하고 싶다면 아래와 같은 옵션을 추가하여 조회한다.


"Name=instance-state-name,Values=stopped"


1
$ aws ec2 describe-instances --filter "Name=tag:Key,Values=TEST-AWS" "Name=instance-state-name,Values=stopped"
cs


이렇게 조회 시 instances의 모든 정보가 return 된다.

return 되는 정보에서 InstanceId 만 알고 싶다면 아래와 같은 옵션을 추가한다.


--query "Reservations[].Instances[].InstanceId"



1
2
3
4
$ aws ec2 describe-instances --filter "Name=tag:Key,Values=TEST-AWS" "Name=instance-state-name,Values=stopped" --query "Reservations[].Instances[].InstanceId"
[
    "i-001e1ee223a323112"
]
cs


Posted by 사용자 guru_k

댓글을 달아 주세요

awscli 설치

아래 명령어를 이용하여 awscli 설치


1
$ pip install awscli --upgrade --user
cs

awscli 사용을 위한 configure 설정

aws ec2 instance 정보를 가져오기 위한 명령어 'aws ec2 describe-instances' 를 실행하면 아래와 같은 메시지를 볼 수 있다.

1
2
$ aws ec2 describe-instances
You must specify a region. You can also configure your region by running "aws configure".
cs


aws cli를 사용하기 위해선 아래와 같은 정보를 설정해줘야 한다.


1
2
3
4
[profile adminuser]
aws_access_key_id = adminuser access key ID
aws_secret_access_key = adminuser secret access key
region = aws-region


cs


access_key_id 와 secret_access_key 는 

IAM Console -> 사용자 -> 해당 IAM 사용자를 선택하면 확인 할 수 있습니다.

id와 key를 확인 했다면 aws configure를 설정합니다.

1
2
3
4
5
$ aws configure
AWS Access Key ID [None]: BKTESTACCESSKEYXL2SI7C
AWS Secret Access Key [None]: 6DV/tes+t/0QF0AG9secret/97skkey
Default region name [None]: ap-northeast-2
Default output format [None]: json
cs


 
이후 aws ec2 describe-instances 를 사용해보면 현재 존재하는 모든 ec2 instances를 확인할 수 있습니다.

1
$ aws ec2 describe-instances
{ "Reservations": [] }
cs







Posted by 사용자 guru_k

댓글을 달아 주세요

javascript에서 aws clie waiter 사용


aws cli중 waiter 함수를 사용하여 특정 인스턴스의 상태까지 대기 할 수 있다.


instanceRunning 상태 대기

1
2
3
4
5
const AWS = require('aws-sdk');
const ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
 
await ec2.waitFor('instanceRunning', params).promise();
console.log("instance's status is running");
cs

하지만 instanceRunning 상태라고 하지만 ssm 을 통해 해당 인스턴스에 명령어를 날릴 경우 'InvaildInstanceId: null' 에러가 발생할 수 있다.
그럴경우 instanceStatusOk waiter를 이용하면 해당 상태 대기 후 바로 명령어를 사용할 수 있는 상태가 된다.

1
2
3
4
5
const AWS = require('aws-sdk');
const ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
 
await ec2.waitFor('instanceStatusOk', params).promise();
console.log("instance's status is ok");
cs


Waiter Resource States 는 아래 aws document에서 확인할 수 있습니다.



Posted by 사용자 guru_k

댓글을 달아 주세요