(c) 2014 Amazon Web Services, Inc. and its afflialtes, All rights reserved.

The content in this file is copied from qwikLABS

- Automating AWS Services with Scripting and the AWS CLI

Please respect the rights.

Putty: a Secure Shell(SSH) client that will provide a command-line interface to my Linux EC2 instance.

http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe

Discover my own public IP address:

http://icanhazip.com/

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the AWS cloud. Using Amazon EC2 eliminates your need to invest in hardware upfront, so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many virtual machines as you need, configure security and networking, and manage storage.

Amazon incurs a charge for every hour that an instance is running. Thus, the easiest way to save money is to turn off instances that are not required.

The <Stopinator> is a simple script that can turn off EC2 instances. It can be triggered by CRON(Linux) or Scheduled Task (Windows) and if it finds a specific tag, it either stops or terminates them.

(The following file should be run in a EC2 Linux instance.)

- Stopinator.py

import boto.ec2, os

# Connect to EC2 in this region

region = os.environ.get(‘EC2_REGION’)

connection = boto.ec2.connect_to_region(region)

# Get a list of all instances

reservations = connection.get_all_instance()

# Loop through each instance

for r in reservavtions:

for i in r.instances:

# Check for the ‘stopinator’ tag on running instances

if ‘ stopinator’ in i.tags.keys():

action = i.tags[‘stopinator’].lower()

# Stop?

if action == ‘stop’ and i.state == ‘running’:

print “Stopping instance”, i.id

connection.stop_instances([i.id])

# Terminate?

elif action == ‘terminate’ and i.state != ‘terminated’:

print “Terminating instance”, i,id

connection.terminate_instances([i.id])

############CODE TERMINATES#############

Ideas for implementing stopinator:

- Schedule the stopinator to stop machines each evening, to save money

- Mark instances that you want to keep running, then have the stopinator stop only the unknown instances (but don’t terminates them – they might be important)

- Have another instances script that turns on the instances in the morning

- Set different actions for weekdays and weekends

- Use another tag to identify how many hours you want an instance to run, which is ideal for instances you just want to use for an experiments. Schedule the stopinator to run hourly and configure it to terminate instances that run longer than the indicated number of hours.

connection = boto.ec2.connect_to_region(region)

connection.put_metric_data(namespace=”Lab”, name=”highlow”, value=seconds)

While you’re taking this lab, you may have noticed that there’s no prompt for security credentials. You were able to copy data, take snapshots and start/stop instances without having to identify yourself. You were able to copy data, take snapshot and start/stop/terminate instances without having identify yourself. (except when connection, we open PPK for PuTTy and set up the SSH)

Instance Metadata Service

Instance metadata is data about your instance that you can use to configure or manage the running instance. Included in the data is a set of security credentials that was used for all your commands during this lab.

It works as follows:

- A role called scripts was created with appropriate permissions to run the lab.

- The Amazon EC2 instance you have been using was launched with the scripts role.

- The AWS CLI and Python SDK automatically retrieved the security credentials via the Instance Metadata Service.

Run ./show-credentials

A large block of text will appear:

{

“Code” : “Success”

“LastUpdated”: <Time>

“Type” : ”AWS-HMAC”

“AccessKeyId”

“SecretAccessKey”

“Token”

“Expiration”

}

The metadata contains an Access Key and Secret Key, which authorizes the AWS CLI and scripts on your EC2 instance to call AWS services.

- Three ways to acess AWS ~ CLI, browser management, Programming (Py, Ruby…)

- Access Amazon S3, copy and paste

- Automate EBS snapshot

- Automate Bastion security

- Automate CloudWatch Metrics

