Issue
- For business reasons, we have a custom service that we occasionally need to delete and recreate with a new deployment. However, when we delete the service, all secret variables previously attached to this service (using the UI) are no longer injected when the service is recreated, requiring us to go in and reattach each of the secrets.
- Is there a way to persist secrets across a deletion and recreation event for a service?
Environment
- Liferay PaaS
Resolution
The following steps and syntax leveraging the LCP.json can be used to ensure that a secret is injected into a service when that service is created (or even deployed, if attaching a new secret):
- Ensure that a secret has already been created within an environment, and is visible when viewing the "Secrets" section of the environment's Settings page (see: Adding a New Secret)
Note: This secret does not need to currently be attached to any services under "Used in". - Edit or add the env section in the service's LCP.json to include the following variable syntax:
"DESIRED_VARIABLE_KEY" : "@nameofexistingsecret"
For example, you can reference a secret named "examplename" in the LCP.json with this:"env": { "EXAMPLE_KEY": "@examplename" }, - Create a build including these changes, then deploy that build to the environment.
When doing so, even if the secret was not previously attached to the service, the value saved in the examplename secret will be injected into the service during its startup process using the Key set in the LCP.json.
Additional Information
The following official documentation also contains related helpful information: