-
-
Save masudur-rahman-niloy/7492fddfa60b54da02afd0f52c63b18c to your computer and use it in GitHub Desktop.
Get all env variables for all AWS Lamda functions in a CloudFormation/SAM stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 | |
import argparse | |
parser = argparse.ArgumentParser( | |
prog='get-vars', | |
description='This function gets environment variable datas from lambda functions in a cloudformation stack' | |
) | |
parser.add_argument('--region') | |
parser.add_argument('--profile', default='default') | |
parser.add_argument('--stack_name', required=True) | |
parser.add_argument('--output') | |
parser.add_argument( | |
"--nested", | |
action="store_true", | |
help="Check in nested stacks" | |
) | |
args = parser.parse_args() | |
stacks = dict() | |
merged = dict() | |
stack_name = args.stack_name | |
profile = args.profile | |
output = args.output | |
nested = args.nested | |
print(f'Using profile: {profile}') | |
my_session = boto3.session.Session(profile_name=profile) | |
if args.region: | |
my_session = boto3.session.Session(profile_name=profile, region_name=args.region) | |
cloudformation_client = my_session.client('cloudformation') | |
lambda_client = my_session.client('lambda') | |
# Grab stack from AWS | |
def list_stack_resources(stack_name): | |
global merged | |
page = {} | |
try: | |
stack_resources = cloudformation_client.list_stack_resources( | |
StackName=stack_name | |
) | |
except Exception as e: | |
print(e) | |
sys.exit() | |
print(f"started stack: {stack_name}") | |
function_count = 0 | |
# Grab environment vars from each lambda in the stack | |
for resource in stack_resources['StackResourceSummaries']: | |
if (resource['ResourceType'] == 'AWS::CloudFormation::Stack') and nested: | |
nested_stack = resource.get('PhysicalResourceId').split('/')[1] | |
list_stack_resources(nested_stack) | |
if (resource['ResourceType'] == 'AWS::Lambda::Function'): | |
function_count += 1 | |
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'] | |
merged = {**merged, **lambda_config['Environment']['Variables']} | |
print(f"completed stack: {stack_name}") | |
print(f"function_count: {function_count}") | |
if page != {}: | |
stacks[stack_name] = page | |
list_stack_resources(stack_name) | |
# print(json.dumps(stacks, indent=2)) | |
print(json.dumps(merged, indent=2)) | |
if output is not None: | |
f = open(output, "w") | |
f.write(json.dumps({'Parameters': merged}, indent=2)) | |
f.close() | |
print("Write successful") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment