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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4916 | 웹서버 | 6년 전 | 2868 | ||
| 4915 | PHP |
|
6년 전 | 2185 | |
| 4914 | 웹서버 | 7년 전 | 1895 | ||
| 4913 | JavaScript | 7년 전 | 2599 | ||
| 4912 | node.js | 7년 전 | 3715 | ||
| 4911 | 기타 | 7년 전 | 4194 | ||
| 4910 | 기타 | 7년 전 | 2279 | ||
| 4909 | 기타 | 7년 전 | 1985 | ||
| 4908 | 기타 | 7년 전 | 2021 | ||
| 4907 | Mobile | 7년 전 | 2292 | ||
| 4906 | JavaScript | 7년 전 | 2293 | ||
| 4905 | 기타 | 7년 전 | 2274 | ||
| 4904 | jQuery | 7년 전 | 2520 | ||
| 4903 | PHP | 7년 전 | 5232 | ||
| 4902 | jQuery | 7년 전 | 5130 | ||
| 4901 | 기타 | 7년 전 | 2709 | ||
| 4900 | MySQL | 7년 전 | 4122 | ||
| 4899 | 기타 | 7년 전 | 2268 | ||
| 4898 | 웹서버 | 7년 전 | 2452 | ||
| 4897 | MySQL | 7년 전 | 2207 | ||
| 4896 | MySQL | 7년 전 | 2637 | ||
| 4895 | JavaScript | 7년 전 | 9776 | ||
| 4894 | 웹서버 | 7년 전 | 2365 | ||
| 4893 | 기타 | 7년 전 | 8321 | ||
| 4892 | jQuery | 7년 전 | 5688 | ||
| 4891 | 기타 | 7년 전 | 2838 | ||
| 4890 | PHP | 7년 전 | 3423 | ||
| 4889 | JavaScript | 7년 전 | 6378 | ||
| 4888 | MySQL | 7년 전 | 3139 | ||
| 4887 | MySQL | 7년 전 | 2767 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기