A more complete API Gateway HTTP API
MODIFY
README.md
README.md
@@ -68,6 +68,7 @@ $ git pull origin master
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.
[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.

ADD
aws-ts-apigatewayv2-http-api/.gitignore
+/bin/
+/node_modules/

ADD
aws-ts-apigatewayv2-http-api/Pulumi.yaml
+name: aws-ts-apigatewayv2-http-api
+runtime: nodejs
+description: Example of using AWS API Gateway v2 HTTP API

ADD
aws-ts-apigatewayv2-http-api/app/index.js
+exports.handler = async function(event, context) {
+ console.log("EVENT: \n" + JSON.stringify(event, null, 2))
+ return {
+ statusCode: 200,
+ body: "Hello, Pulumi!"
+ };
+ }
\ No newline at end of file

ADD
aws-ts-apigatewayv2-http-api/index.ts
+// Copyright 2016-2019, Pulumi Corporation.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import * as aws from "@pulumi/aws";
+import * as pulumi from "@pulumi/pulumi";
+
+const stack = pulumi.getStack();
+
+const lambdaRole = new aws.iam.Role("lambdaRole", {
+ assumeRolePolicy: {
+ Version: "2012-10-17",
+ Statement: [
+ {
+ Action: "sts:AssumeRole",
+ Principal: {
+ Service: "lambda.amazonaws.com",
+ },
+ Effect: "Allow",
+ Sid: "",
+ },
+ ],
+ },
+});
+
+const lambadRoleAttachment = new aws.iam.RolePolicyAttachment("lambdaRoleAttachment", {
+ role: lambdaRole,
+ policyArn: aws.iam.ManagedPolicies.AWSLambdaFullAccess,
+});
+
+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",
+});
+
+const apigw = new aws.apigatewayv2.Api("httpApiGateway", {
+ protocolType: "HTTP",
+});
+
+const lambdaPermission = new aws.lambda.Permission("lambdaPermission", {
+ action: "lambda:InvokeFunction",
+ principal: "apigateway.amazonaws.com",
+ function: lambda,
+ sourceArn: pulumi.interpolate`${apigw.executionArn}/*/*`,
+}, {dependsOn: [apigw, lambda]});
+
+const integration = new aws.apigatewayv2.Integration("lambdaIntegration", {
+ apiId: apigw.id,
+ integrationType: "AWS_PROXY",
+ integrationUri: lambda.arn,
+ integrationMethod: "POST",
+ payloadFormatVersion: "2.0",
+ passthroughBehavior: "WHEN_NO_MATCH",
+});
+
+const route = new aws.apigatewayv2.Route("apiRoute", {
+ apiId: apigw.id,
+ routeKey: "$default",
+ target: pulumi.interpolate`integrations/${integration.id}`,
+});
+
+const stage = new aws.apigatewayv2.Stage("apiStage", {
+ apiId: apigw.id,
+ name: stack,
+ routeSettings: [
+ {
+ routeKey: route.routeKey,
+ throttlingBurstLimit: 5000,
+ throttlingRateLimit: 10000,
+ },
+ ],
+ autoDeploy: true,
+}, {dependsOn: [route]});
+
+export const endpoint = pulumi.interpolate`${apigw.apiEndpoint}/${stage.name}`;

ADD
aws-ts-apigatewayv2-http-api/package.json
+{
+ "name": "aws-ts-apigatewayv2-http-api",
+ "devDependencies": {
+ "@types/node": "^10.0.0"
+ },
+ "dependencies": {
+ "@pulumi/aws": "^3.0.0",
+ "@pulumi/awsx": "^0.22.0",
+ "@pulumi/pulumi": "^2.0.0"
+ }
+}

ADD
aws-ts-apigatewayv2-http-api/tsconfig.json
+{
+ "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"
+ ]
+}

A more complete API Gateway HTTP API (#863)