Skip to content

Instantly share code, notes, and snippets.

@jsbonso
Last active February 11, 2024 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsbonso/a59d9fb6fa80ec9ef261bd50f3f1a4b3 to your computer and use it in GitHub Desktop.
Save jsbonso/a59d9fb6fa80ec9ef261bd50f3f1a4b3 to your computer and use it in GitHub Desktop.
AWS Lambda SSM Parameter Store Updater function
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