Skip to content

Commit ffc8e80

Browse files
zzxwillswgillespie
authored andcommitted
Add an example of deploying a Nginx server in GCP (#234)
* Add an example of deploying a Nginx server in GCP In a GCP instance, deploy a Nginx server by `gcp-py` environment. * add Python environment configuration in README
1 parent fd2d349 commit ffc8e80

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed

gcp-py-instance-nginx/Pulumi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: gcp-instance-nginx
2+
runtime: python
3+
description: Deploy a Nginx Server in a GCP instance

gcp-py-instance-nginx/README.md

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Pulumi Nginx Server in a instance (GCP)
4+
5+
Starting point for building the Pulumi nginx server sample in Google Cloud Platform.
6+
7+
## Running the App
8+
9+
1. Create a new stack:
10+
11+
```
12+
$ pulumi stack init gcp-instance-nginx
13+
```
14+
15+
2. Configure Python environment:
16+
```
17+
virtualenv -p python3 venv
18+
source venv/bin/activate
19+
pip install pulumi_gcp
20+
```
21+
22+
3. Configure the project:
23+
24+
```
25+
$ export GOOGLE_PROJECT=cncf-230209; export GOOGLE_REGION=asia-east1; export GOOGLE_ZONE=asia-east1-a;
26+
$ export GOOGLE_CREDENTIALS=YOURGCPCREDENTIALS
27+
```
28+
29+
4. Run `pulumi up` to preview and deploy changes:
30+
31+
```
32+
Previewing update (gcp-instance-nginx):
33+
34+
Type Name Plan
35+
pulumi:pulumi:Stack gcp-instance-nginx-gcp-instance-nginx
36+
+ ├─ gcp:compute:Network network create
37+
+ ├─ gcp:compute:Address poc create
38+
+ ├─ gcp:compute:Firewall firewall create
39+
+ └─ gcp:compute:Instance poc create
40+
41+
Resources:
42+
+ 4 to create
43+
1 unchanged
44+
45+
Do you want to perform this update? yes
46+
Updating (gcp-instance-nginx):
47+
48+
Type Name Status
49+
pulumi:pulumi:Stack gcp-instance-nginx-gcp-instance-nginx
50+
+ ├─ gcp:compute:Network network created
51+
+ ├─ gcp:compute:Address poc created
52+
+ ├─ gcp:compute:Instance poc created
53+
+ └─ gcp:compute:Firewall firewall created
54+
55+
Outputs:
56+
+ external_ip : "34.80.8.146"
57+
+ instance_name : "poc"
58+
+ instance_network: [
59+
+ [0]: {
60+
+ accessConfigs : [
61+
+ [0]: {
62+
+ assignedNatIp : "34.80.8.146"
63+
+ natIp : "34.80.8.146"
64+
+ network_tier : "PREMIUM"
65+
}
66+
]
67+
+ address : "10.140.0.2"
68+
+ name : "nic0"
69+
+ network : "https://www.googleapis.com/compute/v1/projects/cncf-230209/global/networks/network-6054c92"
70+
+ networkIp : "10.140.0.2"
71+
+ subnetwork : "https://www.googleapis.com/compute/v1/projects/cncf-230209/regions/asia-east1/subnetworks/network-6054c92"
72+
+ subnetworkProject: "cncf-230209"
73+
}
74+
]
75+
76+
Resources:
77+
+ 4 created
78+
1 unchanged
79+
80+
Duration: 51s
81+
```
82+
83+
5. Curl the HTTP server:
84+
85+
```
86+
$ curl $(pulumi stack output external_ip)
87+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
88+
89+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
90+
<head>
91+
<title>Test Page for the Nginx HTTP Server on Fedora</title>
92+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
93+
<style type="text/css">
94+
/*<![CDATA[*/
95+
body {
96+
background-color: #fff;
97+
color: #000;
98+
font-size: 0.9em;
99+
font-family: sans-serif,helvetica;
100+
margin: 0;
101+
padding: 0;
102+
}
103+
:link {
104+
color: #c00;
105+
}
106+
:visited {
107+
color: #c00;
108+
}
109+
a:hover {
110+
color: #f50;
111+
}
112+
h1 {
113+
text-align: center;
114+
margin: 0;
115+
padding: 0.6em 2em 0.4em;
116+
background-color: #294172;
117+
color: #fff;
118+
font-weight: normal;
119+
font-size: 1.75em;
120+
border-bottom: 2px solid #000;
121+
}
122+
h1 strong {
123+
font-weight: bold;
124+
font-size: 1.5em;
125+
}
126+
h2 {
127+
text-align: center;
128+
background-color: #3C6EB4;
129+
font-size: 1.1em;
130+
font-weight: bold;
131+
color: #fff;
132+
margin: 0;
133+
padding: 0.5em;
134+
border-bottom: 2px solid #294172;
135+
}
136+
hr {
137+
display: none;
138+
}
139+
.content {
140+
padding: 1em 5em;
141+
}
142+
.alert {
143+
border: 2px solid #000;
144+
}
145+
146+
img {
147+
border: 2px solid #fff;
148+
padding: 2px;
149+
margin: 2px;
150+
}
151+
a:hover img {
152+
border: 2px solid #294172;
153+
}
154+
.logos {
155+
margin: 1em;
156+
text-align: center;
157+
}
158+
/*]]>*/
159+
</style>
160+
</head>
161+
162+
<body>
163+
<h1>Welcome to <strong>nginx</strong> on Fedora!</h1>
164+
165+
<div class="content">
166+
<p>This page is used to test the proper operation of the
167+
<strong>nginx</strong> HTTP server after it has been
168+
installed. If you can read this page, it means that the
169+
web server installed at this site is working
170+
properly.</p>
171+
172+
<div class="alert">
173+
<h2>Website Administrator</h2>
174+
<div class="content">
175+
<p>This is the default <tt>index.html</tt> page that
176+
is distributed with <strong>nginx</strong> on
177+
Fedora. It is located in
178+
<tt>/usr/share/nginx/html</tt>.</p>
179+
180+
<p>You should now put your content in a location of
181+
your choice and edit the <tt>root</tt> configuration
182+
directive in the <strong>nginx</strong>
183+
configuration file
184+
<tt>/etc/nginx/nginx.conf</tt>.</p>
185+
186+
</div>
187+
</div>
188+
189+
<div class="logos">
190+
<a href="http://nginx.net/"><img
191+
src="nginx-logo.png"
192+
alt="[ Powered by nginx ]"
193+
width="121" height="32" /></a>
194+
195+
<a href="http://fedoraproject.org/"><img
196+
src="poweredby.png"
197+
alt="[ Powered by Fedora ]"
198+
width="88" height="31" /></a>
199+
</div>
200+
</div>
201+
</body>
202+
</html>
203+
```
204+
205+
6. Destroy the created resources:
206+
207+
```
208+
$ pulumi destroy
209+
Previewing destroy (gcp-instance-nginx):
210+
211+
Type Name Plan
212+
- pulumi:pulumi:Stack gcp-instance-nginx-gcp-instance-nginx delete
213+
- ├─ gcp:compute:Instance poc delete
214+
- ├─ gcp:compute:Firewall firewall delete
215+
- ├─ gcp:compute:Network network delete
216+
- └─ gcp:compute:Address poc delete
217+
218+
Resources:
219+
- 5 to delete
220+
221+
Do you want to perform this destroy? yes
222+
Destroying (gcp-instance-nginx):
223+
224+
Type Name Status
225+
- pulumi:pulumi:Stack gcp-instance-nginx-gcp-instance-nginx deleted
226+
- ├─ gcp:compute:Instance poc deleted
227+
- ├─ gcp:compute:Firewall firewall deleted
228+
- ├─ gcp:compute:Network network deleted
229+
- └─ gcp:compute:Address poc deleted
230+
231+
Resources:
232+
- 5 deleted
233+
234+
Duration: 1m57s
235+
```
236+
237+
7. Destroy the stack:
238+
239+
```
240+
$ pulumi stack rm
241+
This will permanently remove the 'gcp-instance-nginx' stack!
242+
Please confirm that this is what you'd like to do by typing ("gcp-instance-nginx"): gcp-instance-nginx
243+
Stack 'gcp-instance-nginx' has been removed!
244+
```

gcp-py-instance-nginx/__main__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pulumi
2+
from pulumi_gcp import compute
3+
4+
disk = {
5+
'initializeParams': {
6+
'image': "centos-cloud/centos-7-v20190116"
7+
}
8+
}
9+
10+
script = "#!/bin/bash\nsudo touch /tmp/a.txt\nsudo yum install -y nginx\nsudo service nginx start"
11+
addr = compute.address.Address(resource_name='poc')
12+
external_ip = addr.address
13+
14+
network = compute.Network("network")
15+
network_interface = [
16+
{
17+
'network': network.id,
18+
'accessConfigs': [{'nat_ip': external_ip}],
19+
}
20+
]
21+
22+
firewall = compute.Firewall("firewall", network=network.self_link, allows=[{
23+
'protocol': "tcp",
24+
'ports': ["22", "80"]
25+
}])
26+
27+
instance = compute.Instance('poc', name='poc', boot_disk=disk, machine_type="f1-micro",
28+
network_interfaces=network_interface, metadata_startup_script=script)
29+
30+
# Export the DNS name of the bucket
31+
pulumi.export('instance_name', instance.name)
32+
pulumi.export('instance_network', instance.network_interfaces)
33+
pulumi.export('external_ip', addr.address)

0 commit comments

Comments
 (0)