Skip to content

Commit

Permalink
Adding more Python examples (pulumi#402)
Browse files Browse the repository at this point in the history
* Adding the DigitalOcean Kubernets example in Python

* Adding the DigitalOcean Loadbalancer example in Python

* Enabling the new DigitalOcean python examples in the test run

* Adding gcp-py-serverless-raw example

* Enabling gcp-py-instance-nginx test

* Adding azure-py-vm-scaleset

* Adding azure-py-appservice

* Adding azure-py-appservice-docker

* Adding azure-py-hdinsight-spark

* Add azure-py-functions-raw

* Add azure-py-aks-multicluster

* Add azure-py-keyvault-rbac

* Fixing up the azure-py-aks test to not continually use the same name

* Adding customTimeouts to aws-ts-pulumi-miniflux as destroy times out

* Adding aws-py-resources

* Add aws-py-appsync

* Add aws-py-stackreference

* Review changes
  • Loading branch information
stack72 committed Sep 27, 2019
1 parent f02e333 commit 0ae4ddb
Show file tree
Hide file tree
Showing 90 changed files with 2,731 additions and 83 deletions.
2 changes: 2 additions & 0 deletions aws-py-appsync/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
venv/
8 changes: 8 additions & 0 deletions aws-py-appsync/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: aws-py-appsync
runtime: python
description: Basic example of defining an AWS AppSync endpoint from Pulumi in Python
template:
config:
aws:region:
description: The AWS region to deploy into
default: us-east-2
67 changes: 67 additions & 0 deletions aws-py-appsync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# GraphQL Endpoint in AWS AppSync

This example shows how to setup a basic GraphQL endpoint in AWS AppSync. The endpoint contains one query and one mutation that get and put items to a Dynamo DB table.

## Deploying and running the Pulumi App

1. Create a new stack:

```bash
$ pulumi stack init dev
```

1. Set the AWS region:

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

1. Create a Python virtualenv, activate it, and install dependencies:

This installs the dependent packages [needed](https://www.pulumi.com/docs/intro/concepts/how-pulumi-works/) for our Pulumi program.

```
$ virtualenv -p python3 venv
$ source venv/bin/activate
$ pip3 install -r requirements.txt
```

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

```
$ pulumi up
Previewing update (dev):
...
Updating (dev):
...
Resources:
+ 10 created
Duration: 20s
```

1. Check the deployed GraphQL endpoint:

```
$ pulumi stack output endpoint
https://***.appsync-api.us-east-2.amazonaws.com/graphql
$ pulumi stack output key
***sensitivekey***
$ curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:$(pulumi stack output key)" -d '{ "query": "mutation AddTenant { addTenant(id: \"123\", name: \"FirstCorp\") { id name } }" }' "$(pulumi stack output endpoint)"
{
"data": {
"addTenant": {
"id": "123",
"name": "FirstCorp"
}
}
}
```

## 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.
131 changes: 131 additions & 0 deletions aws-py-appsync/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import json
from pulumi import export
from pulumi_aws import dynamodb, iam, appsync

## Dynamo DB table to hold data for the GraphQL endpoint
table = dynamodb.Table(
"tenants",
name="Tenant",
hash_key="id",
attributes=[{
"name": "id",
"type": "S"
}],
read_capacity=1,
write_capacity=1)

## Create IAM role and policy wiring
role = iam.Role(
"iam-role",
assume_role_policy=json.dumps({
"Version": "2012-10-17",
"Statement": [{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "appsync.amazonaws.com"
},
"Effect": "Allow",
}]
}))

policy = iam.Policy(
"iam-policy",
policy=table.arn.apply(lambda arn: json.dumps({
"Version": "2012-10-17",
"Statement": [{
"Action": [
"dynamodb:PutItem",
"dynamodb:GetItem"
],
"Effect": "Allow",
"Resource": [arn]
}]
})))

attachment = iam.RolePolicyAttachment(
"iam-policy-attachment",
role=role,
policy_arn=policy.arn)

## GraphQL Schema
schema = """
type Query {
getTenantById(id: ID!): Tenant
}
type Mutation {
addTenant(id: ID!, name: String!): Tenant!
}
type Tenant {
id: ID!
name: String
}
schema {
query: Query
mutation: Mutation
}
"""

## Create API accessible with a key
api = appsync.GraphQLApi(
"key",
authentication_type="API_KEY",
schema=schema
)

api_key = appsync.ApiKey(
"key",
api_id=api.id)

## Link a data source to the Dynamo DB Table
data_source = appsync.DataSource(
"tenants-ds",
name="TenantsDataSource",
api_id=api.id,
type="AMAZON_DYNAMODB",
dynamodb_config={
"table_name": table.name
},
service_role_arn=role.arn)

## A resolver for the [getTenantById] query
get_resolver = appsync.Resolver(
"get-resolver",
api_id=api.id,
data_source=data_source.name,
type="Query",
field="getTenantById",
request_template="""{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($ctx.args.id),
}
}
""",
response_template="$util.toJson($ctx.result)")

## A resolver for the [addTenant] mutation
add_resolver = appsync.Resolver(
"add-resolver",
api_id=api.id,
data_source=data_source.name,
type="Mutation",
field="addTenant",
request_template="""{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id" : $util.dynamodb.toDynamoDBJson($ctx.args.id)
},
"attributeValues" : {
"name": $util.dynamodb.toDynamoDBJson($ctx.args.name)
}
}
""",
response_template="$util.toJson($ctx.result)")

export("endpoint", api.uris["GRAPHQL"])
export("key", api_key.key)
2 changes: 2 additions & 0 deletions aws-py-appsync/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi>=1.0.0
pulumi-aws>=1.0.0
2 changes: 2 additions & 0 deletions aws-py-resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
venv/
8 changes: 8 additions & 0 deletions aws-py-resources/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: aws-py-resources
description: A Pulumi program that demonstrates creating various AWS resources in PYthon
runtime: python
template:
config:
aws:region:
description: The AWS region to deploy into
default: us-east-2
23 changes: 23 additions & 0 deletions aws-py-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# AWS Resources

A Pulumi program that demonstrates creating various AWS resources in Python

```bash
# Create and configure a new stack
$ pulumi stack init dev
$ pulumi config set aws:region us-east-2

# Install dependencies
$ virtualenv -p python3 venv
$ source venv/bin/activate
$ pip3 install -r requirements.txt

# Preview and run the deployment
$ pulumi up

# Remove the app
$ pulumi destroy
$ pulumi stack rm
```
Loading

0 comments on commit 0ae4ddb

Please sign in to comment.