I am trying to create a lambda function with the help of AWS CodeCommit and AWS CodeBuild. I tried this in zip mode, but it exceeds the zip threshold set by the lambda function (50MB). So now I’m trying to use the image mode. But somehow I’m not able to push the code into ECR.
Any help would be appreiciated.
My buildspec.yml
:
<code>version: 0.2
env:
variables:
AWS_DEFAULT_REGION: "us-east-1"
AWS_ACCOUNT_ID: ""
IMAGE_REPO_NAME: "deployment"
IMAGE_TAG: "latest"
S3_BUCKET: "deployment"
phases:
install:
runtime-versions:
python: 3.11
commands:
- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
- timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com || true
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"container-name","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- echo Updating Lambda function...
- sed -i 's|0123.dkr.ecr.us-east-1.amazonaws.com/agent-deployment|'$REPOSITORY_URI:$IMAGE_TAG'|' template.yaml
- pip install aws-sam-cli
- sam package --template-file template.yaml --s3-bucket $S3_BUCKET --output-template-file packaged.yaml
artifacts:
files:
- packaged.yaml
- imagedefinitions.json
</code>
<code>version: 0.2
env:
variables:
AWS_DEFAULT_REGION: "us-east-1"
AWS_ACCOUNT_ID: ""
IMAGE_REPO_NAME: "deployment"
IMAGE_TAG: "latest"
S3_BUCKET: "deployment"
phases:
install:
runtime-versions:
python: 3.11
commands:
- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
- timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com || true
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"container-name","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- echo Updating Lambda function...
- sed -i 's|0123.dkr.ecr.us-east-1.amazonaws.com/agent-deployment|'$REPOSITORY_URI:$IMAGE_TAG'|' template.yaml
- pip install aws-sam-cli
- sam package --template-file template.yaml --s3-bucket $S3_BUCKET --output-template-file packaged.yaml
artifacts:
files:
- packaged.yaml
- imagedefinitions.json
</code>
version: 0.2
env:
variables:
AWS_DEFAULT_REGION: "us-east-1"
AWS_ACCOUNT_ID: ""
IMAGE_REPO_NAME: "deployment"
IMAGE_TAG: "latest"
S3_BUCKET: "deployment"
phases:
install:
runtime-versions:
python: 3.11
commands:
- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
- timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com || true
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"container-name","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- echo Updating Lambda function...
- sed -i 's|0123.dkr.ecr.us-east-1.amazonaws.com/agent-deployment|'$REPOSITORY_URI:$IMAGE_TAG'|' template.yaml
- pip install aws-sam-cli
- sam package --template-file template.yaml --s3-bucket $S3_BUCKET --output-template-file packaged.yaml
artifacts:
files:
- packaged.yaml
- imagedefinitions.json
My template.yml :
<code>AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: 0123.dkr.ecr.us-east-1.amazonaws.com/agent-deployment
MemorySize: 128
Timeout: 30
Environment:
Variables:
PARAM1: "value1"
Events:
HttpApi:
Type: HttpApi
Properties:
Path: /
Method: GET
Outputs:
LambdaFunction:
Description: "Lambda Function ARN"
Value: !GetAtt MyLambdaFunction.Arn
LambdaFunctionUrl:
Description: "Lambda Function URL"
Value: !GetAtt MyLambdaFunction.FunctionUrl
</code>
<code>AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: 0123.dkr.ecr.us-east-1.amazonaws.com/agent-deployment
MemorySize: 128
Timeout: 30
Environment:
Variables:
PARAM1: "value1"
Events:
HttpApi:
Type: HttpApi
Properties:
Path: /
Method: GET
Outputs:
LambdaFunction:
Description: "Lambda Function ARN"
Value: !GetAtt MyLambdaFunction.Arn
LambdaFunctionUrl:
Description: "Lambda Function URL"
Value: !GetAtt MyLambdaFunction.FunctionUrl
</code>
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
ImageUri: 0123.dkr.ecr.us-east-1.amazonaws.com/agent-deployment
MemorySize: 128
Timeout: 30
Environment:
Variables:
PARAM1: "value1"
Events:
HttpApi:
Type: HttpApi
Properties:
Path: /
Method: GET
Outputs:
LambdaFunction:
Description: "Lambda Function ARN"
Value: !GetAtt MyLambdaFunction.Arn
LambdaFunctionUrl:
Description: "Lambda Function URL"
Value: !GetAtt MyLambdaFunction.FunctionUrl
Logs :