Inject Secrets to Deployment/Pod as Environment variable from AWS Secret Manager

Hi, I have an EKS cluster running on my AWS, currently I am passing credentials/sensitive information as secret and use as environment variable in deployment yaml. Now we are trying to move on to use AWS Secret Manager. Therefore we have put credentials in the AWS Secret Manager.

I want to know how can I pass those AWS Secret Manager key value to Deployment/Pods as environment variables.

Few solutions, I came across are CSI driver but it uses Volumes/Volume mount then set as environment variable (Wouldn’t like to use it don’t want to use Volumes/volume mount to pass secrets), External Secrets has a con that if we update the Secret in AWS Secret Manager then we have to again apply all the secrets and restart deployment again.

Please correct me if I am wrong somewhere. Thanks in advance

I use terraform for this. Refer to the aws secret as a data source and use kubectl_manifest resource to kubectl apply the deployments and inject the terrafrom data source (aws secret) as env variables in the yaml.

https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/kubectl_path_documents

We did a solution based on external secrets(https://github.com/external-secrets/kubernetes-external-secrets) and stakater reloader (https://github.com/stakater/Reloader)

External secrets enables you to map secrets from aws secrets manager to native kube secrets, it also updates the k8s secrets when it is updated on AWS. While reloader will watch out for changes on config maps/secrets and restart said deployments accordingly.

Instructions to setup should be pretty straight forward from the repos themselves :slight_smile: feel free to comment or pm me if you have additional questions.

Good luck!

(Wouldn’t like to use it don’t want to use Volumes/volume mount to pass secrets)

Can you explain why not? The CSI Driver is a very nice solution.

Initially I tried using CSI driver, but since in the CSI method the secrets are mounted as a file. Applications are designed to search in the ENV not from files.

(plus read a medium article where it was mentioned reading configuration from environment variable, is a best practice recommended by the Twelve-Factor App methodology) .

The 12Factor app is not something you must adhere to exactly as they are written. They’re recommendations, not literal requirements, and you can achieve them in gradations.

The Secrets CSI Driver is perfectly capable of taking an external cloud provider secret and converting that into an env var for your pod. The volume mount is required as part of that process, but your app does not need to know or care about the file. Even so, the use of these files if you wanted to use them ‘as is’ you aren’t violating 12factor app principles. The key thing that the 12factor site is trying to get across there is to separate your code from its deployment specific configuration so that you have increased portability and flexibility.

Define your SecretProviderClass.

apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: myapp-secretsprovider
spec:
provider: aws
parameters:
objects: |
- objectName: /myapp/service-password
objectType: ssmparameter
objectAlias: service-password
pathTranslation: False
secretObjects:
- secretName: myapp-aws-secrets
type: Opaque
labels:
app: myapp
data:
- key: service-password
objectName: service-password

In the above code, the parameters contains the mapping of the actual SSM ParameterStore object from AWS to an object alias. This is what will show up in the volume mount as a file. The secretObjects then tells the CSI Driver to create an ephemeral k8s Secret when the SecretProviderClass is mounted to a pod. The volume mount is mandatory for this feature. Its documentation refers to this as a ‘secret synchronization’. (There’s supposedly some way to make it synch into an existing Secret object but I’ve had issues getting that to work)

Next, adjust your Deployment template so that it mounts the volume and then you can map the Secret into the environment configuration.

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: default
labels:
app: myapp
component: api
spec:
selector:
matchLabels:
app: myapp
component: api
template:
metadata:
labels:
app: myapp
component: api
spec:
containers:
- name: app
image: myapp:dev
env:
- name: SERVICE_PASSWORD
valueFrom:
secretKeyRef:
key: service-password
name: myapp-aws-secrets
volumeMounts:
- name: myapp-aws-secrets
mountPath: /var/run/secrets/aws-secrets
readOnly: true
volumes:
- name: myapp-aws-secrets
csi:
driver: secrets-store.csi.k8s.io
readOnly: true
volumeAttributes:
secretProviderClass: myapp-aws-secrets

Voila, you have brought your AWS (or other cloud provider) secrets into K8s and injected it into your pods as an environment variable. When all pods utilizing the volume mount are removed, the corresponding Secret is destroyed.

Refer to sections 2.2, 2.3, and 2.4 of the documentation.

https://secrets-store-csi-driver.sigs.k8s.io/

The idea of volume mount being mandatory thing to do was bothering me. But on the other hand I liked the idea

When all pods utilizing the volume mount are removed, the corresponding Secret is destroyed.

Thanks for the great explanation u/SelfDestructSep2020.