Skip to content

Instantly share code, notes, and snippets.

@singledigit
Last active April 12, 2023 14:13
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save singledigit/3ac5ae997c2455f0dc77119d86478a85 to your computer and use it in GitHub Desktop.
Get all env variables for all AWS Lamda functions in a CloudFormation/SAM stack
#! /usr/bin/env python
# Use at your own risk and reward.
# requires boto3 to be installed
# example `./get-vars.py MyStack > vars.json`
import sys, json
import boto3
if len(sys.argv) == 1:
print('CloudFormation stack required')
sys.exit()
cloudformation_client = boto3.client('cloudformation')
lambda_client = boto3.client('lambda')
page = {}
# Grab stack from AWS
try:
stack_resources = cloudformation_client.list_stack_resources(
StackName=sys.argv[1]
)
except Exception as e:
print(e)
sys.exit()
# Grab environment vars from each lambda in the stack
for resource in stack_resources['StackResourceSummaries']:
if(resource['ResourceType']=='AWS::Lambda::Function'):
lambda_config = lambda_client.get_function_configuration(
FunctionName=resource['PhysicalResourceId']
)
if 'Environment' in lambda_config and 'Variables' in lambda_config['Environment']:
page[resource['LogicalResourceId']] = lambda_config['Environment']['Variables']
# Print to console
print(json.dumps(page, indent=2))
@singledigit
Copy link
Author

I use theis script to generate my environment file for using AWS SAM sam local invoke -n environmant-vars.json. As described here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-invoke.html#serverless-sam-cli-using-invoke-environment-file

@masudur-rahman-niloy
Copy link

masudur-rahman-niloy commented Jul 15, 2021

Hi Eric,

Can you see my fork, I've made some changes for using profile names as a second argument. It will also work if the stack has nested stack. So if you pass the root stack's name it will check if the stack has nested stacks and will pull the information of the nested stack. The link is here

https://gist.github.com/masudur-rahman-niloy/7492fddfa60b54da02afd0f52c63b18c

@sakibguy
Copy link

@singledigit
Copy link
Author

Ahhh nice @masudur-rahman-niloy !! Thank you!

@mfilipelino
Copy link

I was on a youtube channel. like that tool, congrats!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment