目次

よく使うオプション

オプション説明使用例
--regionリージョン--region ap-northeast-1
--endpoint-urlエンドポイント
※DynamoDB Local 等にコマンド発行する場合に利用
--endpoint-url http://localhost:8000
--output実行結果の出力形式--output json
--profile使用するプロファイル名--profile developper

http://docs.aws.amazon.com/cli/latest/reference/

名前付きプロファイルの作成

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 から ユーザとアクセスキーを作成しておく。

アカウントIDの取得/確認

aws sts get-caller-identity | grep Account | awk '{print $2}' | sed -e "s/[^0-9]//g"

Lambda

Lambda関数の作成

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

Lambda関数コードの変更

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

Lambda関数の削除

aws lambda delete-function \
 --function-name test-function \
 --region ap-northeast-1 \
--profile developper

Lambda関数の実行

 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

DynamoDB

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

テーブル一覧

aws dynamodb list-tables

テーブル作成

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 ) を作成

{
    "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を使用してテーブル作成

aws dynamodb create-table  --cli-input-json file://books.json

テーブル削除

aws dynamodb delete-table  --table-name Books

テーブル検索(全件検索)

aws dynamodb scan --table-name Books

テーブル検索(キー指定) --key-condition-expression

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"} }'

テーブル検索(グローバルセカンダリインデックスを使用して検索)

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"} }'

※取得できるのは、射影されているデータのみ

データ登録

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"} }'

データ更新

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!"} }'

尚、主キー項目の値変更はできない模様。
An error occurred (ValidationException) when calling the UpdateItem operation: This attribute is part of the key と怒られる。

データ削除

aws dynamodb delete-item --endpoint-url http://localhost:8000 --table-name ExampleTable \
  --table-name ExampleTable \
  --key '{ "key1": {"S":"A"}, "key2": {"S":"01"} }'

Kinesis

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

ストリームの作成

aws kinesis create-stream --stream-name Foo --shard-count 1

ストリームにレコードを入力する

aws kinesis put-record --stream-name Foo --partition-key 123 --data testdata

ストリームからレコードを取得する

aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name Foo

ストリームの削除

aws kinesis delete-stream --stream-name Foo

S3

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

バケット一覧

aws s3api list-buckets --output text

バケット配下のファイル一覧

aws s3api list-objects --bucket バケット名 --output text

オブジェクトの取得

aws s3api get-object --bucket バケット名 --key キー名 出力先ファイルPATH

オブジェクトのアップロード

aws s3api put-object --bucket バケット名 --key キー名 --body アップロードするファイルのPATH

SSMパラメータストア

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

パラメータの設定

aws ssm put-parameter --name パラメータ名 --value 値 --type [String|StringList|SecureString] --description "説明" --overwrite

パラメータ内容の確認

aws ssm get-parameter --name パラメータ名

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