Deploy an AppSync GraphQL API with DynamoDB backend (Part 1)
2020年05月09日
# cd
# exit
$ sudo npm install -g aws-cdk
If encounter below error, try running this command again as root/Administrator.
$ git clone https://github.com/aws-samples/aws-cdk-examples.git
$ cd aws-cdk-examples/python/appsync-graphql-dynamodb/
$ python3.8 -m venv .env
$ source .env/bin/activate
$ pip install -r requirements.txt
Fix bug of the file app_sync_cdk_stack.py around line 90:
$ vim app_sync_cdk/app_sync_cdk_stack.py
Deploy / redeploy your Stack to your AWS Account.
Deploys the CloudFormation template
$ cdk deploy
If encounter below error, make sure the Node.js version is >= 10.3.0. In some cases, this may be led by using "amazon-linux-extras install epel" and "yum install npm" commands to install npm on Amazon Linux 2 AMI.
If encounter below error, make sure the file is corrected.
Add a mutation:
To destroy the resources:
$ cdk destroy
References
AppSync GraphQL API Acting on DynamoDB
-
Image: Amazon Linux 2
Install Python3
$ sudo yum install -y git
$ curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
$ sudo yum install -y nodejs
$ aws configure
AWS Access Key ID [None]: AWS Secret Access Key [None]: Default region name [None]: us-west-2 Default output format [None]:
# cd
# exit
$ sudo npm install -g aws-cdk
/usr/bin/cdk -> /usr/lib/node_modules/aws-cdk/bin/cdk + aws-cdk@1.38.0 added 213 packages from 187 contributors in 8.348sPS: "npm i" is an alias for "npm install".
If encounter below error, try running this command again as root/Administrator.
npm ERR! Linux 4.14.173-137.229.amzn2.x86_64 npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "aws-cdk" npm ERR! node v6.17.1 npm ERR! npm v3.10.10 npm ERR! path /usr/lib/node_modules npm ERR! code EACCES npm ERR! errno -13 npm ERR! syscall access npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules' npm ERR! at Error (native) npm ERR! { Error: EACCES: permission denied, access '/usr/lib/node_modules' npm ERR! at Error (native) npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'access', npm ERR! path: '/usr/lib/node_modules' } npm ERR! npm ERR! Please try running this command again as root/Administrator. npm ERR! Please include the following file with any support request: npm ERR! /home/ec2-user/npm-debug.log
$ git clone https://github.com/aws-samples/aws-cdk-examples.git
$ cd aws-cdk-examples/python/appsync-graphql-dynamodb/
$ python3.8 -m venv .env
$ source .env/bin/activate
$ pip install -r requirements.txt
Fix bug of the file app_sync_cdk_stack.py around line 90:
$ vim app_sync_cdk/app_sync_cdk_stack.py
... items_table_role.add_managed_policy( ManagedPolicy.from_aws_managed_policy_name( 'AmazonDynamoDBFullAccess' ) )
Deploy / redeploy your Stack to your AWS Account.
Deploys the CloudFormation template
$ cdk deploy
This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening). Please confirm you intend to make the following modifications: IAM Statement Changes ┌───┬──────────────────────────┬────────┬────────────────┬───────────────────────────────┬───────────┐ │ │ Resource │ Effect │ Action │ Principal │ Condition │ ├───┼──────────────────────────┼────────┼────────────────┼───────────────────────────────┼───────────┤ │ + │ ${ItemsDynamoDBRole.Arn} │ Allow │ sts:AssumeRole │ Service:appsync.amazonaws.com │ │ └───┴──────────────────────────┴────────┴────────────────┴───────────────────────────────┴───────────┘ IAM Policy Changes ┌───┬──────────────────────┬────────────────────────────────────────────────────────────────┐ │ │ Resource │ Managed Policy ARN │ ├───┼──────────────────────┼────────────────────────────────────────────────────────────────┤ │ + │ ${ItemsDynamoDBRole} │ arn:${AWS::Partition}:iam::aws:policy/AmazonDynamoDBFullAccess │ └───┴──────────────────────┴────────────────────────────────────────────────────────────────┘ (NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299) Do you wish to deploy these changes (y/n)? y AppSyncGraphQLDynamoDBExample: deploying... AppSyncGraphQLDynamoDBExample: creating CloudFormation changeset... 0/12 | 4:54:43 AM | CREATE_IN_PROGRESS | AWS::AppSync::GraphQLApi | ItemsApi 0/12 | 4:54:43 AM | CREATE_IN_PROGRESS | AWS::CDK::Metadata | CDKMetadata 0/12 | 4:54:43 AM | CREATE_IN_PROGRESS | AWS::DynamoDB::Table | ItemsTable (ItemsTable5AAC2C46) 0/12 | 4:54:43 AM | CREATE_IN_PROGRESS | AWS::IAM::Role | ItemsDynamoDBRole (ItemsDynamoDBRole7D2E3F6D) 0/12 | 4:54:44 AM | CREATE_IN_PROGRESS | AWS::DynamoDB::Table | ItemsTable (ItemsTable5AAC2C46) Resource creation Initiated 0/12 | 4:54:44 AM | CREATE_IN_PROGRESS | AWS::IAM::Role | ItemsDynamoDBRole (ItemsDynamoDBRole7D2E3F6D) Resource creation Initiated 0/12 | 4:54:45 AM | CREATE_IN_PROGRESS | AWS::CDK::Metadata | CDKMetadata Resource creation Initiated 1/12 | 4:54:45 AM | CREATE_COMPLETE | AWS::CDK::Metadata | CDKMetadata 1/12 | 4:54:47 AM | CREATE_IN_PROGRESS | AWS::AppSync::GraphQLApi | ItemsApi Resource creation Initiated 2/12 | 4:54:47 AM | CREATE_COMPLETE | AWS::AppSync::GraphQLApi | ItemsApi 2/12 | 4:54:49 AM | CREATE_IN_PROGRESS | AWS::AppSync::ApiKey | ItemsApiKey 2/12 | 4:54:50 AM | CREATE_IN_PROGRESS | AWS::AppSync::GraphQLSchema | ItemsSchema 2/12 | 4:54:51 AM | CREATE_IN_PROGRESS | AWS::AppSync::GraphQLSchema | ItemsSchema Resource creation Initiated 3/12 | 4:54:58 AM | CREATE_COMPLETE | AWS::IAM::Role | ItemsDynamoDBRole (ItemsDynamoDBRole7D2E3F6D) 3/12 | 4:55:04 AM | CREATE_IN_PROGRESS | AWS::AppSync::ApiKey | ItemsApiKey Resource creation Initiated 4/12 | 4:55:04 AM | CREATE_COMPLETE | AWS::AppSync::ApiKey | ItemsApiKey 5/12 | 4:55:14 AM | CREATE_COMPLETE | AWS::DynamoDB::Table | ItemsTable (ItemsTable5AAC2C46) 5/12 | 4:55:17 AM | CREATE_IN_PROGRESS | AWS::AppSync::DataSource | ItemsDataSource 5/12 | 4:55:18 AM | CREATE_IN_PROGRESS | AWS::AppSync::DataSource | ItemsDataSource Resource creation Initiated 6/12 | 4:55:19 AM | CREATE_COMPLETE | AWS::AppSync::DataSource | ItemsDataSource 6/12 Currently in progress: ItemsSchema 7/12 | 4:55:53 AM | CREATE_COMPLETE | AWS::AppSync::GraphQLSchema | ItemsSchema 7/12 | 4:55:55 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | GetOneQueryResolver 7/12 | 4:55:55 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | GetAllQueryResolver 7/12 | 4:55:55 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | DeleteMutationResolver 7/12 | 4:55:56 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | SaveMutationResolver 7/12 | 4:55:57 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | GetAllQueryResolver Resource creation Initiated 8/12 | 4:55:57 AM | CREATE_COMPLETE | AWS::AppSync::Resolver | GetAllQueryResolver 8/12 | 4:55:58 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | SaveMutationResolver Resource creation Initiated 9/12 | 4:55:58 AM | CREATE_COMPLETE | AWS::AppSync::Resolver | SaveMutationResolver 9/12 | 4:55:58 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | GetOneQueryResolver Resource creation Initiated 9/12 | 4:55:58 AM | CREATE_IN_PROGRESS | AWS::AppSync::Resolver | DeleteMutationResolver Resource creation Initiated 10/12 | 4:55:58 AM | CREATE_COMPLETE | AWS::AppSync::Resolver | GetOneQueryResolver 11/12 | 4:55:58 AM | CREATE_COMPLETE | AWS::AppSync::Resolver | DeleteMutationResolver 12/12 | 4:56:00 AM | CREATE_COMPLETE | AWS::CloudFormation::Stack | AppSyncGraphQLDynamoDBExample ✅ AppSyncGraphQLDynamoDBExample Stack ARN: arn:aws:cloudformation:us-west-2:123456789012:stack/AppSyncGraphQLDynamoDBExample/***
If encounter below error, make sure the Node.js version is >= 10.3.0. In some cases, this may be led by using "amazon-linux-extras install epel" and "yum install npm" commands to install npm on Amazon Linux 2 AMI.
/usr/lib/node_modules/aws-cdk/bin/cdk.js:22 async function parseCommandLineArguments() { ^^^^^^^^ SyntaxError: Unexpected token function at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.require (module.js:504:17) at require (internal/module.js:20:19) at Object.<anonymous> (/usr/lib/node_modules/aws-cdk/bin/cdk:2:1)
If encounter below error, make sure the file is corrected.
1/12 | 4:25:39 AM | CREATE_FAILED | AWS::IAM::Role | ItemsDynamoDBRole (ItemsDynamoDBRole7D2E3F6D) ARN arn:aws:iam::aws:policy/arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess is not valid. (Service: AmazonIdentityManagement; Status Code: 400; Error Code: InvalidInput; Request ID: ***) new Role (/tmp/jsii-kernel-uwXSzv/node_modules/@aws-cdk/aws-iam/lib/role.js:41:22) \_ /home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:49 \_ Kernel._wrapSandboxCode (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8313:20) \_ Kernel._create (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:26) \_ Kernel.create (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7600:21) \_ KernelHost.processRequest (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7388:28) \_ KernelHost.run (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7328:14) \_ Immediate._onImmediate (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7331:37) \_ processImmediate (internal/timers.js:456:21) 2/12 | 4:25:40 AM | CREATE_FAILED | AWS::DynamoDB::Table | ItemsTable (ItemsTable5AAC2C46) Resource creation cancelled new Table (/tmp/jsii-kernel-uwXSzv/node_modules/@aws-cdk/aws-dynamodb/lib/table.js:230:22) \_ /home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:49 \_ Kernel._wrapSandboxCode (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8313:20) \_ Kernel._create (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:26) \_ Kernel.create (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7600:21) \_ KernelHost.processRequest (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7388:28) \_ KernelHost.run (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7328:14) \_ Immediate._onImmediate (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7331:37) \_ processImmediate (internal/timers.js:456:21) 3/12 | 4:25:41 AM | CREATE_FAILED | AWS::CDK::Metadata | CDKMetadata Resource creation cancelled 4/12 | 4:25:45 AM | CREATE_FAILED | AWS::AppSync::GraphQLApi | ItemsApi Resource creation cancelled /home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:49 \_ Kernel._wrapSandboxCode (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8313:20) \_ Kernel._create (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7853:26) \_ Kernel.create (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7600:21) \_ KernelHost.processRequest (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7388:28) \_ KernelHost.run (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7328:14) \_ Immediate._onImmediate (/home/ec2-user/aws-cdk-examples/python/appsync-graphql-dynamodb/.env/lib/python3.8/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7331:37) \_ processImmediate (internal/timers.js:456:21) 4/12 | 4:25:46 AM | ROLLBACK_IN_PROGRESS | AWS::CloudFormation::Stack | AppSyncGraphQLDynamoDBExample The following resource(s) failed to create: [ItemsDynamoDBRole7D2E3F6D, ItemsApi, ItemsTable5AAC2C46, CDKMetadata]. . Rollback requested by user. 4/12 | 4:25:49 AM | DELETE_IN_PROGRESS | AWS::CDK::Metadata | CDKMetadata 4/12 | 4:25:49 AM | DELETE_IN_PROGRESS | AWS::AppSync::GraphQLApi | ItemsApi 4/12 | 4:25:49 AM | DELETE_IN_PROGRESS | AWS::IAM::Role | ItemsDynamoDBRole (ItemsDynamoDBRole7D2E3F6D) 5/12 | 4:25:51 AM | DELETE_COMPLETE | AWS::IAM::Role | ItemsDynamoDBRole (ItemsDynamoDBRole7D2E3F6D) 6/12 | 4:25:51 AM | DELETE_COMPLETE | AWS::AppSync::GraphQLApi | ItemsApi 7/12 | 4:25:51 AM | DELETE_COMPLETE | AWS::CDK::Metadata | CDKMetadata 7/12 | 4:25:54 AM | DELETE_IN_PROGRESS | AWS::DynamoDB::Table | ItemsTable (ItemsTable5AAC2C46) 7/12 Currently in progress: AppSyncGraphQLDynamoDBExample, ItemsTable5AAC2C46 ❌ AppSyncGraphQLDynamoDBExample failed: Error: The stack named AppSyncGraphQLDynamoDBExample failed creation, it may need to be manually deleted from the AWS console: ROLLBACK_COMPLETE at /usr/lib/node_modules/aws-cdk/lib/api/util/cloudformation.ts:245:13 at processTicksAndRejections (internal/process/task_queues.js:97:5) at waitFor (/usr/lib/node_modules/aws-cdk/lib/api/util/cloudformation.ts:157:20) at Object.deployStack (/usr/lib/node_modules/aws-cdk/lib/api/deploy-stack.ts:233:26) at CdkToolkit.deploy (/usr/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:181:24) at main (/usr/lib/node_modules/aws-cdk/bin/cdk.ts:235:16) at initCommandLine (/usr/lib/node_modules/aws-cdk/bin/cdk.ts:169:9) The stack named AppSyncGraphQLDynamoDBExample failed creation, it may need to be manually deleted from the AWS console: ROLLBACK_COMPLETE
Add a mutation:
mutation save { save(name: "123") { itemsId name } }
To destroy the resources:
$ cdk destroy
References
AppSync GraphQL API Acting on DynamoDB
-