Automatically Windows AMI Creation using Eventbridge
Open the IAM service "Create Role " and Choose the Lambda use case and provide the required Permissions "AmazonEC2FullAccess" & Cloudwatch full Access policy for testing, but it's recommended to create a custom policy with the minimum required permissions in a production environment.
-
- Create a Lambda function that will be responsible for creating the AMI & choose a runtime environment for it.
Now In the Lambda function code editor, write the python code to create the AMI
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
instance_id = 'YOUR_INSTANCE_ID'
ami_name = 'YourServer-Backup-' + event['time']
response = ec2.create_image(InstanceId=instance_id, Name=ami_name)
print('Created AMI:', response['ImageId'])
Now Replace "instance id" with your Windows Server's instance ID.
Deploy the Python code and test it by creating an event.Now check Image section Windows AMI has been created
2. Now Open Configure EventBridge (CloudWatch Events )and Create Rule
3 .Click "Create rule." and Choose "Event Source" and select "Schedule." and Configure the cron expression to run every Monday to friday at 8 PM As per your need.
cron(30 4 ? * MON *)
Now Add the Lambda function you created as a target for the rule.
Make sure that the AMIs are being created as expected every Monday at 8 PM.
With these steps, you'll have an automated process to create Windows Server AMIs on a weekly schedule using AWS EventBridge.