@@ -17,8 +17,29 @@ if ! ls build-artifacts/aws-lambda-ric-*.tgz 1> /dev/null 2>&1; then
1717 exit 1
1818fi
1919
20- # Build Lambda image
21- docker build -t ${IMAGE_NAME} :${TAG} -f Dockerfile.lambda .
20+ # Detect architecture from the Docker image that was built
21+ DOCKER_ARCH=$( docker inspect ric/nodejs-js:latest --format ' {{.Architecture}}' 2> /dev/null || echo " " )
22+ if [[ " $DOCKER_ARCH " == " arm64" ]]; then
23+ LAMBDA_ARCH=" arm64"
24+ PLATFORM=" linux/arm64"
25+ elif [[ " $DOCKER_ARCH " == " amd64" ]]; then
26+ LAMBDA_ARCH=" x86_64"
27+ PLATFORM=" linux/amd64"
28+ else
29+ # Fallback to host architecture
30+ HOST_ARCH=$( uname -m)
31+ if [[ " $HOST_ARCH " == " arm64" || " $HOST_ARCH " == " aarch64" ]]; then
32+ LAMBDA_ARCH=" arm64"
33+ PLATFORM=" linux/arm64"
34+ else
35+ LAMBDA_ARCH=" x86_64"
36+ PLATFORM=" linux/amd64"
37+ fi
38+ fi
39+ echo " Detected architecture: $LAMBDA_ARCH (platform: $PLATFORM )"
40+
41+ # Build Lambda image (--provenance=false ensures Docker v2 format compatible with Lambda)
42+ docker build --provenance=false --platform " ${PLATFORM} " -t ${IMAGE_NAME} :${TAG} -f Dockerfile.lambda .
2243
2344# Check if ECR repository exists, create if it doesn't
2445if ! aws ecr describe-repositories --region ${AWS_REGION} --repository-names ${ECR_REPO_NAME} & > /dev/null; then
@@ -74,19 +95,39 @@ ROLE_ARN=$(aws iam get-role --region ${AWS_REGION} --role-name ${ROLE_NAME} --qu
7495
7596# Check if the function exists
7697if aws lambda get-function --region ${AWS_REGION} --function-name ${FUNCTION_NAME} & > /dev/null; then
77- echo " Updating existing Lambda function ${FUNCTION_NAME} "
78- aws lambda update-function-code \
79- --region ${AWS_REGION} \
80- --function-name ${FUNCTION_NAME} \
81- --image-uri ${ECR_URI} /${ECR_REPO_NAME} :${TAG}
98+ # Check if architecture matches
99+ CURRENT_ARCH=$( aws lambda get-function-configuration --region ${AWS_REGION} --function-name ${FUNCTION_NAME} --query ' Architectures[0]' --output text)
100+ if [[ " $CURRENT_ARCH " != " $LAMBDA_ARCH " ]]; then
101+ echo " Architecture mismatch: Lambda is $CURRENT_ARCH , image is $LAMBDA_ARCH "
102+ echo " Deleting and recreating function with correct architecture..."
103+ aws lambda delete-function --region ${AWS_REGION} --function-name ${FUNCTION_NAME}
104+ sleep 5
105+ echo " Creating Lambda function ${FUNCTION_NAME} with architecture ${LAMBDA_ARCH} "
106+ aws lambda create-function \
107+ --region ${AWS_REGION} \
108+ --function-name ${FUNCTION_NAME} \
109+ --package-type Image \
110+ --code ImageUri=${ECR_URI} /${ECR_REPO_NAME} :${TAG} \
111+ --role ${ROLE_ARN} \
112+ --architectures ${LAMBDA_ARCH} \
113+ --timeout 30 \
114+ --memory-size 128
115+ else
116+ echo " Updating existing Lambda function ${FUNCTION_NAME} "
117+ aws lambda update-function-code \
118+ --region ${AWS_REGION} \
119+ --function-name ${FUNCTION_NAME} \
120+ --image-uri ${ECR_URI} /${ECR_REPO_NAME} :${TAG}
121+ fi
82122else
83- echo " Creating new Lambda function ${FUNCTION_NAME} "
123+ echo " Creating new Lambda function ${FUNCTION_NAME} with architecture ${LAMBDA_ARCH} "
84124 aws lambda create-function \
85125 --region ${AWS_REGION} \
86126 --function-name ${FUNCTION_NAME} \
87127 --package-type Image \
88128 --code ImageUri=${ECR_URI} /${ECR_REPO_NAME} :${TAG} \
89129 --role ${ROLE_ARN} \
130+ --architectures ${LAMBDA_ARCH} \
90131 --timeout 30 \
91132 --memory-size 128
92133fi
0 commit comments