├── .copywrite.hcl ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── backend.tf ├── main.tf ├── provider.tf └── scripts └── setup.sh /.copywrite.hcl: -------------------------------------------------------------------------------- 1 | schema_version = 1 2 | 3 | project { 4 | license = "MPL-2.0" 5 | copyright_year = 2018 6 | 7 | # (OPTIONAL) A list of globs that should not have copyright/license headers. 8 | # Supports doublestar glob patterns for more flexibility in defining which 9 | # files or folders should be ignored 10 | header_ignore = [ 11 | # "vendors/**", 12 | # "**autogen**", 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Copywrite 2 | 3 | on: 4 | push: 5 | tags: 6 | - v* 7 | branches: 8 | - main 9 | pull_request: 10 | workflow_dispatch: 11 | 12 | jobs: 13 | copywrite: 14 | name: Run Header Copyright 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0 19 | 20 | - name: Install Copywrite 21 | id: install 22 | uses: hashicorp/setup-copywrite@v1.0.0 23 | 24 | - name: Output Installed Copywrite Version 25 | run: echo "Installed Copywrite CLI ${{steps.install.outputs.version}}" 26 | 27 | - name: Run Copywrite Header Compliance 28 | run: copywrite headers --plan 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform 2 | terraform.tfstate* 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 HashiCorp, Inc. 2 | 3 | Mozilla Public License Version 2.0 4 | ================================== 5 | 6 | 1. Definitions 7 | -------------- 8 | 9 | 1.1. "Contributor" 10 | means each individual or legal entity that creates, contributes to 11 | the creation of, or owns Covered Software. 12 | 13 | 1.2. "Contributor Version" 14 | means the combination of the Contributions of others (if any) used 15 | by a Contributor and that particular Contributor's Contribution. 16 | 17 | 1.3. "Contribution" 18 | means Covered Software of a particular Contributor. 19 | 20 | 1.4. "Covered Software" 21 | means Source Code Form to which the initial Contributor has attached 22 | the notice in Exhibit A, the Executable Form of such Source Code 23 | Form, and Modifications of such Source Code Form, in each case 24 | including portions thereof. 25 | 26 | 1.5. "Incompatible With Secondary Licenses" 27 | means 28 | 29 | (a) that the initial Contributor has attached the notice described 30 | in Exhibit B to the Covered Software; or 31 | 32 | (b) that the Covered Software was made available under the terms of 33 | version 1.1 or earlier of the License, but not also under the 34 | terms of a Secondary License. 35 | 36 | 1.6. "Executable Form" 37 | means any form of the work other than Source Code Form. 38 | 39 | 1.7. "Larger Work" 40 | means a work that combines Covered Software with other material, in 41 | a separate file or files, that is not Covered Software. 42 | 43 | 1.8. "License" 44 | means this document. 45 | 46 | 1.9. "Licensable" 47 | means having the right to grant, to the maximum extent possible, 48 | whether at the time of the initial grant or subsequently, any and 49 | all of the rights conveyed by this License. 50 | 51 | 1.10. "Modifications" 52 | means any of the following: 53 | 54 | (a) any file in Source Code Form that results from an addition to, 55 | deletion from, or modification of the contents of Covered 56 | Software; or 57 | 58 | (b) any new file in Source Code Form that contains any Covered 59 | Software. 60 | 61 | 1.11. "Patent Claims" of a Contributor 62 | means any patent claim(s), including without limitation, method, 63 | process, and apparatus claims, in any patent Licensable by such 64 | Contributor that would be infringed, but for the grant of the 65 | License, by the making, using, selling, offering for sale, having 66 | made, import, or transfer of either its Contributions or its 67 | Contributor Version. 68 | 69 | 1.12. "Secondary License" 70 | means either the GNU General Public License, Version 2.0, the GNU 71 | Lesser General Public License, Version 2.1, the GNU Affero General 72 | Public License, Version 3.0, or any later versions of those 73 | licenses. 74 | 75 | 1.13. "Source Code Form" 76 | means the form of the work preferred for making modifications. 77 | 78 | 1.14. "You" (or "Your") 79 | means an individual or a legal entity exercising rights under this 80 | License. For legal entities, "You" includes any entity that 81 | controls, is controlled by, or is under common control with You. For 82 | purposes of this definition, "control" means (a) the power, direct 83 | or indirect, to cause the direction or management of such entity, 84 | whether by contract or otherwise, or (b) ownership of more than 85 | fifty percent (50%) of the outstanding shares or beneficial 86 | ownership of such entity. 87 | 88 | 2. License Grants and Conditions 89 | -------------------------------- 90 | 91 | 2.1. Grants 92 | 93 | Each Contributor hereby grants You a world-wide, royalty-free, 94 | non-exclusive license: 95 | 96 | (a) under intellectual property rights (other than patent or trademark) 97 | Licensable by such Contributor to use, reproduce, make available, 98 | modify, display, perform, distribute, and otherwise exploit its 99 | Contributions, either on an unmodified basis, with Modifications, or 100 | as part of a Larger Work; and 101 | 102 | (b) under Patent Claims of such Contributor to make, use, sell, offer 103 | for sale, have made, import, and otherwise transfer either its 104 | Contributions or its Contributor Version. 105 | 106 | 2.2. Effective Date 107 | 108 | The licenses granted in Section 2.1 with respect to any Contribution 109 | become effective for each Contribution on the date the Contributor first 110 | distributes such Contribution. 111 | 112 | 2.3. Limitations on Grant Scope 113 | 114 | The licenses granted in this Section 2 are the only rights granted under 115 | this License. No additional rights or licenses will be implied from the 116 | distribution or licensing of Covered Software under this License. 117 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 118 | Contributor: 119 | 120 | (a) for any code that a Contributor has removed from Covered Software; 121 | or 122 | 123 | (b) for infringements caused by: (i) Your and any other third party's 124 | modifications of Covered Software, or (ii) the combination of its 125 | Contributions with other software (except as part of its Contributor 126 | Version); or 127 | 128 | (c) under Patent Claims infringed by Covered Software in the absence of 129 | its Contributions. 130 | 131 | This License does not grant any rights in the trademarks, service marks, 132 | or logos of any Contributor (except as may be necessary to comply with 133 | the notice requirements in Section 3.4). 134 | 135 | 2.4. Subsequent Licenses 136 | 137 | No Contributor makes additional grants as a result of Your choice to 138 | distribute the Covered Software under a subsequent version of this 139 | License (see Section 10.2) or under the terms of a Secondary License (if 140 | permitted under the terms of Section 3.3). 141 | 142 | 2.5. Representation 143 | 144 | Each Contributor represents that the Contributor believes its 145 | Contributions are its original creation(s) or it has sufficient rights 146 | to grant the rights to its Contributions conveyed by this License. 147 | 148 | 2.6. Fair Use 149 | 150 | This License is not intended to limit any rights You have under 151 | applicable copyright doctrines of fair use, fair dealing, or other 152 | equivalents. 153 | 154 | 2.7. Conditions 155 | 156 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 157 | in Section 2.1. 158 | 159 | 3. Responsibilities 160 | ------------------- 161 | 162 | 3.1. Distribution of Source Form 163 | 164 | All distribution of Covered Software in Source Code Form, including any 165 | Modifications that You create or to which You contribute, must be under 166 | the terms of this License. You must inform recipients that the Source 167 | Code Form of the Covered Software is governed by the terms of this 168 | License, and how they can obtain a copy of this License. You may not 169 | attempt to alter or restrict the recipients' rights in the Source Code 170 | Form. 171 | 172 | 3.2. Distribution of Executable Form 173 | 174 | If You distribute Covered Software in Executable Form then: 175 | 176 | (a) such Covered Software must also be made available in Source Code 177 | Form, as described in Section 3.1, and You must inform recipients of 178 | the Executable Form how they can obtain a copy of such Source Code 179 | Form by reasonable means in a timely manner, at a charge no more 180 | than the cost of distribution to the recipient; and 181 | 182 | (b) You may distribute such Executable Form under the terms of this 183 | License, or sublicense it under different terms, provided that the 184 | license for the Executable Form does not attempt to limit or alter 185 | the recipients' rights in the Source Code Form under this License. 186 | 187 | 3.3. Distribution of a Larger Work 188 | 189 | You may create and distribute a Larger Work under terms of Your choice, 190 | provided that You also comply with the requirements of this License for 191 | the Covered Software. If the Larger Work is a combination of Covered 192 | Software with a work governed by one or more Secondary Licenses, and the 193 | Covered Software is not Incompatible With Secondary Licenses, this 194 | License permits You to additionally distribute such Covered Software 195 | under the terms of such Secondary License(s), so that the recipient of 196 | the Larger Work may, at their option, further distribute the Covered 197 | Software under the terms of either this License or such Secondary 198 | License(s). 199 | 200 | 3.4. Notices 201 | 202 | You may not remove or alter the substance of any license notices 203 | (including copyright notices, patent notices, disclaimers of warranty, 204 | or limitations of liability) contained within the Source Code Form of 205 | the Covered Software, except that You may alter any license notices to 206 | the extent required to remedy known factual inaccuracies. 207 | 208 | 3.5. Application of Additional Terms 209 | 210 | You may choose to offer, and to charge a fee for, warranty, support, 211 | indemnity or liability obligations to one or more recipients of Covered 212 | Software. However, You may do so only on Your own behalf, and not on 213 | behalf of any Contributor. You must make it absolutely clear that any 214 | such warranty, support, indemnity, or liability obligation is offered by 215 | You alone, and You hereby agree to indemnify every Contributor for any 216 | liability incurred by such Contributor as a result of warranty, support, 217 | indemnity or liability terms You offer. You may include additional 218 | disclaimers of warranty and limitations of liability specific to any 219 | jurisdiction. 220 | 221 | 4. Inability to Comply Due to Statute or Regulation 222 | --------------------------------------------------- 223 | 224 | If it is impossible for You to comply with any of the terms of this 225 | License with respect to some or all of the Covered Software due to 226 | statute, judicial order, or regulation then You must: (a) comply with 227 | the terms of this License to the maximum extent possible; and (b) 228 | describe the limitations and the code they affect. Such description must 229 | be placed in a text file included with all distributions of the Covered 230 | Software under this License. Except to the extent prohibited by statute 231 | or regulation, such description must be sufficiently detailed for a 232 | recipient of ordinary skill to be able to understand it. 233 | 234 | 5. Termination 235 | -------------- 236 | 237 | 5.1. The rights granted under this License will terminate automatically 238 | if You fail to comply with any of its terms. However, if You become 239 | compliant, then the rights granted under this License from a particular 240 | Contributor are reinstated (a) provisionally, unless and until such 241 | Contributor explicitly and finally terminates Your grants, and (b) on an 242 | ongoing basis, if such Contributor fails to notify You of the 243 | non-compliance by some reasonable means prior to 60 days after You have 244 | come back into compliance. Moreover, Your grants from a particular 245 | Contributor are reinstated on an ongoing basis if such Contributor 246 | notifies You of the non-compliance by some reasonable means, this is the 247 | first time You have received notice of non-compliance with this License 248 | from such Contributor, and You become compliant prior to 30 days after 249 | Your receipt of the notice. 250 | 251 | 5.2. If You initiate litigation against any entity by asserting a patent 252 | infringement claim (excluding declaratory judgment actions, 253 | counter-claims, and cross-claims) alleging that a Contributor Version 254 | directly or indirectly infringes any patent, then the rights granted to 255 | You by any and all Contributors for the Covered Software under Section 256 | 2.1 of this License shall terminate. 257 | 258 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 259 | end user license agreements (excluding distributors and resellers) which 260 | have been validly granted by You or Your distributors under this License 261 | prior to termination shall survive termination. 262 | 263 | ************************************************************************ 264 | * * 265 | * 6. Disclaimer of Warranty * 266 | * ------------------------- * 267 | * * 268 | * Covered Software is provided under this License on an "as is" * 269 | * basis, without warranty of any kind, either expressed, implied, or * 270 | * statutory, including, without limitation, warranties that the * 271 | * Covered Software is free of defects, merchantable, fit for a * 272 | * particular purpose or non-infringing. The entire risk as to the * 273 | * quality and performance of the Covered Software is with You. * 274 | * Should any Covered Software prove defective in any respect, You * 275 | * (not any Contributor) assume the cost of any necessary servicing, * 276 | * repair, or correction. This disclaimer of warranty constitutes an * 277 | * essential part of this License. No use of any Covered Software is * 278 | * authorized under this License except under this disclaimer. * 279 | * * 280 | ************************************************************************ 281 | 282 | ************************************************************************ 283 | * * 284 | * 7. Limitation of Liability * 285 | * -------------------------- * 286 | * * 287 | * Under no circumstances and under no legal theory, whether tort * 288 | * (including negligence), contract, or otherwise, shall any * 289 | * Contributor, or anyone who distributes Covered Software as * 290 | * permitted above, be liable to You for any direct, indirect, * 291 | * special, incidental, or consequential damages of any character * 292 | * including, without limitation, damages for lost profits, loss of * 293 | * goodwill, work stoppage, computer failure or malfunction, or any * 294 | * and all other commercial damages or losses, even if such party * 295 | * shall have been informed of the possibility of such damages. This * 296 | * limitation of liability shall not apply to liability for death or * 297 | * personal injury resulting from such party's negligence to the * 298 | * extent applicable law prohibits such limitation. Some * 299 | * jurisdictions do not allow the exclusion or limitation of * 300 | * incidental or consequential damages, so this exclusion and * 301 | * limitation may not apply to You. * 302 | * * 303 | ************************************************************************ 304 | 305 | 8. Litigation 306 | ------------- 307 | 308 | Any litigation relating to this License may be brought only in the 309 | courts of a jurisdiction where the defendant maintains its principal 310 | place of business and such litigation shall be governed by laws of that 311 | jurisdiction, without reference to its conflict-of-law provisions. 312 | Nothing in this Section shall prevent a party's ability to bring 313 | cross-claims or counter-claims. 314 | 315 | 9. Miscellaneous 316 | ---------------- 317 | 318 | This License represents the complete agreement concerning the subject 319 | matter hereof. If any provision of this License is held to be 320 | unenforceable, such provision shall be reformed only to the extent 321 | necessary to make it enforceable. Any law or regulation which provides 322 | that the language of a contract shall be construed against the drafter 323 | shall not be used to construe this License against a Contributor. 324 | 325 | 10. Versions of the License 326 | --------------------------- 327 | 328 | 10.1. New Versions 329 | 330 | Mozilla Foundation is the license steward. Except as provided in Section 331 | 10.3, no one other than the license steward has the right to modify or 332 | publish new versions of this License. Each version will be given a 333 | distinguishing version number. 334 | 335 | 10.2. Effect of New Versions 336 | 337 | You may distribute the Covered Software under the terms of the version 338 | of the License under which You originally received the Covered Software, 339 | or under the terms of any subsequent version published by the license 340 | steward. 341 | 342 | 10.3. Modified Versions 343 | 344 | If you create software not governed by this License, and you want to 345 | create a new license for such software, you may create and use a 346 | modified version of this License if you rename the license and remove 347 | any references to the name of the license steward (except to note that 348 | such modified license differs from this License). 349 | 350 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 351 | Licenses 352 | 353 | If You choose to distribute Source Code Form that is Incompatible With 354 | Secondary Licenses under the terms of this version of the License, the 355 | notice described in Exhibit B of this License must be attached. 356 | 357 | Exhibit A - Source Code Form License Notice 358 | ------------------------------------------- 359 | 360 | This Source Code Form is subject to the terms of the Mozilla Public 361 | License, v. 2.0. If a copy of the MPL was not distributed with this 362 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 363 | 364 | If it is not possible or desirable to put the notice in a particular 365 | file, then You may include the notice in a location (such as a LICENSE 366 | file in a relevant directory) where a recipient would be likely to look 367 | for such a notice. 368 | 369 | You may add additional accurate notices of copyright ownership. 370 | 371 | Exhibit B - "Incompatible With Secondary Licenses" Notice 372 | --------------------------------------------------------- 373 | 374 | This Source Code Form is "Incompatible With Secondary Licenses", as 375 | defined by the Mozilla Public License, v. 2.0. 376 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HCP Terraform Getting Started 2 | 3 | In this repo, you'll find a quick and easy path to get started using [HCP Terraform](https://app.terraform.io/) with the [Terraform CLI](https://github.com/hashicorp/terraform). 4 | 5 | ## What's here? 6 | 7 | This repo contains two main things: 8 | 9 | 1. An example Terraform configuration which provisions some mock infrastructure to a fictitious cloud provider called "Fake Web Services" using the [`fakewebservices`](https://registry.terraform.io/providers/hashicorp/fakewebservices/latest) provider. 10 | 1. A [script](./scripts/setup.sh) which automatically handles all the setup required to start using Terraform with HCP Terraform. 11 | 12 | ## Requirements 13 | 14 | - Terraform 0.14 or higher 15 | - The ability to run a bash script in your terminal 16 | - [`sed`](https://www.gnu.org/software/sed/) 17 | - [`curl`](https://curl.se/) 18 | - [`jq`](https://stedolan.github.io/jq/) 19 | 20 | ## Usage 21 | 22 | ### 1. Log in to HCP Terraform via the CLI 23 | 24 | Run `terraform login` and follow the prompts to get an API token for Terraform to use. If you don't have a HCP Terraform account, you can create one during this step. 25 | 26 | ### 2. Clone this repo 27 | 28 | ```sh 29 | git clone https://github.com/hashicorp/tfc-getting-started.git 30 | cd tfc-getting-started 31 | ``` 32 | 33 | ### 3. Run the setup script and follow the prompts 34 | 35 | ``` 36 | ./scripts/setup.sh 37 | ``` 38 | 39 | Welcome to HCP Terraform! 40 | -------------------------------------------------------------------------------- /backend.tf: -------------------------------------------------------------------------------- 1 | # Copyright (c) HashiCorp, Inc. 2 | # SPDX-License-Identifier: MPL-2.0 3 | 4 | # The block below configures Terraform to use the 'remote' backend with HCP Terraform. 5 | # For more information, see https://www.terraform.io/docs/backends/types/remote.html 6 | terraform { 7 | cloud { 8 | organization = "{{ORGANIZATION_NAME}}" 9 | 10 | workspaces { 11 | name = "{{WORKSPACE_NAME}}" 12 | } 13 | } 14 | 15 | required_version = ">= 1.1.2" 16 | } 17 | -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | # Copyright (c) HashiCorp, Inc. 2 | # SPDX-License-Identifier: MPL-2.0 3 | 4 | # The following configuration uses a provider which provisions [fake] resources 5 | # to a fictitious cloud vendor called "Fake Web Services". Configuration for 6 | # the fakewebservices provider can be found in provider.tf. 7 | # 8 | # After running the setup script (./scripts/setup.sh), feel free to change these 9 | # resources and 'terraform apply' as much as you'd like! These resources are 10 | # purely for demonstration and created in HCP Terraform, scoped to your HCP Terraform 11 | # user account. 12 | # 13 | # To review the provider and documentation for the available resources and 14 | # schemas, see: https://registry.terraform.io/providers/hashicorp/fakewebservices 15 | # 16 | # If you're looking for the configuration for the remote backend, you can find that 17 | # in backend.tf. 18 | 19 | 20 | resource "fakewebservices_vpc" "primary_vpc" { 21 | name = "Primary VPC" 22 | cidr_block = "0.0.0.0/1" 23 | } 24 | 25 | resource "fakewebservices_server" "servers" { 26 | count = 2 27 | 28 | name = "Server ${count.index + 1}" 29 | type = "t2.micro" 30 | vpc = fakewebservices_vpc.primary_vpc.name 31 | } 32 | 33 | resource "fakewebservices_load_balancer" "primary_lb" { 34 | name = "Primary Load Balancer" 35 | servers = fakewebservices_server.servers[*].name 36 | } 37 | 38 | resource "fakewebservices_database" "prod_db" { 39 | name = "Production DB" 40 | size = 256 41 | } 42 | -------------------------------------------------------------------------------- /provider.tf: -------------------------------------------------------------------------------- 1 | # Copyright (c) HashiCorp, Inc. 2 | # SPDX-License-Identifier: MPL-2.0 3 | 4 | # The following variable is used to configure the provider's authentication 5 | # token. You don't need to provide a token on the command line to apply changes, 6 | # though: using the remote backend, Terraform will execute remotely in HCP Terraform 7 | # where your token is already securely stored in your workspace! 8 | 9 | variable "provider_token" { 10 | type = string 11 | sensitive = true 12 | } 13 | 14 | provider "fakewebservices" { 15 | token = var.provider_token 16 | } 17 | -------------------------------------------------------------------------------- /scripts/setup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Copyright (c) HashiCorp, Inc. 3 | # SPDX-License-Identifier: MPL-2.0 4 | 5 | set -euo pipefail 6 | 7 | info() { 8 | printf "\r\033[00;35m$1\033[0m\n" 9 | } 10 | 11 | success() { 12 | printf "\r\033[00;32m$1\033[0m\n" 13 | } 14 | 15 | fail() { 16 | printf "\r\033[0;31m$1\033[0m\n" 17 | } 18 | 19 | divider() { 20 | printf "\r\033[0;1m========================================================================\033[0m\n" 21 | } 22 | 23 | pause_for_confirmation() { 24 | read -rsp $'Press any key to continue (ctrl-c to quit):\n' -n1 key 25 | } 26 | 27 | # Set up an interrupt handler so we can exit gracefully 28 | interrupt_count=0 29 | interrupt_handler() { 30 | ((interrupt_count += 1)) 31 | 32 | echo "" 33 | if [[ $interrupt_count -eq 1 ]]; then 34 | fail "Really quit? Hit ctrl-c again to confirm." 35 | else 36 | echo "Goodbye!" 37 | exit 38 | fi 39 | } 40 | trap interrupt_handler SIGINT SIGTERM 41 | 42 | # This setup script does all the magic. 43 | 44 | # Check for required tools 45 | declare -a req_tools=("terraform" "sed" "curl" "jq") 46 | for tool in "${req_tools[@]}"; do 47 | if ! command -v "$tool" > /dev/null; then 48 | fail "It looks like '${tool}' is not installed; please install it and run this setup script again." 49 | exit 1 50 | fi 51 | done 52 | 53 | # Get the minimum required version of Terraform 54 | minimumTerraformMajorVersion=0 55 | minimumTerraformMinorVersion=14 56 | minimumTerraformVersion=$(($minimumTerraformMajorVersion * 1000 + $minimumTerraformMinorVersion)) 57 | 58 | # Get the current version of Terraform 59 | installedTerraformMajorVersion=$(terraform version -json | jq -r '.terraform_version' | cut -d '.' -f 1) 60 | installedTerraformMinorVersion=$(terraform version -json | jq -r '.terraform_version' | cut -d '.' -f 2) 61 | installedTerraformVersion=$(($installedTerraformMajorVersion * 1000 + $installedTerraformMinorVersion)) 62 | 63 | # Check we meet the minimum required version 64 | if [ $installedTerraformVersion -lt $minimumTerraformVersion ]; then 65 | echo 66 | fail "Terraform $minimumTerraformMajorVersion.$minimumTerraformMinorVersion.x or later is required for this setup script!" 67 | echo "You are currently running:" 68 | terraform version 69 | exit 1 70 | fi 71 | 72 | # Set up some variables we'll need 73 | HOST="${1:-app.terraform.io}" 74 | BACKEND_TF=$(dirname ${BASH_SOURCE[0]})/../backend.tf 75 | PROVIDER_TF=$(dirname ${BASH_SOURCE[0]})/../provider.tf 76 | TERRAFORM_VERSION=$(terraform version -json | jq -r '.terraform_version') 77 | 78 | # Check that we've already authenticated via Terraform in the static credentials 79 | # file. Note that if you configure your token via a credentials helper or any 80 | # other method besides the static file, this script will not take that in to 81 | # account - but we do this to avoid embedding a Go binary in this simple script 82 | # and you hopefully do not need this Getting Started project if you're using one 83 | # already! 84 | CREDENTIALS_FILE="$HOME/.terraform.d/credentials.tfrc.json" 85 | 86 | # Credentials are located in App/Data/Roaming on Windows 87 | if [[ "$OSTYPE" =~ ^msys || "$OSTYPE" =~ ^cygwin || "$OSTYPE" =~ ^win32 ]]; then 88 | CREDENTIALS_FILE="$APPDATA/terraform.d/credentials.tfrc.json" 89 | fi 90 | 91 | TOKEN=$(jq -j --arg h "$HOST" '.credentials[$h].token' "$CREDENTIALS_FILE") 92 | if [[ ! -f $CREDENTIALS_FILE || $TOKEN == null ]]; then 93 | fail "We couldn't find a token in the Terraform credentials file at $CREDENTIALS_FILE." 94 | fail "Please run 'terraform login', then run this setup script again." 95 | exit 1 96 | fi 97 | 98 | # Check that this is your first time running this script. If not, we'll reset 99 | # all local state and restart from scratch! 100 | if ! git diff-index --quiet --no-ext-diff HEAD --; then 101 | echo "It looks like you may have run this script before! Re-running it will reset any 102 | changes you've made to backend.tf and provider.tf." 103 | echo 104 | pause_for_confirmation 105 | 106 | git checkout HEAD backend.tf provider.tf 107 | rm -rf .terraform 108 | rm -f *.lock.hcl 109 | fi 110 | 111 | echo 112 | printf "\r\033[00;35;1m 113 | -------------------------------------------------------------------------- 114 | Getting Started with HCP Terraform 115 | -------------------------------------------------------------------------\033[0m" 116 | echo 117 | echo 118 | echo "HCP Terraform offers secure, easy-to-use remote state management and allows 119 | you to run Terraform remotely in a controlled environment. HCP Terraform runs 120 | can be performed on demand or triggered automatically by various events." 121 | echo 122 | echo "This script will set up everything you need to get started. You'll be 123 | applying some example infrastructure - for free - in less than a minute." 124 | echo 125 | info "First, we'll do some setup and configure Terraform to use HCP Terraform." 126 | echo 127 | pause_for_confirmation 128 | 129 | # Create a HCP Terraform organization 130 | echo 131 | echo "Creating an organization and workspace..." 132 | sleep 1 133 | setup() { 134 | curl https://$HOST/api/getting-started/setup \ 135 | --request POST \ 136 | --silent \ 137 | --header "Content-Type: application/vnd.api+json" \ 138 | --header "Authorization: Bearer $TOKEN" \ 139 | --header "User-Agent: tfc-getting-started" \ 140 | --data @- << REQUEST_BODY 141 | { 142 | "workflow": "remote-operations", 143 | "terraform-version": "$TERRAFORM_VERSION" 144 | } 145 | REQUEST_BODY 146 | } 147 | 148 | response=$(setup) 149 | err=$(echo $response | jq -r '.errors') 150 | 151 | if [[ $err != null ]]; then 152 | err_msg=$(echo $err | jq -r '.[0].detail') 153 | if [[ $err_msg != null ]]; then 154 | fail "An error occurred: ${err_msg}" 155 | else 156 | fail "An unknown error occurred: ${err}" 157 | fi 158 | exit 1 159 | fi 160 | 161 | # TODO: If there's an active trial, we should just retrieve that and configure 162 | # it instead (especially if it has no state yet) 163 | info=$(echo $response | jq -r '.info') 164 | if [[ $info != null ]]; then 165 | info "\n${info}" 166 | exit 0 167 | fi 168 | 169 | organization_name=$(echo $response | jq -r '.data."organization-name"') 170 | workspace_name=$(echo $response | jq -r '.data."workspace-name"') 171 | 172 | echo 173 | echo "Writing HCP Terraform configuration to backend.tf..." 174 | sleep 2 175 | 176 | # We don't sed -i because MacOS's sed has problems with it. 177 | TEMP=$(mktemp) 178 | cat $BACKEND_TF | 179 | # Add the backend config for the hostname if necessary 180 | # Note: sed 9a means append the string that follows \\ at line 9 in backend.tf 181 | if [[ "$HOST" != "app.terraform.io" ]]; then sed "9a\\ 182 | \ hostname = \"$HOST\" 183 | "; else cat; fi | 184 | # replace the organization and workspace names 185 | sed "s/{{ORGANIZATION_NAME}}/${organization_name}/" | 186 | sed "s/{{WORKSPACE_NAME}}/${workspace_name}/" \ 187 | > $TEMP 188 | mv $TEMP $BACKEND_TF 189 | 190 | # add extra provider config for the hostname if necessary 191 | if [[ "$HOST" != "app.terraform.io" ]]; then 192 | TEMP=$(mktemp) 193 | cat $PROVIDER_TF | 194 | # Note: sed 15a\\ means append the strings that follows the \\ at line 15 in provider.tf 195 | sed "15a\\ 196 | \ hostname = var.provider_hostname 197 | " \ 198 | > $TEMP 199 | echo " 200 | variable \"provider_hostname\" { 201 | type = string 202 | }" >> $TEMP 203 | mv $TEMP $PROVIDER_TF 204 | fi 205 | 206 | echo 207 | divider 208 | echo 209 | success "Ready to go; the example configuration is set up to use HCP Terraform!" 210 | echo 211 | echo "An example workspace named '${workspace_name}' was created for you." 212 | echo "You can view this workspace in the HCP Terraform UI here:" 213 | echo "https://$HOST/app/${organization_name}/workspaces/${workspace_name}" 214 | echo 215 | info "Next, we'll run 'terraform init' to initialize the backend and providers:" 216 | echo 217 | echo "$ terraform init" 218 | echo 219 | pause_for_confirmation 220 | 221 | echo 222 | terraform init 223 | echo 224 | echo "..." 225 | sleep 2 226 | echo 227 | divider 228 | echo 229 | info "Now it’s time for 'terraform plan', to see what changes Terraform will perform:" 230 | echo 231 | echo "$ terraform plan" 232 | echo 233 | pause_for_confirmation 234 | 235 | echo 236 | terraform plan 237 | echo 238 | echo "..." 239 | sleep 3 240 | echo 241 | divider 242 | echo 243 | success "The plan is complete!" 244 | echo 245 | echo "This plan was initiated from your local machine, but executed within 246 | HCP Terraform!" 247 | echo 248 | echo "HCP Terraform runs Terraform on disposable virtual machines in 249 | its own cloud infrastructure. This 'remote execution' helps provide consistency 250 | and visibility for critical provisioning operations. It also enables notifications, 251 | version control integration, and powerful features like Sentinel policy enforcement 252 | and cost estimation (shown in the output above)." 253 | echo 254 | info "To actually make changes, we'll run 'terraform apply'. We'll also auto-approve 255 | the result, since this is an example:" 256 | echo 257 | echo "$ terraform apply -auto-approve" 258 | echo 259 | pause_for_confirmation 260 | 261 | echo 262 | terraform apply -auto-approve 263 | 264 | echo 265 | echo "..." 266 | sleep 3 267 | echo 268 | divider 269 | echo 270 | success "You did it! You just provisioned infrastructure with HCP Terraform!" 271 | echo 272 | info "The organization we created here has a 30-day free trial of the Team & 273 | Governance tier features. After the trial ends, you'll be moved to the Free tier." 274 | echo 275 | echo "You now have:" 276 | echo 277 | echo " * Workspaces for organizing your infrastructure. HCP Terraform manages" 278 | echo " infrastructure collections with workspaces instead of directories. You" 279 | echo " can view your workspace here:" 280 | echo " https://$HOST/app/$organization_name/workspaces/$workspace_name" 281 | echo " * Remote state management, with the ability to share outputs across" 282 | echo " workspaces. We've set up state management for you in your current" 283 | echo " workspace, and you can reference state from other workspaces using" 284 | echo " the 'terraform_remote_state' data source." 285 | echo " * Much more!" 286 | echo 287 | info "To see the mock infrastructure you just provisioned and continue exploring 288 | HCP Terraform, visit: 289 | https://$HOST/fake-web-services" 290 | echo 291 | exit 0 292 | --------------------------------------------------------------------------------