|
| 1 | +# aws-lambda-go |
| 2 | +An example of using the Chainguard `python` image as a base image for an AWS |
| 3 | +Lambda function as described in [this documentation](https://docs.aws.amazon.com/lambda/latest/dg/go-image.html#go-image-other) |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +You should have these utilities installed: |
| 8 | + |
| 9 | +- `aws` |
| 10 | +- `docker` |
| 11 | +- `jq` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Export a name for your Lambda function. We'll use this value in later steps. |
| 16 | + |
| 17 | +``` |
| 18 | +$ export FUNCTION_NAME=chainguard-python-lambda |
| 19 | +``` |
| 20 | + |
| 21 | +Login to AWS. This may be different depending on how you authenticate to AWS. |
| 22 | + |
| 23 | +``` |
| 24 | +$ aws sso login |
| 25 | +``` |
| 26 | + |
| 27 | +Export the AWS account ID and region. We'll use these values in later steps. |
| 28 | + |
| 29 | +``` |
| 30 | +$ export AWS_REGION=us-west-2 |
| 31 | +$ export ACCOUNT_ID=$(aws sts get-caller-identity | jq -r .Account) |
| 32 | +``` |
| 33 | + |
| 34 | +Create an AWS ECR repository. |
| 35 | + |
| 36 | +``` |
| 37 | +$ aws ecr create-repository --repository-name "${FUNCTION_NAME}" |
| 38 | +``` |
| 39 | + |
| 40 | +Login to AWS ECR. |
| 41 | + |
| 42 | +``` |
| 43 | +$ aws ecr get-login-password \ |
| 44 | + | docker login \ |
| 45 | + --username AWS \ |
| 46 | + --password-stdin "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com" |
| 47 | +``` |
| 48 | + |
| 49 | +Build and push the image. |
| 50 | + |
| 51 | +``` |
| 52 | +$ docker buildx build \ |
| 53 | + --push \ |
| 54 | + --platform linux/amd64 \ |
| 55 | + --provenance=false \ |
| 56 | + -t "${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${FUNCTION_NAME}:latest" \ |
| 57 | + . |
| 58 | +``` |
| 59 | + |
| 60 | +Create a role for the Lambda. |
| 61 | + |
| 62 | +``` |
| 63 | +$ aws iam create-role \ |
| 64 | + --role-name "${FUNCTION_NAME}" \ |
| 65 | + --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' |
| 66 | +``` |
| 67 | + |
| 68 | +Create the Lambda function. |
| 69 | + |
| 70 | +``` |
| 71 | +$ aws lambda create-function \ |
| 72 | + --function-name "${FUNCTION_NAME}" \ |
| 73 | + --package-type Image \ |
| 74 | + --code "ImageUri=${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${FUNCTION_NAME}:latest" \ |
| 75 | + --role "arn:aws:iam::${ACCOUNT_ID}:role/${FUNCTION_NAME}" |
| 76 | +``` |
| 77 | + |
| 78 | +Invoke the Lambda function. |
| 79 | + |
| 80 | +``` |
| 81 | +$ aws lambda invoke --function-name "${FUNCTION_NAME}" /dev/stderr >/dev/null |
| 82 | +``` |
| 83 | + |
| 84 | +The output should look similar to this: |
| 85 | +``` |
| 86 | +{"statusCode":200,"headers":null,"multiValueHeaders":null,"body":"\"Hello from Lambda!\""} |
| 87 | +``` |
| 88 | + |
| 89 | +## Clean Up |
| 90 | + |
| 91 | +Delete the Lambda function. |
| 92 | + |
| 93 | +``` |
| 94 | +$ aws lambda delete-function --function-name "${FUNCTION_NAME}" |
| 95 | +``` |
| 96 | + |
| 97 | +Delete the IAM role. |
| 98 | + |
| 99 | +``` |
| 100 | +$ aws iam delete-role --role-name "${FUNCTION_NAME}" |
| 101 | +``` |
| 102 | + |
| 103 | +Delete the ECR repository. |
| 104 | + |
| 105 | +``` |
| 106 | +$ aws ecr delete-repository --repository-name "${FUNCTION_NAME}" --force |
| 107 | +``` |
0 commit comments