Hephaestus
Hephaestus

Reputation: 5103

Using Terraform to load a single file from git repo

We want to load a file from a git repo and place it into a parameter store. The file contains configuration data that is custom to each of several organizational-accounts, which are being constructed with Terraform and are otherwise identical. The data will be stored in AWS SM Parameter Store. For example the Terraform code to store a string as a parameter is:

resource "aws_ssm_parameter" "parameter_config" {
  name  = "config_object"
  type  = "String"
  value = "long json string with configuration data"
}

I know there is a file() operator (reference) from Terraform and I know that TF can load files from remote git repos, but I'm not sure if I can bring all this together.

Upvotes: 5

Views: 6337

Answers (1)

Highway of Life
Highway of Life

Reputation: 24411

There are a few ways that you can do this.

The first would be to use the github provider with the github_repository_file data source:

terraform {
  required_providers {
    github = {
      source = "integrations/github"
      version = "5.12.0"
    }
  }
}

provider "github" {
  token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  owner = "org-name"
}

data "github_repository_file" "config" {
  repository          = "my-repo"
  branch              = "master"
  file                = "config-file"
}

resource "aws_ssm_parameter" "parameter_config" {
  name  = "config_object"
  type  = "String"
  value = data.github_repository_file.config.content
}

You could also do this with the http provider:

data "http" "config" {
  url = "https://raw.githubusercontent.com/my-org/my-repo/master/config-file"
}

resource "aws_ssm_parameter" "parameter_config" {
  name  = "config_object"
  type  = "String"
  value = data.http.config.response_body
}

Keep in mind that you may get multiline string delimiters when using the http data source method. (e.g.: <<EOT ... EOT)

Upvotes: 5

Related Questions