PHP에서 aws cli 를 이용하여 Lambda 함수 생성하기
요 며칠 회사 업무로 람다 함수를 서비스에 적용하기 위해 작업 중입니다.
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개
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4826 | jQuery | 8년 전 | 1672 | ||
| 4825 | JavaScript | 8년 전 | 2740 | ||
| 4824 | jQuery | 8년 전 | 1464 | ||
| 4823 | jQuery | 8년 전 | 1727 | ||
| 4822 | jQuery | 8년 전 | 1812 | ||
| 4821 | jQuery | 8년 전 | 1641 | ||
| 4820 | jQuery | 8년 전 | 1648 | ||
| 4819 | jQuery | 8년 전 | 1329 | ||
| 4818 | jQuery | 8년 전 | 1969 | ||
| 4817 | jQuery | 8년 전 | 2409 | ||
| 4816 | jQuery | 8년 전 | 1706 | ||
| 4815 | jQuery | 8년 전 | 1422 | ||
| 4814 | jQuery | 8년 전 | 1839 | ||
| 4813 | jQuery | 8년 전 | 5412 | ||
| 4812 | 기타 | 8년 전 | 3164 | ||
| 4811 | jQuery | 8년 전 | 1545 | ||
| 4810 | jQuery | 8년 전 | 1716 | ||
| 4809 | jQuery | 8년 전 | 1806 | ||
| 4808 | PHP |
|
8년 전 | 5085 | |
| 4807 | node.js |
|
8년 전 | 4120 | |
| 4806 | jQuery | 8년 전 | 2219 | ||
| 4805 | jQuery | 8년 전 | 1653 | ||
| 4804 | jQuery | 8년 전 | 1201 | ||
| 4803 | jQuery | 8년 전 | 1913 | ||
| 4802 | jQuery | 8년 전 | 1418 | ||
| 4801 | jQuery | 8년 전 | 1522 | ||
| 4800 | jQuery | 8년 전 | 1742 | ||
| 4799 | jQuery | 8년 전 | 1985 | ||
| 4798 | jQuery | 8년 전 | 1521 | ||
| 4797 | jQuery | 8년 전 | 1456 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기