Add a second deployment target to the pipeline config file


So far, we have configured our pipeline to deploy your application to your team-shared-sandbox-dev account. In this lesson we will configure the project to deploy to a second environment - team-shared-sandbox-test

Locate GLF Pipeline config file and examine it

Lets navigate to the config file and add team-shared-sandbox-test as a target account. Find the config file on this path: [your-app-name]/cdk/glf-config.yml

glf-config location

Part of the configuration file will consist of a deploy target definition (in this case it will be team-shared-sandbox-dev as this is what we instructed ALBA to do). We will need to duplicate this part, changing the deploy account details to reflect team-shared-sandbox-test.

Closer look at the pipeline config file

Glf pipeline config

Your GLF pipeline config file may look something like the image above. The names of entries like PipelineName will be specific to your project. See GLF Pipeline documentation for explanation of items defined.

The pipeline configuration file is in YAML format which is somewhat similar to JSON (JSON vs YAML).

Adding a new pipeline definition for test environment

Easiest way to create a new deploy config is to copy-paste the existing one, starting from “- PipelineName” on line 6 to the closing “]” bracket on line 42 and adjust values to match the new deploy account details.

Make the following changes to the config of your new pipeline:

  1. PipelineName: change the dev suffix to test

  2. SourceBranch: change from dev to test

  3. DeployAccount: 225714867674

Example of how your pipeline config should look like (bar the unique project name references):

Before: further configurations to the stack

After: overview of how the stack should look

Push your changes to the dev branch

We need to add the changes to the dev branch, to ensure that the GLF pipeline definition is the same across all branches (dev/test/prod etc).

git add .
git commit -m "fix: modify pipeline definition to add a new environment"

Deploy the new instance of the GLF pipeline for test environment

Use npx cdk deploy to deploy another stack to the team-shared-sandbox-test account. npx cdk deploy from CLI

Deployment troubles

You will likely get an error now: Error trace due to stack config Let’s understand why it happened. Since we have added another GLF pipeline to be deployed (as well as your application stacks), CDK does not know which stacks you are attempting to deploy now.

Enter npx cdk list in your terminal to see the list of available stacks to deploy. At the bottom of the output, you will see names of the stacks you can deploy: Deployable stacks listed (!)You can see that workload stacks (stacks that the GLF pipeline will create in order to deploy your application to the workload account) are also mentioned here. Do not deploy them yourself via the console as it will deploy your actual application to the devops account. Specify the stack that you want to deploy - npx cdk deploy [YOUR-PROJECT-NAME]-test, for example - npx cdk deploy LkTestApp-LkTestApp-test. Approve the prompt to deploy changes.

Troubles just dont end 😞

This will create a second pipeline within team-shared-sandbox-devops account that will deploy your CDK app to team-shared-sandbox-test. Watch the pipeline progress in your browser:

Oh no! It seems our pipeline has failed. Can you guess why?

This is a new instance of the GLF pipeline stack that was created in our team-shared-sandbox-devops account based on the second config that we added just a little bit earlier in glf-config.yaml

This new stack grabs the code from the test branch. But we have not created and pushed the test branch yet. All our work so far has only been done in the dev branch. Go back to your terminal and enter git checkout -b test which will create a local test branch and copy over your code from the dev branch.

Push your branch to the remote - git push --set-upstream origin test

Our project builds in the new environment

After the push is complete, go back to CodePipeline in team-shared-sandbox-devops and look at your GLF pipeline (test) automatically trigger again and start building the application based on the code that you just pushed to the test branch.

Once it completes, you will be able to see your applications constructs deployed to the team-shared-sandbox-test account.