RDS インスタンスタイプの決め方

db.r5.large などの表記で記載されているが、「r5」の部分がインスタンスタイプ、「large」がインスタンスサイズ

インスタンスタイプは、r系、m系、t系などがある。rdsもt系とm系が汎用で、t系はバースト可能クラス

### r系
・メモリ内の大きいデータセットを処理するワークロードに対して高速なパフォーマンスを実現するように設計されているメモリ最適化インスタンスに属するr系
・r6g.largeのような世代の数字の後ろにgのような文字がついているタイプは追加機能(gならばAWS Gravitonプロセッサ(最良のコストパフォーマンスを提供))
・small < medium < large < xlarge < 2xlarge < 4xlarge < ・・・ なるほど、用途で選ぶのね。

AWS EC2インスタンスタイプの決め方

EC2のインスタンスタイプは用途別に集められた「インスタンスファミリー」とCPUなどのスペックを示す「インスタンスサイズ」から構成される。このインスタンスファミリーの数字は世代を表している。
数字が大きければ大きいほど新しい
t2, t3ではt3の方が新しい

### インスタンスタイプの種類
「汎用」、「コンピューティング最適化」、「メモリ最適化」、「高速コンピューティング」、「ストレージ最適化」の5種類がある
一般的な業務システムに適しているのは「汎用」「コンピューティング最適化」

### 汎用
「T3」「T2」 バーストできる。CPUクレジットを貯めておき、突発的に負荷が高まったときに貯めたCPUクレジットを消費して対応できる。コストが安い

「M5」「M4」 ネットワークをバランスよく提供

### コンピューティング最適化
「C5」「C4」: 性能が高いプロフェッサを積んでいる。高速処理が必須の場合、コストパフォーマンスに優れる。

### 機械学習
GPUインスタンスは、P3

### SAP
X1はSAP から、Business Warehouse on HANA (BW) 、Data Mart Solutions on HANA 、Business Suite on HANA (SoH) 、Business Suite S/4HANA を実行するための認定を受けているインスタンスファミリー

なるほど、勉強になるね。

AWS KMSを使ってみる

AWS KMSコンソールでキーを作成します。

Key policy, Cryptographic configuration, public keyが発行される

public key Publickey-*.pemをダウンロードして使用する。
user nameを設定しているので、ユーザのIDを指定してdecryptするのかな。
概要は何となく理解したが。

AWS KMSとは

データの暗号化/複合化に利用するKeyをセキュアに管理するのがAWS KMSです。
暗号化/複合化のためのキーをAWS KMSで簡単に生成でき、AWS KMS上にキーを保管することができる。

1. KMS上にCMKを作成
暗号化するときに利用するキーを生成するためのCMLを作成

2. CMKのKeyIDからデータキーを作成
CMLを元にしたKeyIDからデータキーを作成する
– 暗号化用のデータキー:データを暗号化するためのキー
– CMKを元に暗号化されたデータキー: データ複合化のために利用するキー

なるほど、使ってみないとわからんな。

FargateのvCPUを理解する

タスクサイズ -> タスクメモリ(MiB)
タスクサイズ -> タスクCPU(単位)
タスクサイズ -> コンテナの定義 -> メモリ制限 -> ハード制限
タスクサイズ -> コンテナの定義 -> メモリ制限 -> ソフト制限
タスクサイズ -> コンテナの定義 -> CPUユニット数

### ハード制限(Memory)
コンテナに適用されるハードウェアのメモリ制限。上限を超えると強制的に終了となる。
ウェブアプリケーションだと300~500MBがおすすめとのこと

### ソフト制限(memoryReservation)
システムメモリ競合時に維持されるメモリ上限 
状況に応じてハード制限(設定されている場合)かインスタンス自体の利用可能上限まで消費
memory (<= memoryReservation) <= instance memory Fargateの動作ベース https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/task-cpu-memory-error.html サポートされる CPU 値は、128CPU 単位 (0.125vCPUs) と10240CPU 単位 (10vCPUs)の間 ### CPU value 256(.25vCPU): 512, 1024, 2048 512(.5vCPU): 1024, 2048, 3072, 4096 1024(1vCPU): 2048, 3072, 4096, 5120, 6144, 7168 2048(2vCPU): Between 4096 and 16384 in increments of 1024 4096(4vCPU): Between 8192 and 30720 in increments of 1024 うーむ、何をベースにvCPU, Memoryを決めたら良いかイマイチわからんな。。

[CakePHP3.10] 認証コンポーネント

UsersController.php

use Cake\Auth\DefaultPasswordHasher;
use Cake\Event\Event;

    public function initialize(){
        parent::initialize();
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
                'authorize' => ['Controller'],
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'username',
                            'password' => 'password'
                        ]
                    ]
                ],
                'loginRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'logout',
                ],
                'authError' => 'ログインしてください。',
            ]);
    }

    function login(){
        if($this->request->isPost()){
            $user = $this->Auth->identify();
            if(!empty($user)){
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('ユーザ名かパスワードが間違っています。');
        }
    }

    public function logout(){
        $this->request->session()->destroy();
        return $this->redirect($this->Auth->logout());
    }

    public function beforeFilter(Event $event){
        parent::beforeFilter($event);
        $this->Auth->allow(['login', 'index', 'add']);
    }

    public function isAuthorized($user = null){
        if($user['role'] === 'admin'){
            return true;
        }
        if($user['role'] === 'user'){
            return false;
        }
        return false;
    }

authorize, authenticate, loginRedirect, logoutRedirect, authError
$this->request->session()->destroy();

### login.ctpを作成

<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
	<fieldset>
		<legend>アカウント名とパスワードを入力して下さい。</legend>
		<?= $this->Form->input('username') ?>
		<?= $this->Form->input('password') ?>
	<fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>

AuctionBaseController.phpを作成し、継承させる
なるほどー

[CakePHP3.10] auctionサイトを作ろう

$ php composer.phar create-project –prefer-dist cakephp/app:3.* auction

bin/cake bake migration CreateUsers username:string[100] password:string[100] role:string[20]
bin/cake bake migration CreateBiditems user_id:integer name:string[100] finished:boolean endtime:datetime created
bin/cake bake migration CreateBidinfo bititem_id:integer user_id:integer price:integer created
bin/cake bake migration CreateBidrequests biditem_id:integer user_id:integer price:integer created
bin/cake bake migration CreateBidmessages bidinfo_id:integer user_id:integer message:text created
bin/cake migrations migrate
bin/cake bake all users
bin/cake bake all biditems
bin/cake bake all bidinfo
bin/cake bake all bidrequests
bin/cake bake all bidmessages

[CakePHP3.10] ページネーション

– コントローラのページネーション: 一定数ごとにレコードを取り出す
– ビューテンプレートのリンク

controller

$this->loadComponent('Paginator');

PeopleController.php

	public $paginate = [
		'limit' => 5,
		'sort' => 'id',
		'direction' => 'asc',
		'contain' => ['Messages'],
	];

	public function initialize(){
		parent::initialize();
		$this->loadComponent('Paginator');
	}

	public function index(){
		$data = $this->paginate($this->People);
		$this->set('data', $data);
	}

template

<p>This is People table records.</p>
<table>
<thead><tr>
	<th>id</th><th>name</th><th>mail</th><th>age</th><th>message</th>
</tr></thead>
<?php foreach($data->toArray() as $obj): ?>
<tr>
	<td><?=h($obj->id) ?></td>
	<td><a href="<?=$this->Url->build(["controller"=>"People", "action"=>"edit"]); ?>?id=<?=$obj->id ?>"><?=h($obj->name) ?></a></td>
	<td><?=h($obj->mail) ?></td>
	<td><?=h($obj->age) ?></td>
	<td><?php foreach($obj->messages as $item): ?>
	"<?=h($item->message) ?>"<br>
	<?php endforeach; ?></td>
	<td><a href="<?=$this->Url->build(["controller"=>"People", "action"=>"delete"]); ?>?id=<?=$obj->id ?>">delete</a></td>
</tr>
<?php endforeach; ?>
</table>
<div class="paginator">
	<ul class="pagination">
		<?=$this->Paginator->first(' |<< ' . '最初へ') ?>
		<?=$this->Paginator->prev(' << ' . '前へ') ?>
		<?=$this->Paginator->next('次へ ' . ' >> ') ?>
		<?=$this->Paginator->last('最後へ' . ' >>| ') ?>
	</ul>
</div>

custom finderを利用することもできる

なるほど、とりあえずOK 次に行こう

[CakePHP3.10] ヘルパー

$this->Html->doctype(document type)
$this->Html->charset(charset)
$this->Html->css(file name)
$this->Html->style()
$this->Html->script()
$this->Html->scriptStart()
$this->Html->scriptEnd()
$this->Html->link()
$this->Html->image()
$this->Html->script()
$this->Html->tableHeaders()
$this->Html->tableCells()

<table>
<?=$this->Html->tableHeaders(["title", "name", "mail"],
	["style"=>["background:#006; color:white"]]) ?>
<?=$this->Html->tableCells([["this is sample", "taro", "taro@yamada"],["this is sample", "taro", "taro@yamada"],["this is sample", "taro", "taro@yamada"]], ['style'=>['background:#ccf']], ['style'=>['background:#ccf']]) ?>
</table>

$this->Html->nestedList()

アクションの指定、クエリパラメータの指定、拡張子の指定などもUrlBuilderで指定できる

### Textヘルパー

<?=$this->Text->autoLinkUrls('http://google.com') ?>
<?=$this->Text->autoLinkEmails('hoge@gmail.com') ?>
<?=$this->Text->autoParagraph('one\ntwo\nthree') ?>

Numberヘルパー

<p>金額は、<?=$this->Number->currency(1234567, 'JPY') ?>です。</p>
<p>2桁で表すと、<?=$this->Number->precision(1234.56789, 2) ?>です。</p>
<p>2桁で表すと、<?=$this->Number->toPercentage(0.12345, 2, ['multiply'=>true]) ?>です。</p>

OK、大分いいところまで来た気がする