Skip to content

Commit

Permalink
Added quickstart version of apigatewayv2 http api (pulumi#862)
Browse files Browse the repository at this point in the history
* Added quickstart version of apigatewayv2 http api

* Fixing lint errors

* Fixed linting

* Moved folder to be more descriptive

* Renamed pulumi project

* Update index.ts

* Update README.md

* Update README.md

* Updated README

* Fixing linting
  • Loading branch information
pierskarsenbarg committed Jan 7, 2021
1 parent 695da88 commit 1990689
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Example | Description |
--------- | --------- |
[API Gateway](aws-ts-apigateway) | Deploy a simple REST API that counts the number of times a route has been hit.
[API Gateway HTTP API with routes](aws-ts-apigatewayv2-http-api) | Deploy a HTTP API that invokes a lambda.
[API Gateway HTTP API quickstart](aws-ts-apigatewayv2-http-api-quickstart) | Deploy a very simple HTTP API that invokes a lambda.
[Apigateway - Auth0](aws-ts-apigateway-auth0) | Deploy a simple REST API protected by Auth0.
[AppSync](aws-ts-appsync) | Deploy a basic GraphQL endpoint in AWS AppSync.
[AssumeRole](aws-ts-assume-role) | Use AssumeRole to create resources.
Expand Down
2 changes: 2 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/node_modules/
3 changes: 3 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: aws-ts-apigatewayv2-http-api-quickstart
runtime: nodejs
description: Quickstart example of using AWS API Gateway v2 HTTP API
70 changes: 70 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# AWS API Gateway V2 HTTP API Quickstart

Set up a simple HTTP API using AWS API Gateway V2

## Deploying and running the program

Note: some values in this example will be different from run to run. These values are indicated
with `***`.

1. Create a new stack:

```bash
$ pulumi stack init http-api
```

1. Set the AWS region:

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

1. Restore NPM modules via `npm install` or `yarn install`.

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

```
$ pulumi up
Previewing update (http-api)
...

Updating (http-api)

Type Name Status
+ pulumi:pulumi:Stack aws-ts-apigatewayv2-quickstart-http-api created
+ ├─ aws:iam:Role lambdaRole created
+ ├─ aws:lambda:Function lambdaFunction created
+ ├─ aws:iam:RolePolicyAttachment lambdaRoleAttachment created
+ ├─ aws:apigatewayv2:Api httpApiGateway created
+ └─ aws:lambda:Permission lambdapermission created

Outputs:
endpoint: "https://****.execute-api.us-east-2.amazonaws.com"

Resources:
+ 6 created

Duration: 22s
```

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

```bash
$ pulumi stack output
Current stack outputs (1):
OUTPUT VALUE
endpoint https://***.execute-api.us-east-2.amazonaws.com
$ curl $(pulumi stack output endpoint)
Hello, Pulumi!
```

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.
7 changes: 7 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.handler = async function(event, context) {
console.log("EVENT: \n" + JSON.stringify(event, null, 2))
return {
statusCode: 200,
body: "Hello, Pulumi!"
};
}
53 changes: 53 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.

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

// Create the role for the Lambda to assume
const lambdaRole = new aws.iam.Role("lambdaRole", {
assumeRolePolicy: {
Version: "2012-10-17",
Statement: [
{
Action: "sts:AssumeRole",
Principal: {
Service: "lambda.amazonaws.com",
},
Effect: "Allow",
Sid: "",
},
],
},
});

// Attach the fullaccess policy to the Lambda role created above
const rolepolicyattachment = new aws.iam.RolePolicyAttachment("lambdaRoleAttachment", {
role: lambdaRole,
policyArn: aws.iam.ManagedPolicies.AWSLambdaFullAccess,
});

// Create the Lambda to execute
const lambda = new aws.lambda.Function("lambdaFunction", {
code: new pulumi.asset.AssetArchive({
".": new pulumi.asset.FileArchive("./app"),
}),
runtime: "nodejs12.x",
role: lambdaRole.arn,
handler: "index.handler",
});

// Give API Gateway permissions to invoke the Lambda
const lambdapermission = new aws.lambda.Permission("lambdaPermission", {
action: "lambda:InvokeFunction",
principal: "apigateway.amazonaws.com",
function: lambda,
});

// Set up the API Gateway
const apigw = new aws.apigatewayv2.Api("httpApiGateway", {
protocolType: "HTTP",
routeKey: "GET /",
target: lambda.invokeArn,
});

export const endpoint = apigw.apiEndpoint;
11 changes: 11 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "aws-ts-apigatewayv2-quickstart",
"devDependencies": {
"@types/node": "^10.0.0"
},
"dependencies": {
"@pulumi/aws": "^3.0.0",
"@pulumi/awsx": "^0.22.0",
"@pulumi/pulumi": "^2.0.0"
}
}
18 changes: 18 additions & 0 deletions aws-ts-apigatewayv2-http-api-quickcreate/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"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 1990689

Please sign in to comment.