How to execute shell commands from within a Kubernetes ConfigMap?

12/8/2018

I am using Helm charts to create and deploy applications into my K8s cluster.

One of my pods requires a config file with a SDK key to start and function properly. This SDK key is considered a secret and is stored in AWS Secret Manager. I don't include the secret data in my Docker image. I want to be able to mount this config file at runtime. A ConfigMap seems to be a good option in this case, except that I have not been able to figure out how to obtain the SDK key from Secrets Manager during the chart installation. Part of my ConfigMap looks like this:

   data:
      app.conf: |
      [sdkkey] # I want to be able to retrieve sdk from aws secrets manager

I was looking at ways to write shell commands to use AWS CLI to get secrets, but have not seen a way to execute shell commands from within a ConfigMap.

Any ideas or alternative solutions?

Cheers K

-- Karthik Balasubramanian
aws-secrets-manager
kubernetes
kubernetes-helm

1 Answer

12/8/2018

tl;dr; You can't execute a ConfigMap, it is just a static manifest. Use an init container instead.

ConfigMaps are a static manifest that can be read from the Kubernetes API or injected into a container at runtime as a file or environment variables. There is no way to execute a ConfigMap.

Additionally, ConfigMaps should not be used for secret data, Kubernetes has a specific resource, called Secrets, to use for secret data. It can be used in similar ways to a ConfigMap, including being mounted as a volume or exposed as environment variables within the container.

Given your description it sounds like your best option would be to use an init container to retrieve the credentials and write them to a shared emptyDir Volume mounted into the container with the application that will use the credentials.

-- chaosaffe
Source: StackOverflow