Skip to content

wamaithaNyamu/building-microservices-with-terraform-ansible-docker-dockercompose-and-github-actions

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn

building-microservices-with-terraform-ansible-docker-dockercompose-and-github-actions Accompanying blog can be found here

Building a microservices using Terraform Ansible Docker Docker-Compose and Github actions.

Prerequisites

  1. A linode api token.
  2. Create a .env file in the microservices folder. Add the following value
  3. Change the branch you need the Github Actions to run on.
    MONGO_INITDB_DATABASE=admin
    MONGO_INITDB_ROOT_USERNAME=root
    MONGO_INITDB_ROOT_PASSWORD=example
    MONGODB_CONNSTRING=mongodb://root:example@mongo:27017/test?directConnection=true&authSource=admin&replicaSet=replicaset&retryWrites=true
  1. In the Terraform directory create the file terraform.tfvars and add the following:
linode-token="add_token_here_within_the_quotes"
root_pass="generate_a_strong_password_and_add_it_here_between_this_quotes"

How to run the code using your laptop as the Ansible controller machine

Navigate to the Scripts folder

    cd Scripts

Run the bash installation script. This script installs Ansible, Terraform and ssh pass to your laptop

    ./installations.sh
    

Run the Terraform setup script that spins up a new ubuntu VM on Linode, generates ssh keys , saves the ip address on the inventory.ini, logs in to the VM and pastes the public key.

   ./terraform_setup.sh
    

Run ansible script. The Script, logs in to the VM, installs the needed modules , spins up the docker compose

  ./run_ansible.sh

How to run the code with Github Actions as the controller machine

Navigate to the Scripts folder

    cd Scripts

Remove the ansible installation block.Run the bash installation script. This script installs Terraform and ssh pass to your laptop.

    ./installations.sh
    

Push the code to github. Ensure you add the following to github secrets:

    MONGO_INITDB_DATABASE=admin
    MONGO_INITDB_ROOT_USERNAME=root
    MONGO_INITDB_ROOT_PASSWORD=example
    MONGODB_CONNSTRING=mongodb://root:example@mongo:27017/test?directConnection=true&authSource=admin&replicaSet=replicaset&retryWrites=true

Copy the private key generated to github secrets.

    SSH_PRIVATE_KEY=<copy-here>

View the private key using the following command


cat $HOME/.ssh/microservicekeys/id_rsa
 

Final github secrets should be as follows: building-microservices-with-terraform-ansible-docker-dockercompose-and-github-actions

See the open issues for a full list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Tweet me at- @wamaithanyamu or shoot me an email at hello@wamaithanyamu.com

Project Link: https://github.com/wamaithanyamu/building-microservices-with-terraform-ansible-docker-dockercompose-and-github-actions

(back to top)