Skip to main content

AWS CloudFormation

This tutorial focuses on the usage of AWS CloudFormation as an Infrastructure-as-a-code tool to provision Harness infrastructure within a Harness pipeline.

Provision and delete infrastructure

This tutorial will provision resources in a Custom stage using the CloudFormation Create Stack step.

Before you begin

Verify that you have the following:

  1. A Kubernetes cluster in CloudFormation: Create a stack in CloudFormation to provision an EKS cluster to be used as deployment infrastructure.
  2. Obtain GitHub personal access token with the repo scope. See the GitHub documentation on creating a personal access token.
  3. Fork the harnessed-example-apps repository through the GitHub website, which contains the CF template file.
  4. A functioning Harness pipeline that deploys an application on your cluster using a manifest. Please follow this get started tutorial to set it up.

Get started

  1. Log into Harness.
  2. Select Projects, and then select Default Project.

Secrets

  1. Under Project Setup, select Secrets.
    • Select New Secret, and then select Text.
    • Enter the secret name harness_gitpat.
    • For the secret value, paste the GitHub personal access token you saved earlier.
    • Select Save.

AWS connector

  1. Copy the contents of aws-connector.yml.
  2. In your Harness project in the Harness Manager, under Project Setup, select Connectors.
  3. Select Create via YAML Builder and paste the copied YAML.
  4. Replace the accessKey placeholder with the AWS access key for the AWS user you created (with the required policies).
  5. Add the permananet secret key in secretKeyRef.
  6. Here we assume the region for secret key to be us-east-1. Please replace it with the appropriate region.
  7. Add an active delegate under the delegateSelectors.
  8. In projectIdentifier, replace with the project identifier with yours, for example, default.
  9. Select Save Changes and verify that the new connector named harness_awsconnector is successfully created.
  10. Finally, select Connection Test under Connectivity Status to ensure the connection is successful.

GitHub connector

info

If you already have a Git connector that gives access to your forked harnesscd-example-apps repo, then proceed to creating a pipeline directly.

  1. Create the GitHub connector.
    • Copy the contents of github-connector.yml.
    • In your Harness project in the Harness Manager, under Project Setup, select Connectors.
    • Select Create via YAML Builder and paste the copied YAML.
    • Assuming you have already forked the harnessed-example-apps repository mentioned earlier, replace GITHUB_USERNAME with your GitHub account username in the YAML.
    • In projectIdentifier, verify that the project identifier is correct. You can see the Id in the browser URL (after account). If it is incorrect, the Harness YAML editor will suggest the correct Id.
    • Select Save Changes and verify that the new connector named harness_gitconnector is successfully created.
    • Finally, select Connection Test under Connectivity Status to ensure the connection is successful.

Create pipeline with custom stage

  1. In Default Project, select Pipelines.
    • Select New Pipeline or Create a Pipeline.
    • Enter the name cf_provisioned_pipeline.
    • Select Inline to store the pipeline in Harness.
    • Select Start.
  2. In the pipeline studio, in Select Stage Type, select Custom Stage.
  3. Name the stage infra-provision and select Set Up Stage.
  4. Select Add Step, and select CloudFormation Create Stack.

CloudFormation Create Stack step

  1. Under Step Parameters, add the Provision Identifier as demoprovision.
  2. Add the AWS Connector you created before and add the region for which your connector has persmission to create the CloudFormation Stack.
  3. Assuming you have already forked the harness-cd-example apps repo and have a functional GitHub connector, use the same connector to add the template file in the file store.
  4. Select the Git Fetch type as Latest from Branch, add the Branch as main, set the Template File Path as cloudformation/cf_template.yaml, and select Submit
  5. Now provide the Stack Name as harness-provisoned-stack and select Apply Changes.
  6. Now Save and Run the pipeline.

Check your AWS Management console for CloudFormation and you'll find the new CloudFormation Stack created.