#author("2019-08-20T19:38:37+00:00","","")
#author("2019-08-29T12:06:28+00:00","","")
#mynavi(AWSメモ)
#setlinebreak(on);

* 目次 [#x44086e0]
#contents

* よく使うオプション [#x448b916]
|オプション|説明|使用例|h
|--region|リージョン|--region ap-northeast-1|
|--endpoint-url|エンドポイント&br;※DynamoDB Local 等にコマンド発行する場合に利用|--endpoint-url http://localhost:8000|
|--output|実行結果の出力形式|--output json|
|--profile|使用するプロファイル名|--profile developper|
※http://docs.aws.amazon.com/cli/latest/reference/

* 名前付きプロファイルの作成 [#e8379a0e]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws configure --profile developper
AWS Access Key ID [None]: XXXXXXXXXXXXXXX
AWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXX
Default region name [None]: ap-northeast-1
Default output format [None]: json
}}
※事前に I AM から ユーザとアクセスキーを作成しておく。
#html(</div>)

* アカウントIDの取得/確認 [#q7ce229f]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws sts get-caller-identity | grep Account | awk '{print $2}' | sed -e "s/[^0-9]//g"
}}
#html(</div>)

* Lambda [#o9acfc6d]
#html(<div style="padding-left:10px">)

** Lambda関数の作成 [#xcfa5312]
#html(<div style="padding-left:10px">)
#myterm2(){{

mkdir test-function
cd test-function
vim index.js
# Lambda関数を入力

zip -r test-function.zip .

aws lambda create-function \
  --function-name test-function \
  --runtime nodejs6.10 \
  --role arn:aws:iam::xxxxxxxxxxxx:role/xxxxxxxx \
  --handler index.handler \
  --zip-file fileb://test-function.zip
}}
#html(</div>)

** Lambda関数コードの変更 [#q245ddcc]
#html(<div style="padding-left:10px">)
#myterm2(){{
rm -rf test-function.zip && \
zip -r test-function.zip . && \
aws lambda update-function-code --function-name test-function --zip-file fileb://test-function.zip
}}
#html(</div>)

** Lambda関数の削除 [#q83b06be]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws lambda delete-function \
 --function-name test-function \
 --region ap-northeast-1 \
--profile developper
}}
#html(</div>)

** Lambda関数の実行 [#ge8ca577]
#html(<div style="padding-left:10px">)
#myterm2(){{
 aws lambda invoke \
--invocation-type RequestResponse \
--function-name helloworld \
--region ap-northeast-1 \
--log-type Tail \
--payload '{"key1":"value1", "key2":"value2", "key3":"value3"}' \
--profile developper \
outputfile.txt
}}
#html(</div>)

#html(</div>)

* DynamoDB [#e7b0ce49]
#html(<div style="padding-left:10px">)

http://docs.aws.amazon.com/cli/latest/reference/dynamodb/index.html

** テーブル一覧 [#za9a7c05]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb list-tables
}}
#html(</div>)

** テーブル作成 [#ka2699b9]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb create-table  \
    --profile developper \
    --table-name Books \
    --attribute-definitions \
        AttributeName=id,AttributeType=S \
        AttributeName=isbn,AttributeType=S \
        AttributeName=title,AttributeType=S \
    --key-schema AttributeName=id,KeyType=HASH AttributeName=isbn,KeyType=RANGE \
    --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
    --global-secondary-indexes IndexName=title-index,KeySchema=[{AttributeName=title,KeyType=RANGE}],Projection={ProjectionType=KEYS_ONLY},ProvisionedThroughput={ReadCapacityUnits=1,WriteCapacityUnits=1}
}}
※ --endpoint-url http://localhost:8000 をつければ ローカルの DynamoDB も作れる。

もしくは DDL的なものを作ってから作成する場合は

DDL( books.json ) を作成
#mycode2(){{
{
    "TableName": "Books",
    "AttributeDefinitions": [
        { "AttributeName": "id", "AttributeType": "S" },
        { "AttributeName": "isbn", "AttributeType": "S" },
        { "AttributeName": "title", "AttributeType": "S" }
    ],  
    "KeySchema": [
        { "AttributeName": "id", "KeyType": "HASH" }
    ],  
    "ProvisionedThroughput": { "WriteCapacityUnits": 1, "ReadCapacityUnits": 1 },
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "isbn-index",
            "KeySchema": [
                { "AttributeName":"isbn", "KeyType":"HASH" }
            ],
            "Projection": {"ProjectionType": "KEYS_ONLY" },
            "ProvisionedThroughput": {"ReadCapacityUnits": 1,"WriteCapacityUnits": 1 } 
        },
        {
            "IndexName": "title-index",
            "KeySchema": [
                { "AttributeName":"title", "KeyType":"HASH" }
            ],
            "Projection": {"ProjectionType": "KEYS_ONLY" },
            "ProvisionedThroughput": {"ReadCapacityUnits": 1,"WriteCapacityUnits": 1 } 
        }
    ]   
}
}}

