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.
Add simple HTTP endpoint sample (pulumi#50)
- Loading branch information
1 parent
a876e35
commit f012ad7
Showing
6 changed files
with
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules/ | ||
/.pulumi/ |
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,3 @@ | ||
name: cloud-js-httpendpoint | ||
description: A simple HTTP endpoint that returns the number of times a route has been hit. | ||
runtime: nodejs |
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,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. |
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,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; |
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": "cloud-js-httpendpoint", | ||
"main": "index.js", | ||
"dependencies": { | ||
"@pulumi/cloud-aws": "^0.12.0", | ||
"@pulumi/cloud": "^0.12.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,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" | ||
] | ||
} |