AWS EC2

1. Introduction

AWS EC2, aka Elastic Compute Cloud, essentially offers virtual servers in the cloud that you can use to run your applications.

12. Automate starting and stopping AWS instances

Ref> https://docs.aws.amazon.com/solutions/latest/instance-scheduler-on-aws/solution-overview.html

Instance Scheduler on AWS leverages Amazon Web Services (AWS) resource tags and AWS Lambda to automatically stop and restart instances across multiple AWS Regions and accounts on a customer-defined schedule. This solution also allows you to use hibernation for stopped Amazon EC2 instances.


Step 1: Launch the instance schedule hub stack

https://console.aws.amazon.com/cloudformation/home?region=eu-north-1#/stacks/new?templateURL=https://s3.amazonaws.com/solutions-reference/instance-scheduler-on-aws/latest/instance-scheduler-on-aws.template

[Next]

Stack name: schedulerhub

Instance Scheduler tag name: Schedule (default)

Service(s) to schedule: Both

Schedule Aurora Clusters: Yes

Create RDS instance snapshot: Yes (does not apply to Aurora Clusters)

Scheduling enabled: Yes

Default time zone: Europe/Madrid

This account: Yes

Frequency: 5

Memory size: 128

Namespace: <void>

Use AWS Organizations: No

Enable CloudWatch Metrics: No

Enable CloudWatch Debug Logs: No

Enable SSM Maintenance windows: No

Log retention days: 3 (eg: 1, 3 or 90. Retention days for scheduler logs)

Started tags: InstanceScheduler-LastAction=Started By {scheduler} {year}/{month}/{day} {hour}:{minute}{timezone}, 

Stopped tags: InstanceScheduler-LastAction=Stopped By {scheduler} {year}/{month}/{day} {hour}:{minute}{timezone}, 

[Next]

Tags: [{key=ci, value=infra}]

[Next]

[✓] I acknowledge that AWS CloudFormation might create IAM resources.

[Create change set]

You can view the status of the stack in the AWS CloudFormation console in the Status column. You should receive a CREATE_COMPLETE status in approximately five minutes.

Result: CloudFormation stack stayed w/ status "REVIEW_IN_PROGRESS" when using button [Create change set]. Went to stack, change sets tab and clicked execute.



Step 2 (Optional): Launch the remote stack in secondary accounts

Skipped.


Step 3: Configure schedules and periods

Using only the Amazon DynamoDB Console, so we can easily change it at any time.

The ConfigTable contains several sample records of types 'schedule' and 'period' that can be used as a reference to create custom ones.

Insert period w/ name 'justicia-hours', Monday to Friday from 7:52 to 18:08:

{

  "type": {

    "S": "period"

  },

  "name": {

    "S": "justicia-hours"

  },

  "begintime": {

    "S": "07:52"

  },

  "description": {

    "S": "Justicia hours"

  },

  "endtime": {

    "S": "18:08"

  },

  "weekdays": {

    "SS": [

      "mon-fri"

    ]

  }

}

Insert schedule w/ name 'justicia-hours', Monday to Friday from 7:52 to 18:08:

{

  "type": {

    "S": "schedule"

  },

  "name": {

    "S": "madrid-justicia-hours"

  },

  "description": {

    "S": "Justicia hours in Madrid"

  },

  "periods": {

    "SS": [

      "justicia-hours"

    ]

  },

  "timezone": {

    "S": "Europe/Madrid"

  }

}


Step 4: Tag your instances

Tag the EC2 and RDS resources.

Schedule=madrid-justicia-hours

Tag "InstanceScheduler-LastAction" is updated in the instances whenever the schedule runs.