Skip to content

Commit

Permalink
initial port of ts simple http api to py
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitesh Boinpally authored and Hitesh Boinpally committed Mar 25, 2021
1 parent 9eceed4 commit 139a088
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws-py-apigatewayv2-http-api-quickcreate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
venv/
6 changes: 6 additions & 0 deletions aws-py-apigatewayv2-http-api-quickcreate/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: aws-py-apigatewayv2-http-api-quickstart
runtime:
name: python
options:
virtualenv: venv
description: Quickstart example of using AWS API Gateway v2 HTTP API
76 changes: 76 additions & 0 deletions aws-py-apigatewayv2-http-api-quickcreate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[![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. The API executes a simple lambda function found
in `/app/index.js`.

## Prerequisites
1. Install [Pulumi](https://www.pulumi.com/docs/get-started/install/).
2. Configure [Pulumi for AWS](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/).
3. Install [Python 3.6](https://www.python.org/downloads/) or higher.

## 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-py-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.
48 changes: 48 additions & 0 deletions aws-py-apigatewayv2-http-api-quickcreate/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2016-2021, Pulumi Corporation. All rights reserved

import pulumi
import json
import pulumi_aws as aws

# Create the role for the Lambda to assume
lambda_role = aws.iam.Role("lambdaRole",
assume_role_policy=json.dumps({
"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
role_policy_attachment = aws.iam.RolePolicyAttachment("lambdaRoleAttachment",
role=lambda_role,
policy_arn=aws.iam.ManagedPolicy.AWS_LAMBDA_BASIC_EXECUTION_ROLE)

# Create the lambda to execute
lambda_function = aws.lambda_.Function("lambdaFunction",
code=pulumi.AssetArchive({
".": pulumi.FileArchive("./app"),
}),
runtime="nodejs12.x",
role=lambda_role.arn,
handler="index.handler")

# Give API Gateway permissions to invoke the Lambda
lambda_permission = aws.lambda_.Permission("lambdaPermission",
action="lambda:InvokeFunction",
principal="apigateway.amazonaws.com",
function=lambda_function)

# Set up the API Gateway
apigw = aws.apigatewayv2.Api("httpApiGateway",
protocol_type="HTTP",
route_key="GET /",
target=lambda_function.invoke_arn)

# Export the API endpoint for easy access
pulumi.export("endpoint", apigw.api_endpoint)
7 changes: 7 additions & 0 deletions aws-py-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!"
};
}
2 changes: 2 additions & 0 deletions aws-py-apigatewayv2-http-api-quickcreate/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi>=2.0.0,<3.0.0
pulumi-aws>=3.2.0,<4.0.0

0 comments on commit 139a088

Please sign in to comment.