오늘 할일
- CodeSeries로 Lambda Container이미지 자동배포해보기
- CloudFormation으로 자동화하기
CodeSeries로 Lambda Container이미지 자동배포해보기
어제까지 했던 것
어제 진행했던 부분까지 다시 생성하기
ECR생성

CodeCommit repository

Code Build



에러1


해결방안
역할 정책 수정
- 첫번째 CodeBuildBasePolict에 정책을 추가해준다.

- 미리 기록해두기위해 정책을 새로 만들어놨다. 이 정책을 복사해서 붙혀넣어준다.
CodeBuild_ECR 권한
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ECRPullPolicy",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
],
"Resource": [
"*"
]
},
{
"Sid": "ECRAuthPolicy",
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken"
],
"Resource": [
"*"
]
}
]
}

- 정책 수정 후 상태.

- 또,
AmazonEC2ContainerRegistryPowerUser
권한을 추가해준다.
빌드

- 권한을 설정했으니 빌드해본다.

- 테스트로 넣어본
aws
명령에서 에러가났다.
- aws 명령을 처리하는데 에러가 났고, command가 필요하다고 한다.
- aws자체에대한 에러는 나오지 않으니 기본적으로 AWS CLI가 존재하는것 같다.
- 테스트로 넣어놓은
aws
명령을 지우고 다시해본다.
에러2
에러 확인

- 먼저 ECR 로그인은 성공
- 도커 데몬에서 에러가 났다.
- Docker build가 안된다.
해결방안
CodeBuild에서 Lambda 이미지 deploy하기
CodeBuild역할에 Lambda관련정책 추가하기
신뢰관계 업데이트
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
},
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
역할에 정책 업데이트

AWSLambdaBasicExecutionRole
을 추가해준다.
Lambda함수 생성

Lambda함수 CLI로 배포
권한 추가
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": ["ecr:SetRepositoryPolicy","ecr:GetRepositoryPolicy"],
"Resource": "arn:aws:ecr:<region>:<account>:repository/<repo name>/"
},
{
"Sid": "ECRPullPolicy",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
],
"Resource": [
"arn:aws:ecr:<region>:<account>:repository/<repo name>/"
]
},
{
"Sid": "ECRAuthPolicy",
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken"
],
"Resource": [
"arn:aws:ecr:<region>:<account>:repository/<repo name>/"
]
},
ecr:SetRepositoryPolicy
,ecr:GetRepositoryPolicy
이 두 권한을 추가했다.
- 또, 권한을 추가하며 Resource를 특정 repository로 지정했다. 에러가나면
*
로 바꿀 것이다.
이미지 업데이트
해결

- 결국 region에 오타 문제였다.
ap-northeast-2a
-> ap-northeast-2
buildspec.yaml 수정
- 위에서 deploy에 성공한 코드를buildspec.yaml의 마지막에 추가해준다.
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- ls
- aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin <acount>.dkr.ecr.ap-northeast-2.amazonaws.com
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t cocudeny .
- docker tag cocudeny:latest <acount>.dkr.ecr.ap-northeast-2.amazonaws.com/cocudeny:latest
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push <acount>.dkr.ecr.ap-northeast-2.amazonaws.com/cocudeny:latest
- echo Deploy New Image to Lambda function...
- aws lambda update-function-code --region <region> --function-name <Lambda function name> --image-uri <ECR Image URI>
git add .
git commit -m "add deploy CLI code"
git push
에러
not authorized to perform: lambda:UpdateFunctionCode
에러가 출력되었다.
- 현재 Codebuild의 역할
codebuild-Image_Processing-service-role
에는 aws lambda update-function-code
를 실행할 권한이 없기 때문이다.
- 따라서 권한을 추가해준다.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lambda:UpdateFunctionCode"
],
"Resource": [
"*"
]
}
]
}
확인

- 성공적으로 이미지가 Deploy되었다.

- Lambda 함수가 업데이트중

- 출력

- 예상했던 출력
- 문제가 있다...
- 수정해야함