Skip to content

michimani/base-of-lambda-container-image

Repository files navigation

base-of-lambda-container-image

This is a base of the AWS Lambda function in Golang and deploys it as a container image.

Usage

  1. Clone repository

    git clone https://github.com/michimani/base-of-lambda-container-image.git \
    && cd base-of-lambda-container-image
  2. 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 at local

  1. Run the image

    docker run \
    --rm \
    -p 9000:8080 \
    base-of-lambda-container-image:local
  2. Invoke function

    curl 'http://localhost:9000/2015-03-31/functions/function/invocations'

Deploy to Lambda

  1. Build the image for production

    docker build -t base-of-lambda-container-image:prod .
  2. Create ECR repository

    REGION='ap-northeast-1' \
    && aws ecr create-repository \
    --repository-name base-of-lambda-container-image \
    --region ${REGION}
  3. 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
  4. 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
  5. Push to ECR repository

    docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/base-of-lambda-container-image:latest
  6. 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
    )
  7. 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}
  8. Invoke function

    aws lambda invoke \
    --function-name base-of-lambda-container-image \
    --invocation-type RequestResponse \
    --region ${REGION} \
    out \
    && cat out | jq .
  9. (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"

About

This is a base of the AWS Lambda function in Go and deploys it as a container image.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published