[Notes] AWS Automation using script and AWS CLI

(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

时间: 2024-10-13 14:49:36

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

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

设计高可用的应用是架构师的一个重要目标,但是基于云计算平台设计高可用应用与基于传统平台的设计有许多不同.云计算在给架构师带来了许多新的设计挑战的时候,也给带来了许多新的设计理念和可用的服务.如何在设计应用的时候充分利用云平台的各种特点是基于云计算设计的一个重要条件.在这个在线讲座中,我们将以亚马逊AWS云平台为例,讨论如何设计一个高可用应用. 我们先会根据AWS服务是否天然高可用.高容错的特点把常见的AWS服务分类.比如AWS把下面服务设计成高可用和高容错的服务: ·     Amazon S3

重新开始继续准备AWS Dev认证考试:AWS Lambda 环境变量

利用 Lambda 函数的环境变量,您可以将设置动态传递到函数代码和库,而无需对代码进行任何更改.环境变量是您使用 AWS Lambda 控制台.AWS Lambda CLI 或 AWS Lambda 开发工具包作为函数配置的一部分创建并修改的密钥值对.AWS Lambda 随后会使用相应语言所支持的标准 API(如适用于 Node.js 函数的 process.env 将这些密钥值对提供给您的 Lambda 函数代码. 您可以使用环境变量帮助库了解以下信息:安装文件的目录.存储输出的位置.存储

[AWS Lambda] Scheduling Events with AWS Lambda (a.k.a. Lambda cron jobs)

Learn how to create AWS Lambda functions that execute on a scheduled interval, much like a cron job would. In this lesson we will create a Lambda function that checks for a string of text on a website to verify the website is up and operational. The

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

一.AWS登陆 1.百度搜索aws 2.输入账户及密码登陆(注册流程按照提示走即可) 二.创建EC2实例(相当于阿里云的ecs) 1.找到EC2 2.创建实例 3.选择系统类型(个人习惯用CentOS,根据情况选择吧) 系统版本 4.选择配置(新用户有免费一年的实例类型) 5.配置实例 6.可以选择添加数据盘 7.添加服务器标签 8.配置安全组信息 9.确认信息 10.查看实例 至此.一个aws的EC2实例就购买完成了!! 原文地址:https://www.cnblogs.com/hei-ma/

使用 AWS CloudTrail 记录 IAM 和 AWS STS API 调用

IAM 和 AWS STS 与 AWS CloudTrail 集成,后者是一项服务,它提供 IAM 用户或角色所采取的操作的记录.CloudTrail 将对 IAM 和 AWS STS 的所有 API 调用作为事件捕获,包括来自控制台和 API 调用的调用.如果您创建了跟踪,则可以使 CloudTrail 事件持续传送到 Amazon S3 存储桶.如果您不配置跟踪,则仍可在 CloudTrail 控制台的 Event history (事件历史记录) 中查看最新事件.可使用 CloudTrai

添加IAM用户、安装AWS CLI

1. 在 Microsoft Windows 上安装 AWS Command Line Interface(下载地址: https://s3.amazonaws.com/aws-cli/AWSCLI64.msi),一路next即可 pasting 2. 添加IAM用户,并添加访问权限, https://console.aws.amazon.com/iam/home#/users ,下载Access key ID和Secret access key 3. 配置AWS 凭证 (https://doc

Installing the AWS SAM CLI on macOS

Installing the AWS SAM CLI on macOS https://docs.aws.amazon.com/zh_cn/serverless-application-model/latest/developerguide/serverless-sam-cli-install-mac.html The following steps help you to install and configure the required prerequisites for using th

原文版:Intel OpenVINO? Toolkit and AWS* Greengrass!!!

https://software.intel.com/en-us/articles/get-started-with-the-openvino-toolkit-and-aws-greengrass Hardware-Accelerated Function-as-a-Service Using AWS Greengrass Hardware Accelerated Function-as-a-Service (FaaS) enables cloud developers to deploy in

Day 1之后,AWS亚马逊云给出了Day 2人工智能生存指南

1997年,亚马逊发表了著名的"Day 1"致股东信,贝索斯在信中说" this is Day 1 for the Internet and, if we execute well, for Amazon.com". 在过去的20年间,亚马逊一直保持在Day 1状态.20年后,贝索斯再次在股东信里强调,"Day 2公司将停滞不前,接着将变得无关紧要,然后将经历痛苦的衰退,直至最终死亡.这就是为何我们总是要做Day 1公司的原因." 尽管贝索斯所在