Amazon ML を触ろう

ガンガン行きます。

というか、Amazon Redshiftって何だ。。。 dataを保存できるのか。

modeling process

モデリングしてる模様

completed

Try real-time prediction

ここで予想します。

うーん、なんとなく流れは分かったようなわからないような。
データセットが用意されていると、単なる作業で頭使わないので駄目ですね。

AWS machine learning

Amazon machine learning モデルの概念図

what is amazon machine learning
amazon ML can be used to make predictions for a variety of purposes. For example, you could build a model in Amazon ML that will predict whether a given customer is likely to respond to a marketing offer. Amazon ML creates models from supervised data sets. This means that the model is based on a set of previous observations. This set of observations consists of features or attributes as well as the target outcome. In the marketing offer example, the features might include the age, profession, and gender of the customer. The target outcome (also called the target variable) would be whether that particular customer responded to the marketing offer or not.

The process of creating a model from a set of known observations is called training. Once you have trained a model in Amazon ML, you can then use the model to predict outcomes from a set of attributes that matches the attributes used to train the model. Amazon ML scales so that you can make thousands of predictions concurrently. This is important, as today machine learning is often used to provide predictions in near real-time. In this lab, you will be using a machine learning model to predict which restaurants a customer is likely to favor based on the results of a search query.

data setをs3のバケットのuploadする。

なるほど、S3はこういう風に使うのね。

machine learningを選択する

流石にまだ翻訳されてないな。

s3

bucketを作成し、upload、permissionをread only everyoneに変更します。

バケットポリシー

{
  "Id": "Policy1535369626475",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1535369623531",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::mybucket198205/*",
      "Principal": "*"
    }
  ]
}

見れるようになりました♪

versioning: uploadしても古いバージョンを呼び出せる。

これは中々いいですね。

AWS Lambdaを触ってみよう

runtime -> python3.6
role: lambda-execution-role

pythonは3.6か。roleは何のことかイマイチよくわかりません。

s3のトリガーを追加する。

Diagramが Create-Thumbnail、S3、Amazon S3、 AWS Lambdaがあります。 S3が2つあるが。。ま~気にせずに進みましょう。それと、Cloudwatchlogってなんだ。。

関数コードを入力していきます。

あれ、何これ、S3のimages-123-aws-resizedのbucketにリサイズされが画像が生成されとる!?

Congratulations! You have successfully:
おいおい、肝心のlambdaの関数がblack boxじゃないか。。まあ、画像のリサイズはライブラリ使えば一発なんですがね。

lambdaが何かは少し理解が深まりました♪ OK!
python3.5ではなく、3.6なんですね。

AWS Lambdaでアプリケーション

S3、Lambdaを使います。

A user uploads an object to the source bucket in Amazon S3 (object-created event).
Amazon S3 detects the object-created event.
Amazon S3 publishes the object-created event to AWS Lambda by invoking the Lambda function and passing event data as a function parameter.
AWS Lambda executes the Lambda function.
From the event data it receives, the Lambda function knows the source bucket name and object key name. The Lambda function reads the object and creates a thumbnail using graphics libraries, then saves the thumbnail to the target bucket.

Task 1: Create the Amazon S3 Buckets
コンソールからS3に入ります。

あれ、もうすでにbucketが出来てますね。。。

bucket name を images-123-aws で一つ作成します。
同様の手順で、images-123-aws-resizedのbucketを作成します。now、bucketは4つになりました。

images-123-awsに画像をuploadします。

さあ、いよいよlambdaを使います。

AWS Lambdaとは

Lambdaの読み方がわからない
⇒インスタンスベースの仮想サーバー(EC2、ElastiChache、Redshiftなど)を使わずにアプリケーションを開発するアーキテクチャ

>Lambda を使用すれば、実質どのようなタイプのアプリケーションやバックエンドサービスでも管理を必要とせずに実行できます。コードさえアップロードすれば、高可用性を実現しながらコードを実行およびスケーリングするために必要なことは、すべて Lambda により行われます。コードは、他の AWS サービスから自動的にトリガーするよう設定することも、ウェブやモバイルアプリケーションから直接呼び出すよう設定することもできます。

サーバー管理がないということは、sakuraの共有サーバーのようなものか?
これ、わざわざやる必要あるか?

とりあえず、AWS Lambda 入門を叩いてみましょう。

なんだこれは!? とりあえず、Qwiklabsにjoinしましょか。

By the end of this lab you will be able to:

  • Create an AWS Lambda function
  • Configure an Amazon S3 bucket as a Lambda Event Source
  • Trigger a Lambda function by uploading an object to Amazon S3
  • Monitor AWS Lambda S3 functions through Amazon CloudWatch Log

コンピューティングにlambdaがありますね。

node.jsのコードを実行します。

exports.handler = (event, context, callback) => {
    // 「Hello World!」という文字列で続行
    callback(null, 'Hello world!');
};

s3から画像をダウンロードする

require_once('vendor/autoload.php');

$s3client = new Aws\S3\S3Client([
		'credentials' => [
				'key' => '',
				'secret' => ''
		],
		'region' => 'ap-northeast-1',
		'version' => 'latest',
]);

$result = $s3client->getObject([
		'Bucket' => 'capital-technology',
		'Key' => 'img.jpeg',
]);

echo $result['ContentLength'];

OK!

次は、vagrantからec2にgitでpush, commitをしたい。

phpでlocalからS3に画像をアップロードする

aws sdk for php v3を使う。autoload.phpで良い。

<?php

require_once('vendor/autoload.php');

$s3client = new Aws\S3\S3Client(&#91;
		'credentials' => [
				'key' => '',
				'secret' => ''
		],
		'region' => 'ap-northeast-1',
		'version' => 'latest',
]);

$result = $s3client->putObject([
		'Bucket' => 'capital-technology',
		'Key' => 'img.jpeg',
		'SourceFile' => 'aaa.jpeg',
		'ContentType' => mime_content_type('aaa.jpeg'),
]);

アップロードはできたが、公開するとしないと、アクセスできない。
更に、ストレージクラスもスタンダード。ここらへんをphp側で操作できるようにしたい。

ACLで’public-read’と指定すると公開指定ができる。

$result = $s3client->putObject([
		'Bucket' => 'capital-technology',
		'Key' => 'img2.jpeg',
		'ACL' => 'public-read',
		'SourceFile' => 'aaa.jpeg',
		'ContentType' => mime_content_type('aaa.jpeg'),
]);

echo $result['ObjectURL'];

aws sdk for php v3

まずcomposerをいれます。

$ curl -sS https://getcomposer.org/installer | php

続いて、aws sdkをinstallします。

$ composer require aws/aws-sdk-php
Using version ^3.62 for aws/aws-sdk-php
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 6 installs, 0 updates, 0 removals
  - Installing mtdowling/jmespath.php (2.4.0): Downloading (100%)
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.3): Downloading (100%)
  - Installing aws/aws-sdk-php (3.62.2): Downloading (100%)
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
aws/aws-sdk-php suggests installing doctrine/cache (To use the DoctrineCacheAdapter)
aws/aws-sdk-php suggests installing aws/aws-php-sns-message-validator (To validate incoming SNS notifications)
Writing lock file
Generating autoload files

AWS S3を使おう

新しいバケットを作成します。

capital-technologyというバケットを作成します。

s3にファイルをアップロードします。

なるほど!バケットの下に保存されました。ディレクトリみたいなもん?

ストレージクラスは
1. スタンダード
2. 標準 IA
3. 1ゾーン IA
4. 低冗長化
から選択できる。