Skip to content

Instantly share code, notes, and snippets.

@rikvanderkemp
Created September 21, 2022 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikvanderkemp/d8a837eb1d09d5f2392c64eb4160b07b to your computer and use it in GitHub Desktop.
Save rikvanderkemp/d8a837eb1d09d5f2392c64eb4160b07b to your computer and use it in GitHub Desktop.
Loading multiple parameters from the AWS parameter store and using them in terraform

Let's say you have the following parameters defined in the AWS parameter store:

/production/worker/SOME_PARAM_1
/prodcution/worker/SOME_PARAM_2

Instead of using multiple data blocks you can use aws_ssm_parameters_by_path to load them all at once. \ But using them is another thing. To be able to use them easily, you can remap this data into your own object.

It looks something like this:

data "aws_ssm_parameters_by_path" "my_params" {
  path      = "/production/worker/"
  recursive = true
}

locals {
  production-worker-params = {
  for k, v in data.aws_ssm_parameters_by_path.my_params.names :
  trimprefix(v, "/production/worker/") => data.aws_ssm_parameters_by_path.my_params.values[k]
  }
}

You can now use this in your resource like this:

resource "aws_elastic_beanstalk_environment", "something" {
    setting {
        name = "SOME_PARAM_1" # The env variable to set
        namespace = "aws:elasticbeanstalk:application:environment"
        value = local.production-worker-params["SOME_PARAM_1"] # this comes from our freshly brewed map
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment