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:
- Get the physical resource id from cloudformation
- Ask lambda about the details of our function configuration. Find the variables section.
- Push our targeted values into the environment with
export
- 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:
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.