forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
138 lines (123 loc) · 3.01 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import pulumi_random as random
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",
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)
random_string = random.RandomString(
"random-datasource-name",
length=15,
special="false",
number="false",
)
## Link a data source to the Dynamo DB Table
data_source = appsync.DataSource(
"tenants-ds",
name=random_string.result,
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)