|
| 1 | +--- |
| 2 | +title: Integrating Atlantis with Opentofu |
| 3 | +lang: en-US |
| 4 | +--- |
| 5 | + |
| 6 | +# Integrating Atlantis with Opentofu |
| 7 | + |
| 8 | +::: info |
| 9 | +This post was originally written on May 27nd, 2024 |
| 10 | +Original post: <https://dev.to/jmateusousa/integrating-atlantis-with-opentofu-lnd> |
| 11 | +::: |
| 12 | + |
| 13 | +## What was our motivation? |
| 14 | + |
| 15 | +Due to the Terraform license change, many companies are migrating their IAC processes to OpenTofu, with this in mind and knowing that many of them use Atlantis and Terraform as infrastructure delivery automation, I created this documentation showing what to do to integrate Atlantis with OpenTofu. |
| 16 | + |
| 17 | +Stack: Atlantis, Terragrunt, OpenTofu, Github, ALB, EKS. |
| 18 | + |
| 19 | +We will implement it with your [Helm chart](https://www.runatlantis.io/docs/deployment.html#kubernetes-helm-chart): |
| 20 | + |
| 21 | +**1** - Add the runatlantis repository. |
| 22 | +``` |
| 23 | +helm repo add runatlantis https://runatlantis.github.io/helm-charts |
| 24 | +``` |
| 25 | +**2** - Create file values.yaml and run: |
| 26 | +``` |
| 27 | +helm inspect values runatlantis/atlantis > values.yaml |
| 28 | +``` |
| 29 | +**3** - Edit the file values.yaml and add your credentials access and secret which will be used in the Atlantis webhook configuration: |
| 30 | +See as create a [GitHubApp](https://docs.github.com/pt/apps/creating-github-apps/about-creating-github-apps). |
| 31 | + |
| 32 | +``` |
| 33 | +githubApp: |
| 34 | + id: "CHANGE ME" |
| 35 | + key: | |
| 36 | + -----BEGIN RSA PRIVATE KEY----- |
| 37 | + "CHANGE ME" |
| 38 | + -----END RSA PRIVATE KEY----- |
| 39 | + slug: atlantis |
| 40 | +# secret webhook Atlantis |
| 41 | + secret: "CHANGE ME" |
| 42 | +``` |
| 43 | +**4** - Enter the org and repository from github that Atlantis will interact in orgAllowlist: |
| 44 | +``` |
| 45 | +# All repositories the org |
| 46 | +orgAllowlist: github.com/MY-ORG/* |
| 47 | +
|
| 48 | +or |
| 49 | +# Just one repository |
| 50 | +orgAllowlist: github.com/MY-ORG/MY-REPO-IAC |
| 51 | +
|
| 52 | +or |
| 53 | +# All repositories that start with MY-REPO-IAC- |
| 54 | +orgAllowlist: github.com/MY-ORG/MY-REPO-IAC-* |
| 55 | +``` |
| 56 | +**5** - Now let’s configure the script that will be executed upon startup of the Atlantis init pod. In this step we download and install Terragrunt and OpenTofu, as well as include their binaries in the shared dir ```/plugins```. |
| 57 | +``` |
| 58 | +initConfig: |
| 59 | + enabled: true |
| 60 | + image: alpine:latest |
| 61 | + imagePullPolicy: IfNotPresent |
| 62 | + # sharedDir is set as env var INIT_SHARED_DIR |
| 63 | + sharedDir: /plugins |
| 64 | + workDir: /tmp |
| 65 | + sizeLimit: 250Mi |
| 66 | + # example of how the script can be configured to install tools/providers required by the atlantis pod |
| 67 | + script: | |
| 68 | + #!/bin/sh |
| 69 | + set -eoux pipefail# terragrunt |
| 70 | + TG_VERSION="0.55.10" |
| 71 | + TG_SHA256_SUM="1ad609399352348a41bb5ea96fdff5c7a18ac223742f60603a557a54fc8c6cff" |
| 72 | + TG_FILE="${INIT_SHARED_DIR}/terragrunt" |
| 73 | + wget https://github.com/gruntwork-io/terragrunt/releases/download/v${TG_VERSION}/terragrunt_linux_amd64 -O "${TG_FILE}" |
| 74 | + echo "${TG_SHA256_SUM} ${TG_FILE}" | sha256sum -c |
| 75 | + chmod 755 "${TG_FILE}" |
| 76 | + terragrunt -v |
| 77 | +
|
| 78 | + # OpenTofu |
| 79 | + TF_VERSION="1.6.2" |
| 80 | + TF_FILE="${INIT_SHARED_DIR}/tofu" |
| 81 | + wget https://github.com/opentofu/opentofu/releases/download/v${TF_VERSION}/tofu_${TF_VERSION}_linux_amd64.zip |
| 82 | + unzip tofu_${TF_VERSION}_linux_amd64.zip |
| 83 | + mv tofu ${INIT_SHARED_DIR} |
| 84 | + chmod 755 "${TF_FILE}" |
| 85 | + tofu -v |
| 86 | +``` |
| 87 | +**6** - Here we configure the envs to avoid downloading alternative versions of Terraform and indicate to Terragrunt where it should fetch the OpenTofu binary. |
| 88 | +``` |
| 89 | +# envs |
| 90 | +environment: |
| 91 | + ATLANTIS_TF_DOWNLOAD: false |
| 92 | + TERRAGRUNT_TFPATH: /plugins/tofu |
| 93 | +``` |
| 94 | +**7** - Last but not least, here we specify which Atlantis-side configurations we will have for the repositories. |
| 95 | +``` |
| 96 | +# repository config |
| 97 | +repoConfig: | |
| 98 | + --- |
| 99 | + repos: |
| 100 | + - id: /.*/ |
| 101 | + apply_requirements: [approved, mergeable] |
| 102 | + allow_custom_workflows: true |
| 103 | + allowed_overrides: [workflow, apply_requirements, delete_source_branch_on_merge] |
| 104 | +``` |
| 105 | +**8** - Configure Atlantis webhook ingress, in the example below we are using the AWS ALB. |
| 106 | +``` |
| 107 | +# ingress config |
| 108 | +ingress: |
| 109 | + annotations: |
| 110 | + alb.ingress.kubernetes.io/backend-protocol: HTTP |
| 111 | + alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:certificate |
| 112 | + alb.ingress.kubernetes.io/group.name: external-atlantis |
| 113 | + alb.ingress.kubernetes.io/healthcheck-path: /healthz |
| 114 | + alb.ingress.kubernetes.io/healthcheck-port: "80" |
| 115 | + alb.ingress.kubernetes.io/healthcheck-protocol: HTTP |
| 116 | + alb.ingress.kubernetes.io/listen-ports: '[{"HTTPS":443}]' |
| 117 | + alb.ingress.kubernetes.io/scheme: internet-facing |
| 118 | + alb.ingress.kubernetes.io/ssl-redirect: "443" |
| 119 | + alb.ingress.kubernetes.io/success-codes: "200" |
| 120 | + alb.ingress.kubernetes.io/target-type: ip |
| 121 | + apiVersion: networking.k8s.io/v1 |
| 122 | + enabled: true |
| 123 | + host: atlantis.your.domain |
| 124 | + ingressClassName: aws-ingress-class-name |
| 125 | + path: /* |
| 126 | + pathType: ImplementationSpecific |
| 127 | +``` |
| 128 | +Save all changes made to ```values.yaml``` |
| 129 | + |
| 130 | +**9** - Using one of the Atlantis options custom workflows, we can create a file ```atlantis.yaml``` in the root folder of your repository, the example below should meet most scenarios, adapt as needed. |
| 131 | +``` |
| 132 | +version: 3 |
| 133 | +automerge: true |
| 134 | +parallel_plan: true |
| 135 | +parallel_apply: false |
| 136 | +projects: |
| 137 | +- name: terragrunt |
| 138 | + dir: . |
| 139 | + workspace: terragrunt |
| 140 | + delete_source_branch_on_merge: true |
| 141 | + autoplan: |
| 142 | + enabled: false |
| 143 | + apply_requirements: [mergeable, approved] |
| 144 | + workflow: terragrunt |
| 145 | +workflows: |
| 146 | + terragrunt: |
| 147 | + plan: |
| 148 | + steps: |
| 149 | + - env: |
| 150 | + name: TF_IN_AUTOMATION |
| 151 | + value: 'true' |
| 152 | + - run: find . -name '.terragrunt-cache' | xargs rm -rf |
| 153 | + - run: terragrunt init -reconfigure |
| 154 | + - run: |
| 155 | + command: terragrunt plan -input=false -out=$PLANFILE |
| 156 | + output: strip_refreshing |
| 157 | + apply: |
| 158 | + steps: |
| 159 | + - run: terragrunt apply $PLANFILE |
| 160 | +``` |
| 161 | +**10** - Now let’s go to the installation itself, search for the available versions of Atlantis: |
| 162 | +``` |
| 163 | +helm search repo runatlantis |
| 164 | +``` |
| 165 | +Replace ```CHART-VERSION``` with the version you want to install and run the command below: |
| 166 | + |
| 167 | +``` |
| 168 | +helm upgrade -i atlantis runatlantis/atlantis --version CHART-VERSION -f values.yaml --create-namespace atlantis |
| 169 | +``` |
| 170 | + |
| 171 | +Now, see as configure Atlantis [webhook on github](https://www.runatlantis.io/docs/configuring-webhooks.html?source=post_page-----85ca0fbe45e5--------------------------------#github-github-enterprise) repository. |
| 172 | + |
| 173 | +See as Atlantis [work](https://www.runatlantis.io/docs/using-atlantis.html?source=post_page-----85ca0fbe45e5--------------------------------). |
| 174 | + |
| 175 | +Find out more at: |
| 176 | + |
| 177 | +https://www.runatlantis.io/guide.html. |
| 178 | +https://opentofu.org/docs/. |
| 179 | +https://github.com/runatlantis/atlantis/issues/3741. |
| 180 | + |
| 181 | +Share it with your friends =) |
0 commit comments