편리

PHP에서 aws cli 를 이용하여 Lambda 함수 생성하기

· 2021-06-24 (목) 15:30:21 · 2986 · 4

요 며칠 회사 업무로 람다 함수를 서비스에 적용하기 위해 작업 중입니다.

AWS 콘솔에서 람다 함수를 생성해도 되지만 aws cli 를 사용하면 자동화가 가능하기 때문에 작업해봤습니다.

aws cli 리턴 값이 json 형태라서 그 처리를 위해 가장 익숙한 php 를 이용했습니다.

 

[code]

<?php

if ($argc < 3) {

    echo 'Wrong number of parameters.'.PHP_EOL;

    echo "Usage: php ".basename(__FILE__)." 'AWS_ACCESS_KEY_ID' 'AWS_SECRET_ACCESS_KEY'".PHP_EOL;

    exit;

}

 

$aws_access_key_id = $argv[1];

$aws_secrect_access_key = $argv[2];

 

$s3_access_key = 'S3_ACCESS_KEY';

$s3_secret_key = 'S3_SECRET_KEY';

 

putenv('AWS_ACCESS_KEY_ID='.$aws_access_key_id);

putenv('AWS_SECRET_ACCESS_KEY='.$aws_secrect_access_key);

putenv('AWS_DEFAULT_REGION=ap-northeast-2');

 

$func_name = 'my-func';

$layer_name = 'my-layer';

$zip_func_name = $func_name.'.zip';

$zip_layer_name = $layer_name.'.zip';

$role_arn = 'arn:aws:iam::11111222334464:role/lambda-role'; // AWS 콘솔에서 확인 필요

 

exec('zip -r '.$zip_func_name.' index.js');

 

mkdir('./nodejs', 0755);

rename('./node_modules', './nodejs/node_modules');

 

exec('zip -r '.$zip_layer_name.' ./nodejs');

 

$res = shell_exec('aws lambda create-function --function-name '.$func_name.' --zip-file fileb://'.$zip_func_name.' --handler index.handler --runtime nodejs14.x --role '.$role_arn);

 

$data = json_decode($res, true);

if (!$data) die($res.PHP_EOL);

 

$func_arn = $data['FunctionArn'];

if (!$func_arn) die('Function creation error.'.PHP_EOL);

 

echo 'Lambda Function created.'.PHP_EOL;

 

$res = shell_exec('aws lambda publish-layer-version --layer-name '.$layer_name.' --description "nodejs modules" --license-info "MIT" --zip-file  "fileb://'.$zip_layer_name.'" --compatible-runtimes nodejs14.x');

 

$data = json_decode($res, true);

if (!$data) die($res.PHP_EOL);

 

$layer_arn = $data['LayerVersionArn'];

if (!$layer_arn) die('Layer Creation error.'.PHP_EOL);

 

echo 'Layer created.'.PHP_EOL;

 

$res = shell_exec('aws lambda update-function-configuration --function-name '.$func_name.' --layers '.$layer_arn.' --memory-size 512 --timeout 900 --environment "Variables={S3_ACCESS_KEY='.$s3_access_key.',S3_SECRET_KEY='.$s3_secret_key.',S3_REGION=ap-northeast-2,S3_BUCKET=nodejs}"');

 

$data = json_decode($res, true);

if (!$data) die($res.PHP_EOL);

 

echo 'Lambda Function configuration updated.'.PHP_EOL;

 

echo 'Completed.'.PHP_EOL;

[/code]

 

함수코드와 노드 모듈을 하나의 zip 파일로 압축해서 처리해도 되지만 모듈의 경우는 다른 함수를 만든다면 다시 사용할 수도 있을 것이라 생각되어 Layer 로 분리해서 처리되도록 했습니다.

|

댓글 4개

좋은 정보 감사합니다.!
감사합니다.
2021-12-03 (금) 21:01:50
와우 좋은정보 감사합니다
2022-09-02 (금) 16:13:39
오..
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 19-06-24 조회 3,181
PHP 19-06-09 조회 3,800
MySQL 19-04-01 조회 4,850
jQuery 19-03-20 조회 4,236
node.js 19-02-12 조회 3,335
node.js 19-02-12 조회 3,070
node.js 19-02-12 조회 3,333
node.js 19-02-07 조회 3,292
node.js 19-02-07 조회 3,052
node.js 19-02-07 조회 3,185
node.js 19-02-04 조회 2,920
node.js 19-02-04 조회 2,909
node.js 19-02-04 조회 3,342
node.js 19-01-31 조회 2,451
node.js 19-01-31 조회 2,597
node.js 19-01-31 조회 3,094
node.js 19-01-28 조회 2,855
node.js 19-01-28 조회 2,939
node.js 19-01-28 조회 2,765
node.js 19-01-22 조회 3,107
node.js 19-01-22 조회 2,851
node.js 19-01-22 조회 3,288
node.js 19-01-16 조회 2,719
node.js
[node.js]
19-01-16 조회 2,634
node.js 19-01-16 조회 9,312
node.js 19-01-12 조회 4,370
node.js 19-01-12 조회 2,988
node.js 19-01-12 조회 3,117
node.js 19-01-11 조회 2,692
node.js 19-01-11 조회 4,009