Skip to content

Commit

Permalink
TypeScript version of webserver example for language parity (pulumi#937)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
David Wrede and Lee Zen authored Apr 30, 2021
1 parent f8b9e06 commit 0389c5c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Example | Description |
[Twitter](aws-ts-twitter-athena) | Query Twitter every 2 minutes, store the results in S3, and set up an Athena table and query.
[URL Shortener](aws-ts-url-shortener-cache-http) | Create a serverless URL shortener that uses high-level components.
[Voting App](aws-ts-voting-app) | Create a simple voting app using Redis and Python Flask.
[Web Server](aws-ts-webserver) | Deploy an EC2 Virtual machine using TypeScript to run a Python web server.
[Web Server with Manual Provisioning](aws-ts-ec2-provisioners) | Use Pulumi dynamic providers to accomplish post-provisioning configuration steps.

### JavaScript
Expand Down
2 changes: 2 additions & 0 deletions aws-ts-webserver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/node_modules/
8 changes: 8 additions & 0 deletions aws-ts-webserver/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: aws-ts-webserver
runtime: nodejs
description: Basic example of an AWS web server accessible over HTTP
template:
config:
aws:region:
description: The AWS region to deploy into
default: us-east-1
62 changes: 62 additions & 0 deletions aws-ts-webserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Web Server Using Amazon EC2

This example deploys a simple AWS EC2 virtual machine running a Python web server.

## Deploying the App

To deploy your infrastructure, follow the below steps.

### Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
2. [Configure AWS Credentials](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/)

### Steps

After cloning this repo, from this working directory, run these commands:

1. Create a new stack, which is an isolated deployment target for this example:

```bash
$ pulumi stack init
```

2. Set the required configuration variables for this program:

```bash
$ pulumi config set aws:region us-east-1
```

3. Stand up the VM, which will also boot up your Python web server on port 80:

```bash
$ pulumi up
```

4. After a couple minutes, your VM will be ready, and two stack outputs are printed:

```bash
$ pulumi stack output
Current stack outputs (2):
OUTPUT VALUE
publicHostName ec2-53-40-227-82.compute-1.amazonaws.com
publicIp 53.40.227.82
```

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:

```bash
$ curl $(pulumi stack output publicIp)
Hello, World!
```

6. From there, feel free to experiment. Simply making edits and running `pulumi up` will incrementally update your VM.

7. Afterwards, destroy your stack and remove it:

```bash
$ pulumi destroy --yes
$ pulumi stack rm --yes
```
37 changes: 37 additions & 0 deletions aws-ts-webserver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.

import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

// Get the id for the latest Amazon Linux AMI
const ami = aws.ec2.getAmi({
filters: [
{ name: "name", values: ["amzn-ami-hvm-*-x86_64-ebs"] },
],
owners: ["137112412989"], // Amazon
mostRecent: true,
}).then(result => result.id);

// create a new security group for port 80
const group = new aws.ec2.SecurityGroup("web-secgrp", {
ingress: [
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
],
});

// (optional) create a simple web server using the startup script for the instance
const userData =
`#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`;

const server = new aws.ec2.Instance("web-server-www", {
tags: { "Name": "web-server-www" },
instanceType: aws.ec2.InstanceType.T2_Micro, // t2.micro is available in the AWS free tier
vpcSecurityGroupIds: [ group.id ], // reference the group object above
ami: ami,
userData: userData, // start a simple web server
});

export const publicIp = server.publicIp;
export const publicHostName = server.publicDns;
11 changes: 11 additions & 0 deletions aws-ts-webserver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "aws-ts-webserver",
"devDependencies": {
"@types/node": "^10.0.0"
},
"dependencies": {
"@pulumi/aws": "^3.25.1",
"@pulumi/awsx": "^0.24.0",
"@pulumi/pulumi": "^2.0.0"
}
}
18 changes: 18 additions & 0 deletions aws-ts-webserver/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}

0 comments on commit 0389c5c

Please sign in to comment.