Skip to content

Instantly share code, notes, and snippets.

@nmagee
Created April 17, 2026 15:59
Show Gist options
  • Select an option

  • Save nmagee/e787963043844e0b441591a2a8aef832 to your computer and use it in GitHub Desktop.

Select an option

Save nmagee/e787963043844e0b441591a2a8aef832 to your computer and use it in GitHub Desktop.
Work with Lambda timers and EC2 instances

Lambda + EC2

You can describe instances using the CLI:

aws ec2 desribe-instances

and with jq you can deserialize the JSON in your terminal. For instance if you wanted a list of all your InstanceIds:

aws ec2 describe-instances | jq -r '.Reservations.[].Instances.[].InstanceId'

With boto3 you can do the same, and filtering becomes simpler and more Pythonic:

import boto3

ec2 = boto3.client('ec2')
response = ec2.describe_instances()
print(response)
import boto3
ec2 = boto3.client('ec2')
response = ec2.describe_instances(
# Filters=[
# {'Name': 'tag:business_hours_only', 'Values': ['True']},
# {'Name': 'instance-state-name', 'Values': ['running', 'stopped']}
# ]
)
instance_ids = [
instance['InstanceId']
for reservation in response['Reservations']
for instance in reservation['Instances']
]
print(instance_ids)
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CloudWatchLogs",
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Sid": "EC2Describe",
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus"
],
"Resource": "*"
},
{
"Sid": "EC2StartStop",
"Effect": "Allow",
"Action": [
"ec2:StartInstances",
"ec2:StopInstances"
],
"Resource": "arn:aws:ec2:*:*:instance/*"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment