Skip to content

Commit

Permalink
Add simple HTTP endpoint sample (pulumi#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindydonna committed May 11, 2018
1 parent a876e35 commit f012ad7
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cloud-js-httpendpoint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/.pulumi/
3 changes: 3 additions & 0 deletions cloud-js-httpendpoint/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: cloud-js-httpendpoint
description: A simple HTTP endpoint that returns the number of times a route has been hit.
runtime: nodejs
75 changes: 75 additions & 0 deletions cloud-js-httpendpoint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Serverless REST API on AWS

A simple REST API that counts the number of times a route has been hit. For a detailed walkthrough of this example, see the article [Create a Serverless REST API](https://docs.pulumi.com/quickstart/aws-rest-api.html).

## Deploying and running the program

1. Create a new stack:

```bash
$ pulumi stack init count-api-testing
```

1. Set the AWS region:

```
$ pulumi config set aws:region us-west-2
```

1. Restore NPM modules via `npm install`.

1. Run `pulumi update` to preview and deploy changes:

```
$ pulumi update
Previewing update of stack 'count-api-testing'
...
Updating stack 'count-api-testing'
Performing changes:
Type Name Status Info
+ pulumi:pulumi:Stack cloud-js-httpendpoint-count-api-testing created
+ ├─ cloud:table:Table counterTable created
+ │ └─ aws:dynamodb:Table counterTable created
+ └─ cloud:http:HttpEndpoint hello-world created
+ ├─ cloud:function:Function hello-world4fcc7b60 created
+ │ └─ aws:serverless:Function hello-world4fcc7b60 created
+ │ ├─ aws:iam:Role hello-world4fcc7b60 created
+ │ ├─ aws:lambda:Function hello-world4fcc7b60 created
+ │ ├─ aws:iam:RolePolicyAttachment hello-world4fcc7b60-32be53a2 created
+ │ └─ aws:iam:RolePolicyAttachment hello-world4fcc7b60-fd1a00e5 created
+ ├─ aws:apigateway:RestApi hello-world created
+ ├─ aws:apigateway:Deployment hello-world created
+ ├─ aws:lambda:Permission hello-world-4fcc7b60 created
+ └─ aws:apigateway:Stage hello-world created
---outputs:---
endpoint: "https://k6z25g5lw6.execute-api.us-west-2.amazonaws.com/stage/"
info: 14 changes performed:
+ 14 resources created
Update duration: 48.510747688s
```

1. View the endpoint URL and curl a few routes:

```bash
$ pulumi stack output
Current stack outputs (1):
OUTPUT VALUE
url https://5e8xrktey3.execute-api.us-west-2.amazonaws.com/stage/

$ curl $(pulumi stack output url)/hello
{"route":"hello","count":1}
$ curl $(pulumi stack output url)/woohoo
{"route":"woohoo","count":1}
```

1. To view the runtime logs of the Lambda function, use the `pulumi logs` command. To get a log stream, use `pulumi logs --follow`.

## Clean up

1. Run `pulumi destroy` to tear down all resources.

1. To delete the stack itself, run `pulumi stack rm`. Note that this command deletes all deployment history from the Pulumi Console.
22 changes: 22 additions & 0 deletions cloud-js-httpendpoint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const cloud = require("@pulumi/cloud-aws");

// Create a mapping from 'route' to a count
let counterTable = new cloud.Table("counterTable", "route");

// Create an API endpoint
let endpoint = new cloud.HttpEndpoint("hello-world");

endpoint.get("/{route+}", async (req, res) => {
let route = req.params["route"];
console.log(`Getting count for '${route}'`);

// get previous value and increment
let value = await counterTable.get({route}); // reference outer `counterTable` object
let count = (value && value.count) || 0;
await counterTable.insert( { route, count: ++count });

res.status(200).json({ route, count});
console.log(`Got count ${count} for '${route}'`);
});

module.exports.endpoint = endpoint.publish().url;
8 changes: 8 additions & 0 deletions cloud-js-httpendpoint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "cloud-js-httpendpoint",
"main": "index.js",
"dependencies": {
"@pulumi/cloud-aws": "^0.12.0",
"@pulumi/cloud": "^0.12.0"
}
}
24 changes: 24 additions & 0 deletions cloud-js-httpendpoint/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"lib": [
"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"
]
}

0 comments on commit f012ad7

Please sign in to comment.