forked from cloud-gov/cloud-native-aws-terraform-workshop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunit2.html
278 lines (263 loc) · 10.2 KB
/
unit2.html
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>reveal.js</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/18f.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>Infrastructure as Code with Terraform and AWS</h2>
<h2><em>Unit 2: Guest Networking</em></h2>
<p>This 18F <a href="https://github.com/18F/12-factor-aws-terraform-training">workshop</a> is in the worldwide public domain.</p>
</section>
<section>
<h2>Learning objectives</h2>
<ul>
<li>Understand AWS guest networking primitives and configuration</li>
<li>Be able to spin up an EC2 instance with terraform</li>
<li>Know how to make an EC2 box public routable</lu>
</ul>
</section>
<section>
<h2>Set up your access</h2>
<pre><code data-noescape>
export AWS_ACCESS_KEY_ID=<mark>[access key]</mark>
export AWS_SECRET_ACCESS_KEY=<mark>[secret key]</mark>
export AWS_DEFAULT_REGION=us-east-1
ssh-keygen -f ~/.ssh/aws
aws ec2 import-key-pair --key-name <mark>[key name]</mark> \
--public-key-material file://$HOME//.ssh/aws.pub
</code></pre>
(replace <mark>highlighted sections</mark> with the correct values).
</section>
<section>
<h2>Your first terraform file</h2>
<pre><code data-noescape>resource "aws_vpc" "workshop_vpc" {
cidr_block = "<mark>10.0.X.0/24</mark>"
}
resource "aws_subnet" "public_subnet_1" {
vpc_id = "${aws_vpc.workshop_vpc.id}"
availability_zone = "us-east-1a"
cidr_block = "<mark>10.0.X.0/26</mark>"
}
resource "aws_instance" "bastion" {
ami = "ami-04681a1dbd79675a5"
subnet_id = "${aws_subnet.public_subnet_1.id}"
associate_public_ip_address = true
key_name = "<mark>[key name]</mark>"
instance_type = "t2.micro"
}</code></pre>
<aside class="notes">
In a VPC, the max hosts you can create is a /16. So if you want to peer with another VPC, you'll want to start with, say, a /24.
</aside>
</section>
<section>
<h2>Run Terraform</h2>
<ul>
<li>Create a directory called <code>terraform</code></li>
<li>Save the file as <code>terraform/terraform.tf</code></li>
<li>Run <code>terraform init terraform</code></li>
<li>Run <code>terraform apply terraform</code></li>
<li><code>grep '"id": "i-.*"' terraform.tfstate</code></li>
<li>Go find your new EC2 instance in the console</li>
<li>Try pinging it! Hmmm...</li>
</ul>
<aside class="notes">
We're going to talk about networking shortly...
</aside>
</section>
<section>
<h2>Now let's kill it</h2>
<ul>
<li>Run <code>terraform destroy terraform</code></li>
<li>This will take a while...</li>
<li>...So let's talk about terraform state</li>
</ul>
</section>
<section>
<h2>Terraform state</h2>
<ul>
<li>You've seen a file called <code>terraform.tfstate</code></li>
<li>By default Terraform stores state locally</li>
<li>For a real project, you'll store it in an S3 bucket</li>
<li><pre><code>terraform remote config -backend=s3 \
-backend-config="bucket=[unique_bucket_name]" \
-backend-config="key=network/terraform.tfstate" \
-backend-config="region=us-east-1"</code></pre></li>
<li>If your state file is deleted or corrupted...</li>
<li>...you just orphaned everything in your AWS account</li>
</ul>
<aside class="notes">
You probably want multiple S3 buckets for different systems or layers. See <a href="https://charity.wtf/2016/04/14/scrapbag-of-useful-terraform-tips/">Charity Major's blog.</a>
</aside>
</section>
<section>
<h2>Networking</h2>
We created an instance, but we can't connect to it. To do that, we need to understand how networking is modeled in AWS. We'll start with regions and availability zones.
<aside class="notes">
In fact, a fresh AWS account gives you a default VPC with some stuff set up in it so you can create internet routable hosts from the beginning - but we won't be using that.
</aside>
</section>
<section>
<h2>AWS datacenters ("regions")</h2>
<img src="https://d1.awsstatic.com/about-aws/regions/global-infra_3.30.18.b559f46825615c1ae40f319d0c4d9139fea9c492.png" alt="AWS's datacenters"/>
<aside class="notes">
AWS "regions" are completely independent, and function as virtual datacenters (in reality, they're comprised of multiple physical datacenters). Traffic between them goes over the public internet.
</aside>
</section>
<section>
<h2>Availability zones</h2>
Availability zones within regions are isolated, but have low latency links between them.
<img src="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/images/aws_regions.png" alt="AWS's datacenters"/>
</section>
<section>
<h2>VPCs, subnets, and routes</h2>
<img src="http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/images/custom-route-table-diagram.png" style="float:right" width="45%"/>
<ul style="width:45%">
<li class="fragment">Instances always launched in a subnet</li>
<li class="fragment">Subnets always associated with an AZ and a VPC</li>
<li class="fragment">Routers always associated with a VPC and one or more subnets</li>
<li class="fragment">You need a gateway to get anywhere</li>
</ul>
</section>
<section>
<h2>Let's add a gateway & route table</h2>
<pre><code>resource "aws_internet_gateway" "workshop_gw" {
vpc_id = "${aws_vpc.workshop_vpc.id}"
}
resource "aws_route_table" "workshop_route_table" {
vpc_id = "${aws_vpc.workshop_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.workshop_gw.id}"
}
}
resource "aws_route_table_association" "route_subnet_1" {
subnet_id = "${aws_subnet.public_subnet_1.id}"
route_table_id = "${aws_route_table.workshop_route_table.id}"
}</code></pre>
</section>
<section>
<h2>OK let's network!</h2>
<ul>
<li><code>terraform plan terraform</code></li>
<li>How cool is that!</li>
<li><code>terraform apply terraform</code></li>
<li>(Note: <code>terraform apply</code> is idempotent)</li>
<li>But... still can't ping our bastion. Boo!</li>
</ul>
</section>
<section>
<h2>Security groups</h2>
<ul>
<li class="fragment">A security group is like a firewall.</li>
<li class="fragment">They can be attached to multiple AWS resource types</li>
<li class="fragment">By default, VPCs come with a security group that allows egress but not ingress</li>
</ul>
</section>
<section>
<h2>Let's add a security group</h2>
<pre><code>resource "aws_security_group" "bastion_sg" {
vpc_id = "${aws_vpc.workshop_vpc.id}"
ingress {
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
}
</code></pre>
</section>
<section>
<h2>Now attach it to our instance</h2>
<pre><code data-noescape>resource "aws_instance" "bastion" {
ami = "ami-04681a1dbd79675a5"
subnet_id = "${aws_subnet.public_subnet_1.id}"
associate_public_ip_address = true
key_name = "[key name]"
instance_type = "t2.micro"
<mark>vpc_security_group_ids = ["${aws_security_group.bastion_sg.id}"]</mark>
}</code></pre>
<ul>
<li><code>terraform apply terraform</code></li>
<li>...and now we can ping our host!</li>
</ul>
</section>
<section>
<h2>Add ssh access too</h2>
<pre><code> ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
</code></pre>
<ul>
<li><code>terraform apply terraform</code></li>
<li>...and now we can ssh in too!</li>
<li>Once you're in, try pinging Google</li>
<li>Hmmm...</li>
</ul>
</section>
<section>
<h2>Add an egress rule</h2>
<pre><code> egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
</code></pre>
<ul>
<li><code>terraform apply terraform</code></li>
<li>Now we can see out.</li>
<li>By default, AWS lets you egress - but Terraform turns this off unless you declare it explicitly.</li>
</ul>
</section>
<section>
<h2>Unit 2 review</h2>
<ul>
<li class="fragment">We have a VPC, a subnet in an AZ, a gateway, route table, security group...</li>
<li class="fragment">...and a host. <span class="fragment">That we can reach.</span></li>
<li class="fragment">We've understood all the basics of AWS networking</li>
<li class="fragment">By adding block storage (EBS), we can "lift and shift"</li>
<li class="fragment">However there is a cloud on the horizon...</li>
</ul>
</section>
<section>
<a href="unit3.html">Unit 3</a>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>