This is a base of the AWS Lambda function in Golang and deploys it as a container image.
-
Clone repository
git clone https://github.com/michimani/base-of-lambda-container-image.git \ && cd base-of-lambda-container-image
-
Build the image for local
Use AWS base image.
docker build -t base-of-lambda-container-image:local -f Dockerfile_local .
Or, use alpine base image.
docker build -t base-of-lambda-container-image:local -f Dockerfile_alpine_local .
-
Run the image
docker run \ --rm \ -p 9000:8080 \ base-of-lambda-container-image:local
-
Invoke function
curl 'http://localhost:9000/2015-03-31/functions/function/invocations'
-
Build the image for production
docker build -t base-of-lambda-container-image:prod .
-
Create ECR repository
REGION='ap-northeast-1' \ && aws ecr create-repository \ --repository-name base-of-lambda-container-image \ --region ${REGION}
-
Login to ECR
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text) \ && aws ecr get-login-password --region ${REGION} \ | docker login \ --username AWS \ --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
-
Add image tag
docker tag base-of-lambda-container-image:prod ${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/base-of-lambda-container-image:latest
-
Push to ECR repository
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/base-of-lambda-container-image:latest
-
Create IAM Role for the function
ROLE_ARN=$( aws iam create-role \ --role-name role-for-base-of-lambda-container-image \ --assume-role-policy-document file://trust-policy.json \ --query 'Role.Arn' \ --output text )
-
Create Lambda Function from container image
aws lambda create-function \ --function-name "base-of-lambda-container-image" \ --package-type "Image" \ --code "ImageUri=${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/base-of-lambda-container-image:latest" \ --timeout 30 \ --role "${ROLE_ARN}" \ --region ${REGION}
-
Invoke function
aws lambda invoke \ --function-name base-of-lambda-container-image \ --invocation-type RequestResponse \ --region ${REGION} \ out \ && cat out | jq .
-
(Update function code)
aws lambda update-function-code \ --function-name base-of-lambda-container-image \ --image-uri "${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/base-of-lambda-container-image:latest"