Transmitting a secret to Yandex Serverless Containers
Note
This feature is in the Preview stage.
To provide the container with access to the secret, specify a service account with the following roles in the container parameters:
lockbox.payloadViewerto access the secret (see Granting permissions to access a secret).kms.keys.encrypterDecrypterto access the encryption key if the secret was created using a Yandex Key Management Service key (see Granting permissions to access an encryption key).
A Lockbox secret transmitted to a container is cached in Serverless Containers. As soon as the service account can no longer access the secret, the container may store it for up to 5 minutes.
A new revision of a container is created when Yandex Lockbox secrets are transmitted. You cannot transmit secrets to an existing revision.
-
In the management console
, go to the folder with your container. -
Open Serverless Containers.
-
Select a container you want to transmit a secret to.
-
Go to the Editor tab.
-
In the window that opens, under Image settings, in the Lockbox secrets field, specify:
- Name of the environment variable to store the secret.
- Secret ID.
- Secret version ID.
- Key of one of the key-value pairs in the secret version.
-
Click Add.
You can transmit multiple secrets to a container. To do this, click Add.
-
Click Create revision. A new container revision with the specified secrets will be created.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To transmit Yandex Lockbox secrets to a container, run this command:
Warning
If the secrets were already sent to the previous revision, they will be overwritten.
yc serverless container revision deploy \
--container-name test \
--image cr.yandex/<registry_ID>/repository:tag \
--cores 1 \
--memory 1GB \
--service-account-id <service_account_ID> \
--secret environment-variable=<environment_variable_name>,id=<secret_ID>,version-id=<secret_version_ID>,key=<secret_key>
Where:
-
--container-name: Container name. -
--image: Docker image URL. -
--cores: Number of cores available to the container. -
--memory: Required memory. The default value is 128 MB. -
--service-account-id: ID of the service account with thelockbox.payloadViewerrole. -
--secret:environment-variable: Name of the environment variable that will store the secret.id: Secret ID.version-id: Secret version ID.key: Key of one of the key-value pairs in the secret version.
You can transmit multiple secrets to a container. To do this, specify the
--secretparameter as many times as needed.
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
-
Open the Terraform configuration file and add the
secretssection to the function description:resource "yandex_serverless_container" "test-container" { name = "<container_name>" memory = <memory_size> service_account_id = "<service_account_ID>" secrets { id = "<secret_ID>" version_id = "secret_version_ID>" key = "<secret_1_key>" environment_variable = "<environment_variable_1_name>" } secrets { id = "<secret_ID>" version_id = "<secret_version_ID>" key = "<secret_2_key>" environment_variable = "<environment_variable_2_name>" } image { url = "<Docker_image_URL>" } }Where:
secrets: Section with secret settings. It contains the following parameters:id: Secret ID. This is a required parameter.version_id: Secret version ID. This is a required parameter.key: Key of one of the secret version's key-value pairs that will be stored in the environment variable. This is a required parameter.environment_variable: Name of the environment variable that will store the secret. This is a required parameter.
For more information about the
yandex_serverless_containerresource parameters, see the provider documentation. -
Apply the changes:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
-
You can check the function's update settings in the management console
To transmit a Yandex Lockbox secret to a container, use the deployRevision REST API method for the Container resource or the ContainerService/DeployRevision gRPC API call.