[Notes] AWS Automation using script and AWS CLI的更多相关文章

  1. 亚马逊AWS在线系列讲座——基于AWS云平台的高可用应用设计

    设计高可用的应用是架构师的一个重要目标,可是基于云计算平台设计高可用应用与基于传统平台的设计有很多不同.云计算在给架构师带来了很多新的设计挑战的时候,也给带来了很多新的设计理念和可用的服务.怎样在设计 ...

  2. [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage

    这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...

  3. [AWS vs Azure] 云计算里AWS和Azure的探究(5) ——EC2和Azure VM磁盘性能分析

    云计算里AWS和Azure的探究(5) ——EC2和Azure VM磁盘性能分析 在虚拟机创建完成之后,CPU和内存的配置等等基本上是一目了然的.如果不考虑显卡性能,一台机器最重要的性能瓶颈就是硬盘. ...

  4. [AWS vs Azure] 云计算里AWS和Azure的探究(4)

    云计算里AWS和Azure的探究(4) ——Amazon EC2 和 Windows Azure Virtual Machine 接下来我们来看看Azure VM的创建.Azure里面虚拟机的创建跟A ...

  5. [AWS vs Azure] 云计算里AWS和Azure的探究(3)

    云计算里AWS和Azure的探究(3) ——Amazon EC2 和 Windows Azure Virtual Machine 今天我来比较一下AWS EC2和Azure VM的具体流程上的异同.以 ...

  6. [AWS vs Azure] 云计算里AWS和Azure的探究(2.1)

    云计算里AWS和Azure的探究(2.1) ——Amazon EC2 和 WindowsAzure Virtual Machine 刚发表了就发现Amazon的价格下降了,第一代Linux的EC2价格 ...

  7. [AWS vs Azure] 云计算里AWS和Azure的探究(2)

    Amazon EC2是Elastic Compute Cloud的简称,翻译成中文就是弹性计算云.它是Amazon云里面最基础的内容,也是发展到今天最成熟的部分,通过EC2, 你可以在Amazon的云 ...

  8. [AWS vs Azure] 云计算里AWS和Azure的探究(1)

    转自:http://www.cnblogs.com/hotcan/archive/2013/01/31/2886794.html 云计算里AWS和Azure的探究(1) 全球领先的云的计算平台主要有两 ...

  9. AWS探索及创建一个aws EC2实例

    一.AWS登陆 1.百度搜索aws,或者浏览器输入:http://aws.amazon.com 2.输入账户及密码登陆(注册流程按照提示走即可) 二.创建EC2实例(相当于阿里云的ecs) 1.找到E ...

随机推荐

  1. arcgis将图片转成shp地图

    打开arcMap 1.右键[图层]-[属性]-[坐标系]-[地理坐标系]-双击"WGS 1984" 2.右键[图层]-[添加数据],打开图片文件-Band_2 3.右键图片-[属性 ...

  2. Java日志——2016年5月31日

    1. 三元运算符(A?B:C)属于运算符,表达式必须具有返回值,则A必须是boolean类型值,B和C必须是一个具有返回值的表达式. 2. switch...case本质上只支持int类型的选择判断, ...

  3. php工作笔记7-概率算法

    a/m  b/m   c/m   d/m   10%    40%    20% a+b+c+d+... < = m array k   =  {a,b,c...} randt = rand(1 ...

  4. 初识Promise

    Promise对象 曾经用seajs开发后台管理的时候,矫情的PHPER非要JS内联到HTML文件中,方便他调用内容,还指定了jQueryFileUpload作为上传插件. 当时看到jQueryFil ...

  5. dubbo-monitor图标功能不显示

    原因有3: 1.需要创建目录 2.需要在consumer的配置文件中增加如下配置,使用zk为注册中心 <!-- 监控中心,需要配置以后dubbo-monitor才能生效 -->    &l ...

  6. iOS开发中获取文本的宽高的方式

    /** 计算单行文字的size @parms  文本 @parms  字体 @return  字体的CGSize */ + (CGSize)sizeWithText:(NSString *)text ...

  7. 【232】◀▶ IDL显示地理图像

    参考: 01   IMAGE 将图像数据以图形窗体的形式显示. 02   COLORBAR 在已经存在的IDL图形中增加一个colorbar或创建. 03   MAPGRID 在已经存在的IDL地图图 ...

  8. 提升linux下tcp服务器并发连接数限制

    1.修改用户进程可打开文件数限制   在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统 ...

  9. JetBrains激活

    https://www.imsxm.com/jetbrains-license-server/ 已经累计为大家激活1360577次 :) JetBrains授权服务器:http://idea.imsx ...

  10. Eclipse设置、调优、使用(转自)

    转自http://yuanzhifei89.iteye.com/blog/974082 eclipse调优 一般在不对eclipse进行相关设置的时候,使用eclipse总是会觉得启动好慢,用起来好卡 ...