Como puedo prender y apagar automáticamente instancias Amazon Ec2 por demanda basado en horarios.

Servicios que usaremos en AWS

  • Amazon Lambda
  • Amazon cloud Watch
  • Event Rules

1. Creamos una Política IAM.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}

2. Creamos un rol IAM para Lambda

3. Creamos la Lambda

import boto3
region = 'us-east-1'
instances = ['i-0cff3a00763a57d01']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
import boto3
region = 'us-east-1'
instances = ['i-0cff3a00763a57d01']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))

4. Creación de Reglas en Cloud Watch.

30 9 ? * 1-7 *
50 4 ? * 1-7 *

¿He realizado todos los pasos del Blog pero no funciona?

  • Revise siempre los Logs de la función Lambda pues le permitirá saber cuál puede ser la falla.

--

--

Entrepreneur, travel lover, AWS x3, CIO High Cloud Tec , AWS Community Builder, passionate about cloud learning

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Oscar Ivan Ocampo

Oscar Ivan Ocampo

35 Followers

Entrepreneur, travel lover, AWS x3, CIO High Cloud Tec , AWS Community Builder, passionate about cloud learning