Traditionally, code in technical books uses GitHub for a good reason: everyone knows it, and it's free for open source (or just public) repositories. We are going to use GitLab though. First, it's free for both public and private projects. Second, it has some features that GitHub lacks, and we will need them for this chapter: more on it later.
This means that before proceeding further, get yourself an account at https://about.gitlab.com/ (you can use your GitHub account to log in to GitLab with just few clicks).
All code samples will be still available at https://github.com/ as well.
We will start by doing a revision (see what I did there?) of all the files we have by now. All the code written previously in the book will be publicly available on GitLab.
Go to your directory and run git init
to initialize a new Git repository. Let's check what Git wants us to add to the repository:
$> git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) .terraform/ base.json development.tfvars custom_data_source.rb graph.png id_rsa.pub modules/ playbook.yml rolling_update.rb specs/ template.tf terraform.tfstate terraform.tfstate.backup variables.tf nothing added to commit but untracked files present (use "git add" to track)
We decided to perform rolling updates with Auto-scaling groups, thus we don't need rolling_update.rb
anymore. We've also switched to using only Puppet, which means playbook.yml
needs to go too. Storing the graph image inside this repository is meaningless: throw it away as well. Also, remove the specs/
folder: we will revise our approach to test servers later. We won't use an external data source, which means that customer_data_source.rb
is obsolete.
As you might remember, Terraform installs local modules by making symlink
to the .terraform
directory. It should not be inside the Git repository. We can make it invisible for Git by creating the .gitignore
file with the following content:
.terraform/
This is what the list of files for your first commit needs to look like:
.gitignore
base.json
development.tfvars
id_rsa.pub
modules/
template.tf
terraform.tfstate
terraform.tfstate.backup
variables.tf
After this small cleanup, we can commit
our command as follows:
$> git add . $> git commit -m "Initial commit"
Now configure your remote
repository and push
it there:
$> git remote add origin [email protected]:Fodoj/packt-terraform-book.git $> git push origin master
You can get a link such as [email protected]
inside a GitLab web interface on the projects page. In the earlier example, Fodoj
is the author's username and packt-terraform-book
is the name of the repository.
Great! Now all the code we've written so far is version controlled! We can do changes to the template, run the terraform apply
command, and commit
it to the repository. Our colleagues can pull these changes and do their own changes as well. It's already so much better than what we had before.
Note that all the smaller steps will not be present in the commit
history. You have to do them yourself if you really want to learn how to use Terraform.
You can download it from the following repository in case you were too lazy to write it yourself during the previous chapters: https://gitlab.com/Fodoj/packt-terraform-book/tree/master
3.22.51.241