DDLを使用してテーブル作成
#myterm2(){{
aws dynamodb create-table  --cli-input-json file://books.json
}}
#html(</div>)

** テーブル削除 [#kf5b63b9]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb delete-table  --table-name Books
}}
#html(</div>)

** テーブル検索(全件検索) [#za5167e0]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb scan --table-name Books
}}
#html(</div>)

** テーブル検索(キー指定)  --key-condition-expression [#icbc5392]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb query --endpoint-url http://localhost:8000 --table-name ExampleTable \
--key-condition-expression ' key1 = :key1 AND key2 = :key2' \
--expression-attribute-values '{ ":key1": {"S":"A"}, ":key2": {"S":"01"} }'
}}
#html(</div>)

** テーブル検索(グローバルセカンダリインデックスを使用して検索) [#t20dfb5c]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb query --endpoint-url http://localhost:8000 --table-name ExampleTable \
  --index-name gsicol1-index \
  --key-condition-expression 'gsiCol1 = :gsiCol1' \
  --expression-attribute-values '{ ":gsiCol1": {"S":"A01-01"} }'
}}
※取得できるのは、射影されているデータのみ
#html(</div>)

** データ登録 [#p5b2e4f1]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb put-item --endpoint-url http://localhost:8000 --table-name ExampleTable \
  --item '{ "key1" : { "S" : "A"}, "key2" : { "S" : "01"}, "col1" : { "S" : "value1"}, "col2" : { "S" : "value2"} }'
}}
#html(</div>)

** データ更新 [#db2fed7c]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb update-item --endpoint-url http://localhost:8000 --table-name ExampleTable \
  --key '{ "key1": {"S":"A"}, "key2": {"S":"01"} }' \
  --update-expression 'SET col1 = :col1' \
  --expression-attribute-values '{ ":col1": {"S":"value1-update!"} }'
}}
尚、主キー項目の値変更はできない模様。
&color(red){An error occurred (ValidationException) when calling the UpdateItem operation: This attribute is part of the key}; と怒られる。
#html(</div>)

** データ削除 [#m80c6703]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws dynamodb delete-item --endpoint-url http://localhost:8000 --table-name ExampleTable \
  --table-name ExampleTable \
  --key '{ "key1": {"S":"A"}, "key2": {"S":"01"} }'
}}
#html(</div>)



#html(</div>)

* Kinesis [#gfc4d483]
#html(<div style="padding-left:10px">)

https://docs.aws.amazon.com/cli/latest/reference/kinesis/index.html

** ストリームの作成 [#f5d2897c]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws kinesis create-stream --stream-name Foo --shard-count 1
}}
#html(</div>)

** ストリームにレコードを入力する [#lb7fcf90]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws kinesis put-record --stream-name Foo --partition-key 123 --data testdata
}}
#html(</div>)

** ストリームからレコードを取得する [#h87bb19b]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name Foo
}}
#html(</div>)

** ストリームの削除 [#tb880751]
#html(<div style="padding-left:10px">)
#myterm2(){{
aws kinesis delete-stream --stream-name Foo
}}
#html(</div>)

* S3 [#vf13dfbb]
#html(<div style="padding-left:10px">)

https://docs.aws.amazon.com/cli/latest/reference/s3api/
https://docs.aws.amazon.com/cli/latest/reference/s3/

** バケット一覧 [#ke5f26ef]
#myterm2(){{
aws s3api list-buckets --output text
}}

** バケット配下のファイル一覧 [#u839287f]
#myterm2(){{
aws s3api list-objects --bucket バケット名 --output text
}}

** オブジェクトの取得 [#bccdee3b]
#myterm2(){{
aws s3api get-object --bucket バケット名 --key キー名 出力先ファイルPATH
}}

** オブジェクトのアップロード [#o311c7e1]
#myterm2(){{
aws s3api put-object --bucket バケット名 --key キー名 --body アップロードするファイルのPATH
}}

#html(</div>)

* SSMパラメータストア [#n61860f1]
#html(<div style="padding-left:10px">)

https://docs.aws.amazon.com/cli/latest/reference/ssm/index.html#cli-aws-ssm

** パラメータの設定 [#maa6e233]
#myterm2(){{
aws ssm put-parameter --name パラメータ名 --value 値 --type [String|StringList|SecureString] --description "説明" --overwrite
}}

** パラメータ内容の確認 [#l18fa6f7]
#myterm2(){{
aws ssm get-parameter --name パラメータ名
}}

#html(</div>)


#html(</div>)



トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS