Last active
February 11, 2024 02:25
-
-
Save jsbonso/a59d9fb6fa80ec9ef261bd50f3f1a4b3 to your computer and use it in GitHub Desktop.
AWS Lambda SSM Parameter Store Updater function
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
import boto3, os, json | |
REGION = os.environ['REGION'] | |
ASG_NAME = os.environ['ASG_NAME'] | |
SSM_PARAMETER_NAME = os.environ['SSM_PARAMETER_NAME'] | |
ssm = boto3.client('ssm', region_name=REGION) | |
autoscaling = boto3.client('autoscaling', region_name=REGION) | |
def lambda_handler(event, context): | |
# get new instance id | |
instance_ids = [] | |
asg_response = autoscaling.describe_auto_scaling_groups(AutoScalingGroupNames=[ASG_NAME]) | |
for i in asg_response['AutoScalingGroups']: | |
for k in i['Instances']: | |
instance_ids.append(k['InstanceId']) | |
NEW_INSTANCE_ID = instance_ids[0] | |
# update ssm parameter store value with new instance id | |
ssm_response = ssm.put_parameter( | |
Name=SSM_PARAMETER_NAME, | |
Value=NEW_INSTANCE_ID, | |
Type='String', | |
Overwrite=True | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment