Skip to content

Commit 0389c5c

Browse files
David WredeLee Zen
andauthored
TypeScript version of webserver example for language parity (pulumi#937)
* TypeScript version of webserver example for language parity * Fixed linting errors and export errors * Updated example to use enum * Updated README output * Apply suggestions from code review Co-authored-by: Lee Zen <[email protected]> Co-authored-by: Lee Zen <[email protected]>
1 parent f8b9e06 commit 0389c5c

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Example | Description |
9595
[Twitter](aws-ts-twitter-athena) | Query Twitter every 2 minutes, store the results in S3, and set up an Athena table and query.
9696
[URL Shortener](aws-ts-url-shortener-cache-http) | Create a serverless URL shortener that uses high-level components.
9797
[Voting App](aws-ts-voting-app) | Create a simple voting app using Redis and Python Flask.
98+
[Web Server](aws-ts-webserver) | Deploy an EC2 Virtual machine using TypeScript to run a Python web server.
9899
[Web Server with Manual Provisioning](aws-ts-ec2-provisioners) | Use Pulumi dynamic providers to accomplish post-provisioning configuration steps.
99100

100101
### JavaScript

aws-ts-webserver/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin/
2+
/node_modules/

aws-ts-webserver/Pulumi.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: aws-ts-webserver
2+
runtime: nodejs
3+
description: Basic example of an AWS web server accessible over HTTP
4+
template:
5+
config:
6+
aws:region:
7+
description: The AWS region to deploy into
8+
default: us-east-1

aws-ts-webserver/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Web Server Using Amazon EC2
4+
5+
This example deploys a simple AWS EC2 virtual machine running a Python web server.
6+
7+
## Deploying the App
8+
9+
To deploy your infrastructure, follow the below steps.
10+
11+
### Prerequisites
12+
13+
1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
14+
2. [Configure AWS Credentials](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/)
15+
16+
### Steps
17+
18+
After cloning this repo, from this working directory, run these commands:
19+
20+
1. Create a new stack, which is an isolated deployment target for this example:
21+
22+
```bash
23+
$ pulumi stack init
24+
```
25+
26+
2. Set the required configuration variables for this program:
27+
28+
```bash
29+
$ pulumi config set aws:region us-east-1
30+
```
31+
32+
3. Stand up the VM, which will also boot up your Python web server on port 80:
33+
34+
```bash
35+
$ pulumi up
36+
```
37+
38+
4. After a couple minutes, your VM will be ready, and two stack outputs are printed:
39+
40+
```bash
41+
$ pulumi stack output
42+
Current stack outputs (2):
43+
OUTPUT VALUE
44+
publicHostName ec2-53-40-227-82.compute-1.amazonaws.com
45+
publicIp 53.40.227.82
46+
```
47+
48+
5. Thanks to the security group making port 80 accessible to the 0.0.0.0/0 CIDR block (all addresses), we can curl it:
49+
50+
```bash
51+
$ curl $(pulumi stack output publicIp)
52+
Hello, World!
53+
```
54+
55+
6. From there, feel free to experiment. Simply making edits and running `pulumi up` will incrementally update your VM.
56+
57+
7. Afterwards, destroy your stack and remove it:
58+
59+
```bash
60+
$ pulumi destroy --yes
61+
$ pulumi stack rm --yes
62+
```

aws-ts-webserver/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
2+
3+
import * as aws from "@pulumi/aws";
4+
import * as pulumi from "@pulumi/pulumi";
5+
6+
// Get the id for the latest Amazon Linux AMI
7+
const ami = aws.ec2.getAmi({
8+
filters: [
9+
{ name: "name", values: ["amzn-ami-hvm-*-x86_64-ebs"] },
10+
],
11+
owners: ["137112412989"], // Amazon
12+
mostRecent: true,
13+
}).then(result => result.id);
14+
15+
// create a new security group for port 80
16+
const group = new aws.ec2.SecurityGroup("web-secgrp", {
17+
ingress: [
18+
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
19+
],
20+
});
21+
22+
// (optional) create a simple web server using the startup script for the instance
23+
const userData =
24+
`#!/bin/bash
25+
echo "Hello, World!" > index.html
26+
nohup python -m SimpleHTTPServer 80 &`;
27+
28+
const server = new aws.ec2.Instance("web-server-www", {
29+
tags: { "Name": "web-server-www" },
30+
instanceType: aws.ec2.InstanceType.T2_Micro, // t2.micro is available in the AWS free tier
31+
vpcSecurityGroupIds: [ group.id ], // reference the group object above
32+
ami: ami,
33+
userData: userData, // start a simple web server
34+
});
35+
36+
export const publicIp = server.publicIp;
37+
export const publicHostName = server.publicDns;

aws-ts-webserver/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "aws-ts-webserver",
3+
"devDependencies": {
4+
"@types/node": "^10.0.0"
5+
},
6+
"dependencies": {
7+
"@pulumi/aws": "^3.25.1",
8+
"@pulumi/awsx": "^0.24.0",
9+
"@pulumi/pulumi": "^2.0.0"
10+
}
11+
}

aws-ts-webserver/tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"outDir": "bin",
5+
"target": "es2016",
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"sourceMap": true,
9+
"experimentalDecorators": true,
10+
"pretty": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"noImplicitReturns": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.ts"
17+
]
18+
}

0 commit comments

Comments
 (0)