Skip to main content

Command Palette

Search for a command to run...

Terraform Day 9: Terraform Modules with GitLab

Updated
โ€ข3 min read
Terraform Day 9: Terraform Modules with GitLab
K

"Hello, I'm Kiran Pawar, a passionate Cloud and Devops Engineer with a strong background in cloud automation, configuration, and deployment. My journey in the world of technology has been a thrilling adventure, where I've had the privilege to work with cutting-edge tools and practices.

๐Ÿš€ As a DevOps Engineer:

I specialize in automating, configuring, and deploying instances in cloud environments and data centers. My expertise extends to DevOps, GitOps, CI/CD pipeline management, HashiCorp Terraform, and containerization. I'm proficient in AWS and Linux/Unix administration, ensuring robust infrastructure and application performance.

๐Ÿ”ง My Tech Stack:

Front-end skills: HTML, CSS, SCSS, Tailwind CSS, Bootstrap, React, Material-UI, JavaScript DevOps toolbox: GIT, OWASP,Nexus,Trivy, Github, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus, Grafana, Argo CD, AWS EKS.

๐ŸŒ My Cloud Expertise:

I have hands-on experience managing AWS services, including EC2, S3, EBS, VPC, ELB, RDS, IAM, Route53, and more.

๐Ÿ”’ Networking and Security:

My skills include managing networking concepts such as TCP/IP protocols, security policies, and subnet interfacing. I have a strong understanding of infrastructure and networking, covering topics like firewalls, IP addressing, DNS, and more.

๐Ÿ’ก What Sets Me Apart:

I bring a positive attitude, a strong work ethic, and a collaborative spirit to every project. I'm a self-starter, a fast learner, and an effective team player with strong interpersonal skills. In addition to my DevOps skills, I've developed shell scripts (Bash) for automating tasks and have proficiency in Python scripting. My ability to communicate and manage projects, along with a track record of resolving client issues, adds value to every team I work with. If you're looking for a DevOps engineer who is also well-versed in front-end technologies, feel free to connect with me. Let's explore new possibilities and create exceptional technical solutions together!"

Complete Source file here :https://gitlab.com/imkiran13/terraform-modules-cicd.git

GitLab CI Configuration

  1. Access CI/CD Settings:

    • Navigate to your project, then go to Settings > CI/CD.
  2. Upload Secure Files:

    • Under the "Secure Files" section, upload your PEM file.

  3. Add CI/CD Variables:

    • Scroll to "Variables" and click "Add."

    • Add the following masked variables:

      • AWS_ACCESS_KEY_ID

      • AWS_SECRET_ACCESS_KEY

  4. Set Up a New GitLab Runner:

    • Navigate to Runners and select "New project runner."

    • Choose "Linux" and set the following:

      • Tags: Terraform,AWS

      • Description: A brief description of your runner.

      • Timeout: 600 seconds.

    • Click "Create Runner

      .

  5. Using tfenv

To manage different Terraform versions easily, we will use tfenv. Follow these steps:

  1. Install tfenv:

    • Follow the instructions available on the tfenv GitHub page.

    • Manual

      1. Check out tfenv into any path (here is ${HOME}/.tfenv)
        git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
  1. Add ~/.tfenv/bin to your $PATH any way you like

bash:

        echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile

3.On Ubuntu/Debian touching /usr/local/bin might require sudo access, but you can create ${HOME}/bin or ${HOME}/.local/bin and on next login it will get added to the session $PATH or by running . ${HOME}/.profile it will get added to the current shell session's $PATH.

        mkdir -p ~/.local/bin/
        . ~/.profile
        ln -s ~/.tfenv/bin/* ~/.local/bin
        which tfenv

  1. Install the Required Terraform Version:

     sudo apt install unzip
     tfenv list-remote  # Lists all available versions
     tfenv install 1.5.5 # Installs the specified version
    

Installing GitLab Runner

  1. Install GitLab Runner:

    • Open your console and follow the installation commands provided on the GitLab Runner page

      .

  2. Register the Runner:

    • Enter the token and name for the runner, choose "shell" as the executor

      .

  3. Modify Your Code and Push:

    • Make minor changes to your code and push it. This should trigger the CI/CD pipeline.
  4. Run Commands as gitlab-runner:

     cat /etc/passwd
     sudo rm -r /home/gitlab-runner/.bash_logout
     su - gitlab-runner  # Switch to gitlab-runner user
    

Deploying an Ubuntu Server

Log into the server and deploy the necessary infrastructure using your Terraform scripts.

Cleaning Up

To destroy the infrastructure, run:

terraform destroy -auto-approve

You can use Checkov, a free tool, to scan your Terraform code for security issues:

apt install -y python3-pip

Troubleshooting

If you encounter errors:

  • Check the GitLab CI/CD pipeline logs for error messages.

  • Google any error codes for potential solutions.

Conclusion

This setup provides a streamlined approach to managing infrastructure with Terraform in a GitLab CI/CD environment. Feel free to customize the configurations as needed to fit your specific requirements.

For further assistance, refer to the official Terraform documentation or GitLab CI/CD documentation.

More from this blog

Kiran Pawar's Blog

122 posts