Initial version of webserver example
MODIFY
README.md
README.md
# Pulumi Examples
- [URL shortener](/url-shortener)
+- [Web Server](/webserver)
\ No newline at end of file

ADD
webserver/.gitignore
+/.pulumi/
+/bin/
+/node_modules/
+/yarn.lock
\ No newline at end of file

ADD
webserver/Pulumi.yaml
+name: webserver
+runtime: nodejs
+description: Basic example of an AWS web server accessible over HTTP.
\ No newline at end of file

ADD
webserver/README.md
+# Pulumi web server
+
+Starting point for building the Pulumi web server sample.
\ No newline at end of file

ADD
webserver/index.ts
+import * as aws from "@pulumi/aws";
+import * as webserver from "./webserver"; // use our custom component
+
+(async () => {
+ let zones: string[] = (await aws.getAvailabilityZones()).names;
+ for (let i = 0; i < zones.length; i++) {
+ let server = webserver.createInstance("web-server-www-" + i, "t2.micro", zones[i]);
+ }
+})();
\ No newline at end of file

ADD
webserver/package.json
+{
+ "name": "webserver",
+ "version": "0.1",
+ "main": "bin/index.js",
+ "typings": "bin/index.d.ts",
+ "scripts": {
+ "build": "tsc"
+ },
+ "devDependencies": {
+ "typescript": "^2.5.3"
+ },
+ "peerDependencies": {
+ "@pulumi/aws": "*",
+ "pulumi": "*"
+ },
+ "dependencies": {
+ "@types/node": "^8.0.26"
+ }
+}

ADD
webserver/tsconfig.json
+{
+ "compilerOptions": {
+ "outDir": "bin",
+ "target": "es6",
+ "module": "commonjs",
+ "moduleResolution": "node",
+ "declaration": true,
+ "sourceMap": true,
+ "stripInternal": true,
+ "experimentalDecorators": true,
+ "pretty": true,
+ "noFallthroughCasesInSwitch": true,
+ "noImplicitAny": true,
+ "noImplicitReturns": true,
+ "forceConsistentCasingInFileNames": true,
+ "strictNullChecks": true
+ },
+ "files": [
+ "index.ts"
+ ]
+}

ADD
webserver/webserver.ts
+import * as aws from "@pulumi/aws";
+
+export let size: aws.ec2.InstanceType = "t2.micro"; // the type InstanceType contains friendly names for AWS instance sizes
+
+// create a new security group for port 80
+let group = new aws.ec2.SecurityGroup("web-secgrp", {
+ description: "Enable HTTP access",
+ ingress: [
+ { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
+ ],
+});
+
+// a function to find the Amazon Linux AMI for the current region
+async function getLinuxAmi() {
+ let result = await aws.getAmi({
+ owners: ["amazon"],
+ filter: [{ name: "name", values: ["amzn-ami-2017.03.g-amazon-ecs-optimized"] }]
+ });
+ return result.imageId;
+}
+
+let userData =
+ `#!/bin/bash
+ echo "Hello, World!\nInstance metadata:" > index.html
+ curl http://169.254.169.254/latest/meta-data/placement/availability-zone >> index.html
+ nohup python -m SimpleHTTPServer 80 &`;
+
+export function createInstance(name: string, size: aws.ec2.InstanceType, zone: string): aws.ec2.Instance {
+ let result = new aws.ec2.Instance(name, {
+ availabilityZone: zone,
+ instanceType: size, // use function argument for size
+ securityGroups: [ group.name ], // reference the group object above
+ ami: getLinuxAmi(), // call custom function
+ tags: { "Name": name },
+ userData: userData // set up a simple web server
+ });
+
+ result.publicDns.then(url => console.log(`Server URL: http://${url}`));
+
+ return result;
+}
\ No newline at end of file

Initial version of webserver example