Easy Access of Cloudformation Lambda Environment Variables.
2023 September 11

Sometimes you want to run your lambda function deployed as a cloudformation stack locally for testing purposes but you don’t want to hard code any of the underlying resources into your environment. These resources might be things like DynamoDB tables, SNS topics or SQS urls. Their names aren’t hard coded, and shouldn’t be hard coded otherwise you have a singleton stack. Sometimes you’re just a bit too impatient to spin everything up using the SAM cli, and don’t want to debug docker issues, so this helps.

So here’s what we know, the cloudformation stack name, the logical id, and a list of variables we want to export for testing purposes into our environment. This makes our game plan look like so:

  1. Get the physical resource id from cloudformation
  2. Ask lambda about the details of our function configuration. Find the variables section.
  3. Push our targeted values into the environment with export
  4. Clean up any names we create for this script in our environment.

So our script will look something like the below, and instead of running it we will source it:

STACK_NAME=your-cfn-stack-name-here
FUNC_RESOURCE_ID=what-your-func-is-named-in-the-template

getLambdaName () {
    aws cloudformation describe-stack-resource --stack-name $STACK_NAME \
        --logical-resource-id $FUNC_RESOURCE_ID \
        --query StackResourceDetail.PhysicalResourceId \
        --output text
}

getFunctionEnv () {
    aws lambda get-function-configuration --function-name $1 --query Environment.Variables --output json
}

x=$(getLambdaName)
my_vars=$(getFunctionEnv $x)

# Customize this section according to your needs
export CACHE_TABLE=$(echo $my_vars | jq .CACHE_TABLE)
export ANALYTICS_TABLE=$(echo $my_vars | jq .ANALYTICS_TABLE)

# Clean it up
unset x my_vars getFunctionEnv getLambdaName

So what did we learn with this solution? We don’t have to complete parse the output of any given AWS cli command, we can use the --query flag to cut it down. You might think it looks like jq expressions, but it’s something called JMESPath. We can also ask for different more useful output formats with the --output flag. There probably isn’t a better way to set each of required environment variables, but just customize it to your needs. It’s not nice because there’s another spot to maintain, but it shouldn’t really matter much as environment variable names don’t tend to change.

It’s unfortunate there isn’t an easy way to push these things in from the AWS cli, and that we have to chain these pieces together. Hopefully you find this script useful for your cloud development needs, I know I find it difficult myself to keep copy and pasting things into my environment and writing those expressions out.


Remember you can also subscribe using RSS at the top of the page!

Share this on → Mastodon Twitter LinkedIn Reddit

A selected list of related posts that you might enjoy:

*****
Written by Henry J Schmale on 2023 September 11
Hit Counter