Gitlab CI / CD with Terraform + Python

Viewed 548

On my Gitlab CI / CD, I have a terraform code that requires Python installed to use an external module.

When running terraform plan via Gitlab pipelines, I get the following error:

 module.notify_slack.module.lambda.data.aws_caller_identity.current[0]: Refreshing state...
 Error: can't find external program "python3"
   on .terraform/modules/notify_slack.lambda/terraform-aws-lambda-1.6.0/package.tf line 3, in data "external" "archive_prepare":
    3: data "external" "archive_prepare" {
 ERROR: Job failed: exit code 1

What image do I need to use that contains Terraform and Python? Will I need to create my own docker image?

1 Answers

I know this is a bit of an old post, but I'll share my solution in case anyone else stumbles upon this problem too.

Choose an existing python image and install terraform manually - this seems to me to be the easiest solution, if pragmatism is important to you.

This is the relevant section of my .gitlab-ci.yml file:

default:
  image: python:latest
  before_script:
    - python -V  # Display version for debugging purposes only
    - apt-get update -y
    - apt-get install unzip wget -y
    - wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip
    - unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
    - mv terraform /usr/local/bin/
    - terraform --version  # Display version for debugging purposes only

The environment variable was set up in the GitLab CI/CD settings, otherwise just change it for the specific version of Terraform you want.

I was pleasantly surprised at the speed in which this installation takes place, as this clearly isn't an optimal way to do it - the best performing runner will probably use your own custom image with all of your required dependencies pre-installed - I'll leave you to decide whether its worth it for your own purposes. Nonetheless, this solution doesn't appear to be prohibitively slow.

Related