CronJob, django and environment variables - django

I have built an application for django on Openshift v3 PRO with the django-ex template. It works great. I'm using POSTGRESQL with persistent storage.
I need a scheduled cron job to fire every hour to run some django management commands. I'm using the CronJob pod for this.
My problem is this: I need to create the CronJob job with the same environment variables that the django pod was created with (DATABASE_, DJANGO_, and others), but don't see an easy way to do this.
Any help would appreciate it.

You should be able to include a list of environment variables to set as part of the containers definition in the template spec for the job. I can't properly extract the resource definition for a CronJob using oc explain in OpenShift 3.6 because of the way it is registered, but I would expect the field to be similar to:
CronJob.spec.jobTemplate.spec.template.spec.containers.env
RESOURCE: env <[]Object>
DESCRIPTION:
List of environment variables to set in the container. Cannot be updated.
EnvVar represents an environment variable present in a Container.
FIELDS:
name <string> -required-
Name of the environment variable. Must be a C_IDENTIFIER.
value <string>
Variable references $(VAR_NAME) are expanded using the previous defined
environment variables in the container and any service environment
variables. If a variable cannot be resolved, the reference in the input
string will be unchanged. The $(VAR_NAME) syntax can be escaped with a
double $$, ie: $$(VAR_NAME). Escaped references will never be expanded,
regardless of whether the variable exists or not. Defaults to "".
valueFrom <Object>
Source for the environment variable's value. Cannot be used if value is not
empty.

Related

Lambda ValueFrom Environment Variable Like in Task Definition

Is there a way to have a ValueFrom feature in Lambda's environment variable similar to what we have in Task Definition?
How it works.
We have a kv pair in parameter store /dev/db/host=localhost.
In the container definition inside the ECS task definition, we add a new environment variable DB_HOST which has a ValueFrom /dev/db/host. When a new instance of the container is run it will have the value localhost from the parameter store.
I tried on Lambda but it seems like this feature is not available. Is there another way to do this? I wonder if there is a request for this as well.
PS: I'm aware that it can be done via TerraForm or CloudFormation but that will only evaluate and copy the values from parameter store to Lambda environment variables when the infrastructure is built. The problem is some of the values are secured like DB password, thus it cannot be simply copied as it will get exposed.

Add new environment variables to Lambda using Cloud Formation Template

I have a nested Cloud Formation Template (multiple templates within a root template )to create a complete web application.
Lambda is created in the first template and few environment variables are added to it.
The later part of the templates also produces some values that has to be added as environment variables.
Is there a way to attach these environment variables to the existing lambda function?
I don't think so, but there are a few options. If you could change the stack dependency order, you could build the stack creating the values depended upon first. If you cannot, you can store your environment variables in SSM Parameter Store as mentioned in this knowledge center article.
So you set the environment variable to a path where the value can be expected, then when creating the stack that knows the value, you store it at that path. When the lambda runs, you just do get parameter.

Global environment variables for AWS CloudFormation

Is there a way to have global environment variables in a AWS CloudFormation yaml file for Lambdas?
Currently we are using the SSM Parameter Store for global variables, but we don't want to use that anymore.
I looking forward to have something like this:
Environment:
Variables:
variable1: xxx // local variables
variable2: xxx
...
${file(./globalvariables.yml)} // global variables
Or even better: every lambda is including the global environment variables as default without explicitly calling it.
Is this possible? Or what approach would you suggest? Thanks in advance!
Sadly I'm unaware of having predefined defaults for environment variables to be set through CloudFormation for Lambdas however - One possible option is instead of using env variables in CloudFormation add a lambda layer with all the config and pull the values from there.
Benefits of this are that if a value changes you only have to update your layer once then update lambdas to use new layer which could be a single parameter instead of manually updating every single one.
Docs here: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
Another option would be to use AWS Secrets Manager Or SSM Parameter Store as ServerMonkey suggested.

Template syntax in compose file Docker

Can we use template in Docker compose file YML?
For example, I want deploy service with replicated and I want set name for container like: -servicename-_-replicId-
Short answer: yes, and it's called interpolation or variable substitution in their context: https://docs.docker.com/compose/compose-file/#variable-substitution
A bit more details: You can interpolate variable values from environment variables, but can also provide defaults in case the environment doesn't contain the necessary variable.
An example taken from the official docs looks like this:
db:
image: "postgres:${POSTGRES_VERSION}"
Now regarding your actual use case to name a container: the container name stems from a variable key and not from a variable value. So you'll have to use the container_name property to explicitly override the generated container name. See the example above: db would be the generated container name, but db isn't a property value. So to make your use case work, you should try this:
db:
container_name: "app_${CONTAINER_NAME_SUFFIX}"
There is a new project related to docker-compose templating problematics called octo-compose.
In this projects there is included:
Templating by defining custom variables,
templating by using built-in variables like instance id, ports from range, ...,
running host preparation bash scripts,
recursive octo-composes inclusion from another repo or directory,
support for Docker Swarm deployments.

Obtain VCAP_APPLICATION's application_name value on Spring's application.yml

I'm trying to set a logging pattern using 'logging.pattern.console' that needs to include the CloudFoundry's application name of a given application. I know that application names can be found as part of the VCAP_APPLICATION env variable with the 'application_name' key, and I can resolve env variables on Spring Cloud applications using the standard Spring placeholder notation, available on the application.yml file; but as the variable is a Json, I can't parse it nor use SpEL to obtain the requested value only.
Is there any other way to obtain the application name as set on the manifest.yml file in the application.yml?
If you are using Spring Boot, you can access the application name with the property vcap.application.name. You should be able to reference this anywhere that properties are available, like #Value annotations or in application.properties.
Spring Boot's CloudFoundryVcapEnvironmentPostProcessor takes the VCAP_SERVICES & VCAP_APPLICATION environment variables and makes them available as properties through Spring's Environment api. This should happen automatically, no config or work necessary.