Hello everyone! I want to share my experience of exploring AWS CDK. I want to deploy AWS AppSync and Amazon DynamoDB. Let's learn with me.
Background
I used Terraform for provisioning the resources into Heroku. Heroku announced that they will stop the free plan, so I want to move my resources into AWS. I learned AWS SDK 3 months ago, but got stuck. I hope this post will help you.
Our schema only has simple CRUD and store experience data. The experience data has id and title only. You can extend more columns on it, but take note you will need to update the CDK code too.
My CDK stacks look like this:
usingAmazon.CDK;usingAmazon.CDK.AWS.AppSync.Alpha;usingAmazon.CDK.AWS.DynamoDB;usingAmazon.CDK.AWS.IAM;usingConstructs;namespaceMyPersonalWebApiInfra{publicclassMyPersonalWebApiInfraStack:Stack{internalMyPersonalWebApiInfraStack(Constructscope,stringid,IStackPropsprops=null):base(scope,id,props){// AppSyncvarapi=newGraphqlApi(this,"api",newGraphqlApiProps{Name="berv-api",Schema=Schema.FromAsset("schema.graphql"),XrayEnabled=true,});// Dynamo DBvarexperienceTable=newTable(this,"berv-experiences",newTableProps{BillingMode=BillingMode.PAY_PER_REQUEST,PartitionKey=newAttribute{Name="id",Type=AttributeType.STRING},});// IAM Rolevarrole=newRole(this,"Role",newRoleProps{AssumedBy=newServicePrincipal("lambda.amazonaws.com")});// AppSync apply IAM to some mutationsapi.GrantMutation(role,"addExperience");api.GrantMutation(role,"updateExperience");api.GrantMutation(role,"deleteExperience");// Add Dynamo DB as DataSource of AppSyncvardataSource=api.AddDynamoDbDataSource("experience",experienceTable);// Add Resolver for Get All ExperiencesdataSource.CreateResolver(newBaseResolverProps{TypeName="Query",FieldName="getExperiences",RequestMappingTemplate=MappingTemplate.DynamoDbScanTable(),ResponseMappingTemplate=MappingTemplate.DynamoDbResultList(),});// Add Resolver for Get Experience by IddataSource.CreateResolver(newBaseResolverProps{TypeName="Query",FieldName="getExperienceById",RequestMappingTemplate=MappingTemplate.DynamoDbGetItem("id","experienceId"),ResponseMappingTemplate=MappingTemplate.DynamoDbResultItem(),});// Add Resolver for Create ExperiencedataSource.CreateResolver(newBaseResolverProps{TypeName="Mutation",FieldName="addExperience",RequestMappingTemplate=MappingTemplate.DynamoDbPutItem(PrimaryKey.Partition("id").Auto(),Values.Projecting("input")),ResponseMappingTemplate=MappingTemplate.DynamoDbResultItem()});// Add Resolver for Update an ExperiencedataSource.CreateResolver(newBaseResolverProps{TypeName="Mutation",FieldName="updateExperience",RequestMappingTemplate=MappingTemplate.FromString("""
{"version":"2017-02-28","operation":"PutItem","key":{"id":$util.dynamodb.toDynamoDBJson($ctx.args.input.id),},"attributeValues":{"title":$util.dynamodb.toDynamoDBJson($ctx.args.input.title)}}"""),
ResponseMappingTemplate=MappingTemplate.DynamoDbResultItem()});// Add Resolver for Delete an ExperiencedataSource.CreateResolver(newBaseResolverProps{TypeName="Mutation",FieldName="deleteExperience",RequestMappingTemplate=MappingTemplate.DynamoDbDeleteItem("id","experienceId"),ResponseMappingTemplate=MappingTemplate.FromString("$util.toJson($ctx.result)")});}}}
I didn't have enough information on the best way to map the Update Request. Please feel free to give feedback about it. Anyway, visit this repository for the complete project.