35
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

aws cli で Lambda を使う

Last updated at Posted at 2017-10-15

次の記事を参考にしました。
AWS-CLIで試すLambdaでのHelloWorld!

この記事との違いは、リージョンを、アジアパシフィック(東京)にしたことと、aws cli を、Arch Linux で使ったことです。

実行結果
Lambda で、Hello World を、CloudWatchLogsに出力します。
CloudWatch -> ログ

cloudwatch_oct1501.png

手順

1)S3バケットの作成

s3_create.sh
aws s3 mb s3://ekzemplaro-lambda-test --region ap-northeast-1

実行結果
S3

s3_oct1501.png

2)Lambda Exec Roleの作成

role-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
     {
       "Action": "sts:AssumeRole",
       "Principal": {
         "Service": "lambda.amazonaws.com"
        },
        "Effect": "Allow",
        "Sid": ""
     }
  ]
}
role_create.sh
aws iam create-role --role-name hello_exec_role \
        --assume-role-policy-document file://role-policy.json

hello_exec_role が作成されたことを確認
IAM -> ロール

role_oct1501.png

4)ロールにCloudWatchLogsへの書き込みを行う権限を付与

attach-policy.sh
aws iam attach-role-policy --role-name hello_exec_role \
	--policy-arn "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"

確認

aws iam list-attached-role-policies --role-name hello_exec_role
  1. Lambda function の作成
index.js
console.log('Loading function')

exports.handler = function(event, context) {
    console.log('value1 =', event.key1)
    console.log('value2 =', event.key2)
    console.log('value3 =', event.key3)
    console.log('hello world!')
    console.log('おはようございます。')
    context.succeed(event.key1);  // Echo back the first key value
    // context.fail('Something went wrong');
}

zip 圧縮します。

zip -r HelloWorld.zip index.js

作成

function_create.sh
aws lambda create-function \
--function-name hello_function \
--runtime nodejs14.x \
--role arn:aws:iam::123456789012:role/hello_exec_role \
--handler index.handler \
--zip-file fileb://HelloWorld.zip \
--region ap-northeast-1

作成されたことを確認
Lambda -> 関数

function_oct1501.png

  1. CLIからイベントを発火
cli_exec.sh
aws lambda invoke --invocation-type Event \
	--function-name hello_function --region ap-northeast-1 \
	--payload '{"key1":" こんにちは", "key2":"今晩は", "key3":"さようなら"}' \
 outputfile.txt

実行結果

$ ./cli_exec.sh 
{
    "StatusCode": 202
}
  1. CloudWatchLogsにログが出力されているか確認

CloudWatch -> ログ -> ロググループ

画像は、冒頭

8)手順の再確認の為に作成したものを削除

delete.sh
# delete lambda function
aws lambda delete-function --function-name hello_function \
	--region ap-northeast-1

#detach policy
aws iam detach-role-policy --role-name hello_exec_role \
	--policy-arn "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess"

#delete role
aws iam delete-role --role-name hello_exec_role

#delete S3 bucket
aws s3 rm s3://ekzemplaro-lambda-test --recursive --region ap-northeast-1
aws s3 rb s3://ekzemplaro-lambda-test --region ap-northeast-1

Python3 のサンプルはこちら
python の関数を AWS Lambda で使用する

35
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?