im having some issues with global variables. I have this in my template.yaml
Globals:
Function:
Environment:
Variables:
DEV: true
Then I access those variables in code just with
process.env.DEV
But the problem is that variable does not exist. Only ACCESS_AWS_TOKEN is there. Why is that?
Ok.. everything is a string so... use proper condition.
One of the solutions:
process.env.DEV.toLowerCase() === 'true'
Related
I'm probably missing something very obvious, but I can't find a way of just setting a value that I want to reuse. For instance - I have a sam template that creates a bunch of database tables - I want them to all have the same settings - and I want those settings to depend on whether it's production or not.
so at the moment I do
Resources:
firstTable:
Type: AWS::DynamoDb::Table
...
DeletionPolicy: !If[ isProduction, Retain, Delete ]
secondTable:
Type: AWS::DynamoDb::Table
DeletionPolicy: !If[ isProduction, Retain, Delete ]
in the perfect world, I'd want to say something like "every dynamodb table defined in this template should have this list of settings:" - but I suspect that's not possible, but what I think IS possible - I want to somehow be able to say something like:
somewhere:
deletion_policy_value: !If[ isProduction, Retain, Delete ]
...
firstTable:
Type: AWS::DynamoDb::Table
...
DeletionPolicy: deletion_policy_value
but none of parameters, conditionals globals or environment variables seem to fit - ie I want to define a custom variable that exists only for the life of the template - environment variables seem to exist in the actual cloudformation script - which is NOT what I want (I think)
Sadly its not possible. You would have to develop your own macro to create such substitutions.
On a job level environment variable configuration:
The following works when using parameters passed from another pipleline to my template:
Environment Variables
Name: BRANCH
Value: #{PIPELINE_LEVEL_PARAMETER}_INDEX
However, this does not.
Name: BRANCH
Value: ${STAGE_LEVEL_ENVIRONMENT_VARIABLE}_INDEX
in the latter case, it takes the literal value without replacing with the environment variable
I've only seen examples with single values in SAM templates:
Environment:
Variables:
TABLE_NAME: my-table
I want to do something like this but doesn't seem to work:
Environment:
Variables:
myVar:
- prop1: aaa
prop2: sdfsdfsd
prop3: ssss
- prop1: bbb
prop2: wwwwww
prop3: aaaaa
I want to have an environment variable that is like a list of objects. I could store a delimited string and parse it myself but I'd prefer to have it be like an object/map/list like if I'm ready a YAML file.
The closest you can do is to json encode the value for your environmental variable
and decode it using the runtime language:
Environment:
Variables:
USER: '{"name": "john", "surname": "galt"}'
If you want to prevent decoding json on each request, move your decoding logic outside the handler, in this case code won't be re-executed while lambda is hot.
Any declarations in your Lambda function code (outside the handler code, see Programming Model) remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations. We suggest adding logic in your code to check if a connection exists before creating one.
Read about lambda execution model
I personally would create a json file, store it in s3 bucket and use an environment variable to specify s3 url to that file. Additionally, use the same technique I mentioned above or use even more complicated caching mechanism depending on the situation when retrieving the config file
I have a cloudformation template for my lambda:
Resources:
Resource1:
Type: AWS::Res
Properties:
StreamArn:
"Fn::Sub": "${var1}-${var2}"
Resource2:
Type: AWS::Res
Properties:
StreamArn:
"Fn::Sub": "${var1}-${var2}"
Is it possible to move these properties somewhere to Properties field of Resources section or any other place to avoid duplication?
Resources:
Properties:
StreamArn:
"Fn::Sub": "${var1}-${var2}"
I've tried to do it, but it doesn't work.
You can use a Parameters entry with a default value to create the equivalent to a Constant Variable, but it can't accept any values from the Resources section (since they haven't been created at that point).
Otherwise, no -- you'll need to duplicate the values. (As at the time of writing this answer.)
If you're using AWS::Serverless::Function, you can use Globals section to have common properties in a stack in one place. So, you can put the resources you mentioned in a stack for them and define a Globals section that has StreamArn
See docs
If you are using SAM and the right resources then you can use the globals sections for this:
AWS::Serverless::Function
AWS::Serverless::Api, and
AWS::Serverless::SimpleTable
The Globals section is unique to AWS SAM. It defines properties that
are common to all your serverless functions and APIs. All the
AWS::Serverless::Function
AWS::Serverless::Api, and
AWS::Serverless::SimpleTable
resources inherit the properties that are
defined in the Globals section. For more information about the Globals
section, see Globals Section of the Template in the AWS Serverless
Application Model Developer Guide.
Documentation
The most convenient way that I found so far is to use mapping like:
Mappings:
ParametersMap:
Var1:
Value: "A"
Var2:
Value: "B"
and then put line !FindInMap: [ "ParametersMap", "Var1", "Value" ] in all the places were you need Var1 param
I would like to write the same Ansible template to two different files, one with a value in the file set to True, and the other with a value set to False.
What's the best way to do this? My instinct was to try and pass a value in the template: directive. However, it seems like this is frowned upon.
One way would be to have two different jinja files with almost entirely the same contents; one has the value set to True and the other False.
Another way would be to define a variable, write one template, then use set_fact to change the variable's value, then write the second file. This also seems a little cumbersome.
Another would be to have the template detect what filename it's being rendered as, somehow? And branch in the template based on that.
I must be missing something obvious.
With Ansible 2.x you can use vars: with tasks:
---
- hosts: localhost
tasks:
- template: src=my_template.j2 dest=out1.txt
vars:
name: John
- template: src=my_template.j2 dest=out2.txt
vars:
name: Jane