Skip to content
Docs

This guide stores a secret in AWS Secrets Manager, copies it into a Kubernetes Secret with External Secrets, and passes it to a container as environment variables.

The secret value lives only in AWS Secrets Manager, so it never has to be written into a manifest or a Git repository. You add an ExternalSecret manifest that points to the secret; External Secrets reads it through a ClusterSecretStore, creates the Kubernetes Secret and keeps it up to date when the value in AWS changes.

It is good practice to include the ExternalSecret manifest in your application’s Helm chart, so the Secret is deployed together with the application that uses it.

This is not a platform API. It needs only the external-secrets module:

Module Flag Default What it does
external-secrets global.createClusterSecretStore false Creates a ClusterSecretStore for AWS Secrets Manager. Set to true.
external-secrets global.enableWriteForClusterSecretStore false Adds write permissions to the store. By default it can only read secrets.
aws/kms — — Recommended, not required. Creates the kms/config key to encrypt secrets with.

The ClusterSecretStore is named after the external-secrets module name. The examples below use external-secrets, which only works if the module has that name. If your module is named, for example, external-secrets-dev, use external-secrets-dev in secretStoreRef.name.

For the base modules, see Infralib Requirements. A full example is in Infralib Configuration Example at the end of this guide.

1. Create the secret in AWS Secrets Manager

Section titled “1. Create the secret in AWS Secrets Manager”

Create the secret in the AWS Console or with the AWS CLI. Encrypt it with the kms/config key created by the Infralib KMS module. For more information, see the AWS Secrets Manager User Guide.

The examples store this data:

EXAMPLE_USERNAME=admin
EXAMPLE_PASSWORD=verySecret123
Terminal window
aws secretsmanager create-secret \
--name example-secret \
--secret-string '{"EXAMPLE_USERNAME":"admin","EXAMPLE_PASSWORD":"verySecret123"}' \
--kms-key-id alias/dev-net-kms/config
  1. Open Secrets Manager and click Store a new secret.

  2. Select Other type of secret, fill in the Key/value pairs and choose the kms/config key as the Encryption key. Click Next.

  3. Set the Secret name and click Next.

  4. Check that the secret exists in AWS Secrets Manager.

You can view and edit existing secrets in the AWS Console:

External Secrets reads secrets through a ClusterSecretStore or a SecretStore. List the available stores to find the name to use:

Terminal window
$ kubectl get SecretStores,ClusterSecretStores --all-namespaces
NAMESPACE NAME AGE STATUS CAPABILITIES READY
clustersecretstore.external-secrets.io/external-secrets 1h Valid ReadWrite True

Create an ExternalSecret that copies every key of the AWS secret into a Kubernetes Secret:

apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: example-secret
annotations:
argocd.argoproj.io/sync-wave: '-2'
spec:
refreshInterval: 5m
dataFrom:
- extract:
key: example-secret # AWS Secrets Manager secret
secretStoreRef:
kind: ClusterSecretStore
name: external-secrets # the external-secrets module name
target:
name: example-secret # Kubernetes Secret to create
creationPolicy: Owner

For every field, see the External Secrets documentation.

Load every key of the Secret as an environment variable:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
envFrom:
- secretRef:
name: example-secret

For more information about Secrets in Kubernetes, see Kubernetes documentation.

Terminal window
$ kubectl get secret
NAME TYPE DATA AGE
example-secret Opaque 1 10m

The ExternalSecret and the Secret in ArgoCD:

Symptom Cause / fix
no matches for kind "ExternalSecret" The external-secrets module is not installed. Check the Prerequisites.
kubectl get ClusterSecretStores returns nothing global.createClusterSecretStore is false. Set it to true.
The ExternalSecret refers to a store that does not exist The store is named after the external-secrets module. Use the name shown by kubectl get ClusterSecretStores in secretStoreRef.name.
The Kubernetes Secret is not created kubectl describe externalsecret <name> shows the error under Conditions and Events. Check that dataFrom.extract.key matches the secret name in AWS Secrets Manager.
A changed value in AWS does not show up External Secrets refreshes the Secret every refreshInterval, 5 minutes in the example.
steps:
- name: net
type: terraform
modules:
...
- name: kms
source: aws/kms # recommended, provides kms/config
...
- name: apps
type: argocd-apps
modules:
...
- name: external-secrets # also the ClusterSecretStore name
source: external-secrets
inputs:
global:
createClusterSecretStore: true