An example pipeline for deploying a Merged API using AWS CDK and AWS CodePipeline
This project includes 3 source AppSync APIs with resolvers written in Typescript:
- Reviews Service
- Books Service
- Authors Service
Also, it contains a Merged API which is able to integrate these 3 services into a single endpoint. This is known as the Book Reviews Merged API following the same example as the previous blog (https://aws.amazon.com/blogs/mobile/introducing-merged-apis-on-aws-appsync/).
- While this sample configures the stacks in the same repository for simplicity, each API has its own CodePipeline for deployment as these will be managed by separate teams. Each CodePipline has a beta stage, which is recommended for initial integration tests and staging, as well as a production stage.
- For this sample, all APIs are deployed within the same AWS account.
- Each source API stack includes a special SourceApiAssociation construct which handles configuring the association to the corresponding Merged API in the corresponding pipeline stage.
- The construct includes a CustomResource backed by a Lambda function which will propagate changes to the corresponding Merged API whenever the source API stack is updated and verify that the merge is successful. If the merge operation fails, the CustomResource notifies Cloudformation of the failure causing a Rollback and the CodePipeline will halt.
- Each source API stack includes unit testing using the AppSync EvaluateCode API and integration tests on both the source API and the Merged API after merge occurs.
- The sample requires that you have the AWS CDK installed.
- The sample requires that you have Node 16+ and NPM
- The sample requires that you have an AWS account with credentials in order to deploy the sample.
- Cleanup of the sample requires the AWS CLI to be installed.
-
Fork the repository on Github to create a repository that you own.
-
Clone the fork locally for your local development
git clone <your repository fork url>
-
Add the forked repository name in
YOUR_REPOSITORY_NAME
in bin/bookReviewsMergedApi.ts to ensure that the pipeline receives notifications when you commit a change to your repository on Github. -
Install dependencies and build the sample:
npm ci && npm run build && npx cdk synth
-
Create an AWS Secrets Manager secret named "github-token" containing a fine-grained Github personal access token as a plaintext secret. The token requires scopes to include repo and admin:repo_hook.
-
Deploy the AppSync SchemaBreakingChangeDetection stack. This stack is responsible for configuring the Cloudformation Hook which will perform breaking change detection of AppSync schema updates within Cloudformation.
export AWS_DEFAULT_REGION='us-east-1' npx cdk deploy BreakingChangeDetection
-
Deploy the BookReviews MergedApi AWS CodePipeline Stack. This stack is responsible for configuring the CodePipeline which will handle deploying changes to both the beta and production stage BookReviews Merged API. Note that initially the integration tests in this pipeline are not added initially because they have a dependency on the source APIs being associated.
npx cdk deploy BookReviewsMergedApiPipeline
-
Sign in to the AWS Management Console and open the CodePipeline console. You should see the BookReviewsMergedApiPipeline resource with an ongoing pipeline execution. Wait for the execution to finish to ensure that the stacks have been deployed.
-
Once the Merged API pipeline execution has completed, deploy the source API pipelined and wait for them to finish a pipeline execution to successfully deploy the beta and production stages.
npx cdk deploy AuthorsServicePipeline ReviewsServicePipeline BooksServicePipeline
-
Now that all initial resources are deployed, we can enable the Merged API integration tests. Uncomment the code for adding the Merged API integration tests in lib/mergedApi/book-reviews-mergedapi-pipeline.ts as well as lib/sourceApis/authorsService/authors-service-pipeline-stack.ts, lib/sourceApis/booksService/books-service-pipeline-stack.ts, and lib/sourceApis/reviewsService/reviews-service-pipeline-stack.ts. Commit the code to your branch on Github and the changes will be picked up and automatically enable the integration testing step during the next pipeline execution.
pipeline.addStage(new BookReviewsMergedApiStage(this, "BookReviewsMergedApiBetaStage", { env: { region: region }, stageName: 'beta' }), { post: [ new CodeBuildStep('Integ-Test-Beta-MergedApi', { env: { Stage: 'beta', AWS_REGION: this.region }, commands: [ "npm ci", "npm run build", "npm test integ-tests/mergedApi" ], rolePolicyStatements: [ integTestPolicyStatement, integTestListExportsPolicyStatement ] }) ] }); pipeline.addStage(new BookReviewsMergedApiStage(this, "BookReviewsMergedApiProdStage", { env: { region: region }, stageName: 'prod' }), { post: [ new CodeBuildStep('Integ-Test-Prod-MergedApi', { env: { Stage: 'prod', AWS_REGION: this.region }, commands: [ "npm ci", "npm run build", "npm test integ-tests/mergedApi" ], rolePolicyStatements: [ integTestPolicyStatement, integTestListExportsPolicyStatement ] }) ] });
- Clean up all the code pipeline stacks:
npx cdk destroy --all
- Clean up the Source API stacks created by the code pipeline (Requires AWS CLI):
./scripts/cleanup-source-api-beta-and-prod-stages.sh
- Clean up the Merged API stacks created by the code pipeline (Requires AWS CLI):
./scripts/cleanup-merged-api-beta-and-prod-stages.sh