Building an ECR template
This guide is designed to walk you through adding resources to the blank template to support an Elastic Container Registry (ECR) patterned extension, in order to clarify how CDK-Cosmos works. If you would prefer to skip this step and use a pre-existing ECR template, it is available at cosmos-extension-cdk.
lib/ci-cd-solar-system.ts
You will need to import the DockerPipeline construct from the CDK-Cosmos building blocks
Inside the export statement, above the constructor, add a code pipeline variable
Inside the constructor, add the below code after super();
. The codeRepo referenced here is the CodeCommit repo used for your app code (the default option). Remove this reference if using an alternate source. DockerPipeline provides a default build spec, but you can also pass a custom one if preferred.
This section is optional but recommended. The template includes a function to allow you to add discrete stages to your cdk pipeline to deploy individual stacks by referencing the function in bin/main.ts
. The below function allows you to add stages to your docker pipeline. If desired, add this code below addCdkDeployEnvStageToCdkPipeline()
, within the export
statement.
lib/cosmos.ts
This file sets up the Code Repository (AWS CodeCommit) and ECR Repository for your extension.
Add the following imports:
Replace the export function with the below code. If desired, change the EcrRepo name from "Frontend".
bin/galaxy.ts
This file will remain unchanged, as we don't need to create any resources at the galaxy (account) level. Resources are either scoped to the cosmos (management account) or solar system (dev, test, prod environment etc) level.
bin/solar-system.ts
You will need to import the following constructs:
Add app version to solar system props
Add the following inside the constructor, after super();
bin/main.ts
As mentioned in the intro, this file instantiates all the resources you have defined in the above files and is where you can design your Cosmos infrastructure, including AWS accounts, stacks and pipeline stages.
Add the following to imports
In this file, you set the environment configuration for each galaxy and then instantiate a galaxy per account. Examples are included in the blank template. Duplicate these and replace the names as needed. It is essential that names match those set in your core CDK-Cosmos, e.g. Dev
must be consistently named.
For each galaxy, add permissions to allow accessing the ECR repo (repo located in the management account)
For each solar system, create a new solar system stack, ensuring the name matches with your core Cosmos. This allows the portal construct to use resources from core cosmos. You will also need to pass app version as a prop.
Important note with solar systems: Currently, solar systems must be commented out in the first run of the bootstrapper. You can then uncomment solar systems, push up your changes and re-run the cdk pipeline. See Getting Started - Extension # Solar Systems for more information.
Optionally, add a deployment stage in the pipeline to target specific solar systems (can pass one or multiple stacks). If not, all stacks will be deployed in the final deploy stage of the pipeline. It is, however, recommended to separate dev and prod as a minimum.
Again optionally, you can also add a separate stage in the CDK pipeline to target specific stacks
As with the solar system, this should be commented out in the first run of the pipeline (as the solar system targeted by this pipeline stage has not yet been created). Adding a new stage to the pipeline will change it's definition, which will make it pause, and you will have to re-run the pipeline. This is a property of CodePipeline, and something that may be addressed in future versions of CDK-Cosmos by implementing the new CDK Pipeline construct provided by AWS. In the meantime, the blank template includes an optional CiCd stage in the pipeline, which will allow you to deploy any changes needed to the CiCd infrastructure first and minimise waiting time on additional runs of the pipeline.
Final Steps
At the end of this guide, you should have all the resources needed to upload a container to your bootstrapped extension. You will be able to test it out by uploading the sample node app or your own app to the CodeCommit app-*-code-repo that will be created when you bootstrap your extension and running the code pipeline.