Skip to content

Commit

Permalink
Update image ID in launch template with instance refresh
Browse files Browse the repository at this point in the history
* Modifies the Lambda that triggers an instance refresh, to first update
  the image ID to the latest AMI. This ensures the instances will be
  using the most up to date AMI
* This has been placed within the instance refresh lambda, because it
  will require an instance refresh anyway to actually update the
  instances, and allows it to happen at the chosen instance refresh
  cron
* The AutoScaling group's launch template version has also been changed
  to '$Latest', rather than a specific version so that we don't need to
  keep that up to date with the latest version
  • Loading branch information
Stretch96 committed Jan 6, 2025
1 parent 9d3dca5 commit 2443841
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ecs-cluster-infrastructure-instance-refresh-lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ resource "aws_lambda_function" "ecs_cluster_infrastructure_instance_refresh" {

environment {
variables = {
asgName = aws_autoscaling_group.infrastructure_ecs_cluster[0].name
asgName = aws_autoscaling_group.infrastructure_ecs_cluster[0].name
launchTemplateName = aws_launch_template.infrastructure_ecs_cluster[0].name
amiVersion = local.infrastructure_ecs_cluster_ami_version
}
}

Expand Down
2 changes: 1 addition & 1 deletion ecs-cluster-infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ resource "aws_autoscaling_group" "infrastructure_ecs_cluster" {

launch_template {
id = aws_launch_template.infrastructure_ecs_cluster[0].id
version = aws_launch_template.infrastructure_ecs_cluster[0].latest_version
version = "$Latest"
}

vpc_zone_identifier = local.infrastructure_ecs_cluster_publicly_avaialble ? [
Expand Down
38 changes: 38 additions & 0 deletions lambdas/ecs-asg-instance-refresh/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,47 @@
import os

asgName = os.environ['asgName']
launchTemplateName = os.environ['launchTemplateName']
amiVersion = os.environ['amiVersion']

def lambda_handler(event, context):
asgClient = boto3.client('autoscaling')
ec2Client = boto3.client('ec2')

# Update launch template to use the latest AMI
response = ec2Client.describe_images(
Owners=['amazon'],
Filters=[
{'Name': 'name', 'Values': [amiVersion]},
{'Name': 'state', 'Values': ['available']},
{'Name': 'architecture', 'Values': ['x86_64']}
]
)

images = sorted(
response['Images'],
key=lambda x: x['CreationDate'],
reverse=True
)
if not images:
raise Exception("No AMIs found!")

latest_ami_id = images[0]['ImageId']
print(f"Latest ECS-optimized AMI: {latest_ami_id}")

try:
response = ec2Client.modify_launch_template(
LaunchTemplateName=launchTemplateName,
LaunchTemplateData={
'ImageId': latest_ami_id
}
)
print(f"Launch template updated successfully: {response}")
except Exception as e:
print(f"Error updating launch template: {e}")
raise

# Start instance refresh
try:
response = asgClient.start_instance_refresh(
AutoScalingGroupName=asgName,
Expand Down

0 comments on commit 2443841

Please sign in to comment.