forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
converted aws-go-appsync from python to golang (pulumi#734)
* finished converting aws-go-appsync into golang * added test for appsync in golang * made code review changes with better variable names etc * completed unit test for output validation and made necessary modifications in main.go * added code review changes. used fmt for string concat and updated timeout to be shorter
- Loading branch information
1 parent
20b54cc
commit 06c69d9
Showing
8 changed files
with
654 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: aws-go-appsync | ||
runtime: go | ||
description: defining an AWS AppSync endpoint from Pulumi in Golang | ||
template: | ||
config: | ||
aws:region: | ||
description: The AWS region to deploy into | ||
default: us-west-2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new) | ||
|
||
# GraphQL Endpoint in AWS AppSync (in Go) | ||
|
||
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 the App | ||
|
||
To deploy your infrastructure, follow the below steps. | ||
|
||
### Prerequisites | ||
|
||
1. [Install Go](https://golang.org/doc/install) | ||
2. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/) | ||
3. [Configure AWS Credentials](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/) | ||
|
||
### Steps | ||
|
||
After cloning this repo, from this working directory, run these commands: | ||
|
||
1. Create a new Pulumi stack, which is an isolated deployment target for this example: | ||
|
||
```bash | ||
$ pulumi stack init dev | ||
``` | ||
|
||
2. Set the required configuration variables for this program (AWS Region): | ||
|
||
```bash | ||
$ pulumi config set aws:region us-west-2 | ||
``` | ||
|
||
3. Run `pulumi up` up to preview and deploy changes: | ||
```bash | ||
$ pulumi up | ||
Previewing update (dev): | ||
... | ||
Updating (dev): | ||
... | ||
Resources: | ||
+ 10 created | ||
Duration: 20s | ||
``` | ||
|
||
4. Check the deployed GraphQL endpoint: | ||
|
||
```bash | ||
$ pulumi stack output endpoint | ||
https://***.appsync-api.us-west-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. | ||
|
||
2. To delete the stack itself, run `pulumi stack rm`. Note that this command deletes all deployment history from the Pulumi Console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/pulumi/examples/aws-go-webserver | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/pulumi/pulumi-aws/sdk/v2 v2.10.1 | ||
github.com/pulumi/pulumi/sdk/v2 v2.5.0 | ||
github.com/pulumi/pulumi-random/sdk/v2 v2.1.3 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/pulumi/pulumi-aws/sdk/v2/go/aws/appsync" | ||
"github.com/pulumi/pulumi-aws/sdk/v2/go/aws/dynamodb" | ||
"github.com/pulumi/pulumi-aws/sdk/v2/go/aws/iam" | ||
"github.com/pulumi/pulumi-random/sdk/v2/go/random" | ||
"github.com/pulumi/pulumi/sdk/v2/go/pulumi" | ||
) | ||
|
||
func main() { | ||
pulumi.Run(func(ctx *pulumi.Context) error { | ||
|
||
// Create a Dynamo DB | ||
table, err := dynamodb.NewTable(ctx, "tenants", &dynamodb.TableArgs{ | ||
Attributes: dynamodb.TableAttributeArray{ | ||
&dynamodb.TableAttributeArgs{ | ||
Name: pulumi.String("id"), | ||
Type: pulumi.String("S"), | ||
}, | ||
}, | ||
HashKey: pulumi.String("id"), | ||
ReadCapacity: pulumi.Int(1), | ||
WriteCapacity: pulumi.Int(1), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// create IAM role and policy wiring | ||
assumeRolePolicyJSON, err := json.Marshal(map[string]interface{}{ | ||
"Version": "2012-10-17", | ||
"Statement": []interface{}{ | ||
map[string]interface{}{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": map[string]interface{}{ | ||
"Service": "appsync.amazonaws.com", | ||
}, | ||
"Effect": "Allow", | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
role, err := iam.NewRole(ctx, "iam-role", &iam.RoleArgs{ | ||
AssumeRolePolicy: pulumi.String(assumeRolePolicyJSON), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tempPolicy := table.Arn.ApplyT(func(arn string) (string, error) { | ||
policyJSON, err := json.Marshal(map[string]interface{}{ | ||
"Version": "2012-10-17", | ||
"Statement": []interface{}{ | ||
map[string]interface{}{ | ||
"Action": []string{ | ||
"dynamodb:PutItem", | ||
"dynamodb:GetItem", | ||
}, | ||
"Effect": "Allow", | ||
"Resource": []string{ | ||
arn, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(policyJSON), nil | ||
}) | ||
|
||
policy, err := iam.NewPolicy(ctx, "iam-policy", &iam.PolicyArgs{ | ||
Policy: tempPolicy, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = iam.NewRolePolicyAttachment(ctx, "iamPolicyAttachment", &iam.RolePolicyAttachmentArgs{ | ||
Role: role.Name, | ||
PolicyArn: policy.Arn, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create an API accessible with a key | ||
api, err := appsync.NewGraphQLApi(ctx, "key", &appsync.GraphQLApiArgs{ | ||
AuthenticationType: pulumi.String("API_KEY"), | ||
Schema: pulumi.String(` | ||
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 | ||
}`), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// key to access the API | ||
apiKey, err := appsync.NewApiKey(ctx, "key", &appsync.ApiKeyArgs{ | ||
ApiId: api.ID(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Generate random string | ||
randStr, err := random.NewRandomString(ctx, "random-datasource-name", &random.RandomStringArgs{ | ||
Length: pulumi.Int(15), | ||
Special: pulumi.BoolPtr(false), | ||
Number: pulumi.BoolPtr(false), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Link the data source to the Dynamo DB Table | ||
dataSource, err := appsync.NewDataSource(ctx, "tenants-DS", &appsync.DataSourceArgs{ | ||
Name: randStr.Result, | ||
ApiId: api.ID(), | ||
Type: pulumi.String("AMAZON_DYNAMODB"), | ||
DynamodbConfig: &appsync.DataSourceDynamodbConfigArgs{ | ||
TableName: table.Name, | ||
}, | ||
ServiceRoleArn: role.Arn, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Resolver for [getTenantById] query | ||
_, err = appsync.NewResolver(ctx, "getResolver", &appsync.ResolverArgs{ | ||
ApiId: api.ID(), | ||
Type: pulumi.String("Query"), | ||
DataSource: dataSource.Name, | ||
Field: pulumi.String("getTenantById"), | ||
RequestTemplate: pulumi.String(`{ | ||
"version": "2017-02-28", | ||
"operation": "GetItem", | ||
"key": { | ||
"id": $util.dynamodb.toDynamoDBJson($ctx.args.id), | ||
} | ||
}`), | ||
ResponseTemplate: pulumi.String(`$util.toJson($ctx.result)`), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Resolver for [addTenant] mutation | ||
_, err = appsync.NewResolver(ctx, "addResolver", &appsync.ResolverArgs{ | ||
ApiId: api.ID(), | ||
Type: pulumi.String("Mutation"), | ||
DataSource: dataSource.Name, | ||
Field: pulumi.String("addTenant"), | ||
RequestTemplate: pulumi.String(`{ | ||
"version" : "2017-02-28", | ||
"operation" : "PutItem", | ||
"key" : { | ||
"id" : $util.dynamodb.toDynamoDBJson($ctx.args.id) | ||
}, | ||
"attributeValues" : { | ||
"name": $util.dynamodb.toDynamoDBJson($ctx.args.name) | ||
} | ||
}`), | ||
ResponseTemplate: pulumi.String(`$util.toJson($ctx.result)`), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Export | ||
endptURL := api.Uris.MapIndex(pulumi.String("GRAPHQL")) // how to retrieve the value at index "GRAPHQL" | ||
|
||
ctx.Export("endpoint", endptURL) | ||
ctx.Export("key", apiKey.Key) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.