Skip to content

Instantly share code, notes, and snippets.

@3panda
Last active August 13, 2019 04:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3panda/9b8aa6dd2986d1b927d08ea92f6078f5 to your computer and use it in GitHub Desktop.
Save 3panda/9b8aa6dd2986d1b927d08ea92f6078f5 to your computer and use it in GitHub Desktop.
AWS CLIの操作などをまとめる※随時追加予定

インストール

pip install awscli

設定

AWS CLIの aws configure コマンドで認証情報ファイルを設定

default

aws configure
AWS Access Key ID [None]: *************ID
AWS Secret Access Key [None]: ******************************KEY
Default region name [None]: ap-northeast-1
Default output format [None]: json

プロファイル名を指定

--profileオプションでプロファイル名を指定可能。(無指定の場合はdefault)

$ aws configure --profile user001
AWS Access Key ID [None]: *************ID
AWS Secret Access Key [None]: ******************************KEY
Default region name [None]: ap-northeast-1
Default output format [None]: text

一時設定変更

export AWS_ACCESS_KEY_ID=*************ID
export AWS_SECRET_ACCESS_KEY=******************************KEY
export AWS_DEFAULT_REGION=ap-northeast-1
export AWS_DEFAULT_OUTPUT=json

~/.aws/credentials

[default]
aws_access_key_id=*************ID
aws_secret_access_key=******************************KEY
[user001]
aws_access_key_id = *************ID
aws_secret_access_key = ******************************KEY

設定確認

aws configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************AAA shared-credentials-file    
secret_key     ****************XXXX shared-credentials-file    
    region           ap-northeast-1      config-file    ~/.aws/config

userを指定して実行

前提

  • defaultにはec2を操作する権限無
  • user001がec2を起動する権限有

default(--profileの指定無し)

aws ec2 start-instances --instance-ids "<instance_id>" 
An error occurred (UnauthorizedOperation) when calling the StartInstances operation: You are not authorized to perform this operation. Encoded authorization failure message: 
XASDfjkjlkjldfjlklkjklk.......

特定のUserを指定

user001を指定してec2を起動

aws ec2 start-instances --instance-ids "<instance_id>" --profile user001

{
    "StartingInstances": [
        {
            "CurrentState": {
                "Code": 0,
                "Name": "pending"
            },
            "InstanceId": "i-xxxxxxxxxx",
            "PreviousState": {
                "Code": 80,
                "Name": "stopped"
            }
        }
    ]
}

lambda-uploader

lambda_function.py(Pythonの場合)などの実行ファイルを別環境で開発した場合にLambdaにUPするためのTool

インストール

 pip install lambda-uploader 

デプロイ

アップロードする内容のディレクトリ(例ではupload)に移動しファイルをup

cd upload
lambda-uploader

問題なければ以下の様な結果に

λ Building Package
λ Uploading Package
λ Fin

lambda.jsonのサンプル

※Runtimeを指定しておくと便利

{
  "name": "Name",
  "description": "Description",
  "region": "ap-northeast-1",
  "handler": "lambda_function.lambda_handler",
  "role": "arn:aws:iam::111111111111:role/XXXXX",
  "timeout": 300,
  "memory": 128,
  "runtime": "python3.6",
  "variables":
    {
      "TestVarA":"value",
      "TestVarB":"value"
    }
}

Lambda CLI create-function

Lambda CLI create-function コマンドを実行して Lambda 関数を作成 lambda-uploaderを使えない場合はこちらで

zipファイルを用意する

# uploadディレクトリ入る
cd upload
# ディレクトリの中身を全て含めたupload.zip作成
# ここでは一つ上のディレクトリに作成
# zipファイルはコマンドで作成しないと失敗する(※Macの場合はおそらく)
zip -r9 ../upload.zip *

Pathを指定して.zip ファイルをアップロード

Pathを指定して.zip ファイルをアップロードする ※lambda.jsonは読み込まない(おそらく)ので環境変数も指定しておく ※環境変数(environment Variables)を複数指定したい時は最後にしないと失敗する(調査中)

aws lambda create-function \
--region ap-northeast-1 \
--function-name function-name \
--zip-file fileb://{Path}xxx.zip \
--role arn:aws:iam::111111111111:role/XXXXX \
--handler lambda_function.lambda_handler \
--runtime python3.6 \
--profile xxxx\
--environment Variables="{VariableA=xxxx,VariableB=xxxxx}"

Amazon S3 バケットの.zip ファイルをアップロード

同じ AWS リージョンの Amazon S3 バケットに .zip ファイルをアップロード バケット名(bucketName)とオブジェクト名(xxx.zip)を指定

aws lambda create-function \
--region ap-northeast-1 \
--function-name function-name \
--code S3Bucket=bucketName,S3Key=xxx.zip \
--role arn:aws:iam::111111111111:role/XXXXX \
--handler lambda_function.lambda_handler \
--runtime python3.6 \
--profile xxxx\
--environment Variables="{VariableA=xxxx,VariableB=xxxxx}"

参考

AWS CLIのインストール

新人プログラマの為のAWS入門 ~実践編~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment