forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
David Wrede
and
Lee Zen
authored
Apr 30, 2021
1 parent
f8b9e06
commit 0389c5c
Showing
7 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/bin/ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |