AWS導入(1)
必要なものを入れる
- 仮想環境を作ってその中に入れる。
|
1 2 3 |
Macico:test02 macico$ pyenv activate py37b
(py37b)
Macico:test02 macico$ pip install aws-sam-cli (py37b)
Macico:test02 macico$
pip install awscli |
SAMのテンプレートアプリケーション
- SAM =AWS Serverless Application Model (AWS SAM)のこと
- AWS CloudFormation (CFn)の拡張らしい。サーバーレス版がSAMになる
- テンプレートを書くことで同じような環境+実行設定をすることができる
- テンプレートを用いて作った環境をStackっていう
- 参考
- モノ的には、Dockerと似たような感じ!
- 何もの?
- Lambda, API Gateway, DynamoDB のリソースをまとめて管理できる
- 何がうれしい?
- CFnと似たようなフォーマットで書くことができる
- ローカルでも実行可能なので、課金されない
事前にAWSのアカウント設定。
これを参考にした。https://note.com/kuyo/n/n49b56b1448b1
やっていることは
- IAM(Identify and Access Management)ユーザー生成
- アクセスキー取得
- IAMでパスワードポリシー変更
- IAMでエンドポイント制限
- MFAの有効化
- IAMユーザとRootユーザともに仮想MFAデバイスでGoogleAuthenticatorを使うようにする
- 支払関連
- 通貨変更(JPY): 正直どっちがいいのんだ??
- Billing設定で無料枠設定アラートと請求アラートの設定
- CloudWatchで課金設定を見守るようにする
- CostExplorerを有効にしておく
AWSのアクセス設定
|
1 2 3 4 5 |
(py37b)
Macico:dokcer macico$>
aws configure AWS Access Key ID
[None]:
(AWSのアクセスキー) AWS Secret Access Key
[None]:
(AWSのシークレットキー) Default
region name
[None]:
(構築するRegion) Default
output format
[None]:
json |
ローカルで実行する場合は上野アクセスキーとかはテキトーで良い。
ただ、これを使って将来的にデプロイする場合はリージョン舞に設定必要なので–profileをつけて固有の設定にしたほうが幸せ
–profile つけておくと、このオプションがないとawsコマンド通らなくなる
テストテンプレートをつくる
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
(py37b)
Macico:dokcer macico$
sam init
--runtime python3.7 Which template source would you like to
use?
1
-
AWS Quick Start Templates
2
-
Custom Template Location Choice:
1 Project name
[sam-app]:
test02 Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git AWS quick start application templates:
1
-
Hello World Example
2
-
EventBridge Hello World
3
-
EventBridge App from scratch
(100+
Event Schemas)
4
-
Step Functions Sample App
(Stock Trader) Template selection:
4 ----------------------- Generating application: ----------------------- Name:
test02 Runtime:
python3.7 Dependency Manager:
pip Application Template:
step-functions-sample-app Output Directory:
. Next steps can be found in
the README file at
./test02/README.md (py37b)
Macico:dokcer macico$
ll drwxr-xr-x
8
macico staff
256
Oct
24
14:01
test02/ (py37b)
Macico:test02 macico$
cd test02 (py37b)
Macico:test02 macico$
tree . ├──
README.md ├──
functions │
├──
__init__.py │
├──
stock_buyer │
│
├──
__init__.py │
│
├──
app.py │
│
└──
requirements.txt │
├──
stock_checker │
│
├──
__init__.py │
│
├──
app.py │
│
└──
requirements.txt │
└──
stock_seller │ ├──
__init__.py │ ├──
app.py │ └──
requirements.txt ├──
statemachine │
└──
stock_trader.asl.json ├──
template.yaml └──
tests └──
unit ├──
__init__.py ├──
test_buyer.py ├──
test_checker.py └──
test_seller.py 7
directories,
17
files (py37b)
Macico:test02 macico$ |
つまり必要なものは
- functions
- pythonのファイル群
- lamdaで動かす関数たち。
- test
- function に対するテスト群
- templete.yaml
- SAMのテンプレート
SAMテンプレート大解剖
SAMテンプレート、意外に大きかったので必要な部分だけ作ってみる
- AWSTemplateFormatVersion:テンプレートのフォーマットのバージョン(ほぼ固定)
- Transform:AWS CloudFormation がテンプレートを処理するために使用するマクロ(ほぼ固定)
- Description:テンプレートの説明
- Resources: デプロイしたいAWSリソース(EC2、S3など)およびそのAWSリソースの設定
- (FeedHandelLambda)はリソースの名前
- Propertiesにリソースの情報
- functions/lambda_function.lambda_handler:の部分に使用するファイルと関数名
- Runtime: 環境
- Role: でリソース情報を書く(ARNを書く)
- Parameters:テンプレートで使いたい変数定義
- Output:スタック作成後に出力したいもの
※ARN:https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/UserGuide/USER_Tagging.ARN.html
|
1 2 3 4 5 6 7 8 9 10 |
AWSTemplateFormatVersion:
'2010-09-09' Transform:
AWS::Serverless-2016-10-31 Description:
Create Lambda function
by using AWS SAM. Resources: FeedHandelLambda: Type:
AWS::Serverless::Function Properties: Handler:
functions/lambda_function.lambda_handler Runtime:
python3.7 Timeout:
30 |
ローカルでDynamoDBをつくる
Dockerを持ってくる。こちらを参考にした。
本当はComposeしよかと思ったが、できなかったので諦めてdocker上げた上で中身のjavaを落とすことにした
|
1 2 |
(py37b)
Macico:test02 macico$
docker pull amazon/dynamodb-local (py37b)
Macico:test02 macico$
docker network create lambda-local |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
(py37b)
Macico:test02 macico$
ll total
7440 drwxr-xr-x@
10
macico staff 320
Oct
24
16:11
./ drwxr-xr-x 8
macico staff 256
Oct
24
15:59
../ -rw-r--r-- 1
macico staff 3730
Oct
24
14:01
.gitignore -rw-r--r--@ 1
macico staff 3790813
Oct
24
16:11
DynamoDBLocal.jar drwxr-xr-x@
50
macico staff 1600
Oct
24
16:10
DynamoDBLocal_lib/ -rw-r--r-- 1
macico staff 7678
Oct
24
14:01
README.md drwxr-xr-x 6
macico staff 192
Oct
24
14:01
functions/ drwxr-xr-x 3
macico staff 96
Oct
24
14:02
statemachine/ -rw-r--r-- 1
macico staff 3445
Oct
24
14:01
template.yaml drwxr-xr-x 3
macico staff 96
Oct
24
14:01
tests/ (py37b)
Macico:test02 macico$ |
Dockerを作る。一行目は停止中のコンテナ消す呪文。run しているのでこの地点でUPしている
|
1 2 3 4 5 6 7 |
(py37b)
Macico:test02 macico$
docker rm
`docker ps
-f
"status=exited"
-q` (py37b)
Macico:test02 macico$
docker run
-d
--network lambda-local
--name dynamodb
-p
8000:8000
amazon/dynamodb-local
-jar DynamoDBLocal.jar
-sharedDb b217fe2c185665a65b27634065def36c1430b4f63060423257cc25d7d200e3e4 (py37b)
Macico:test02 macico$
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b217fe2c1856 amazon/dynamodb-local "java -jar DynamoDBL…" 5
seconds ago Up
3
seconds 0.0.0.0:8000->8000/tcp dynamodb (py37b)
Macico:test02 macico$ |
実際に、利用する際にはendpointを8000にしているので、localhost:8000にして起動すれば起動する。
起動→テーブル定義create→Insertって感じでサクサク行く。localの場合はどちらもendpointをlocalhost:8000にしないとaws configureで指定したリモートのDBが更新される





