Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 0353a97

Browse files
committed
More README details + logging
1 parent 4a0369b commit 0353a97

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

README.md

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,83 @@
11
## AWS Lambda Container Image Converter
22

3-
This container image converter tool (img2lambda) repackages container images (such as Docker images) into AWS Lambda layers, and publishes them as new layer versions.
3+
This container image converter tool (img2lambda) repackages container images (such as Docker images) into AWS Lambda layers, and publishes them as new layer versions to Lambda. The tool copies all files under '/opt' in the Docker image, maintaining the individual Docker image layers as individual Lambda layers. The published layer ARNs will be stored in a file 'output/layers.json', which can be used as input when creating Lambda functions.
44

55
```
6+
USAGE:
7+
img2lambda [options]
8+
9+
GLOBAL OPTIONS:
10+
--image value, -i value Name of the source container image. For example, 'my-docker-image:latest'
11+
--region value, -r value AWS region (default: "us-east-1")
12+
--output-directory value, -o value Destination directory for command output (default: "./output")
13+
--layer-namespace value, -n value Prefix for the layers published to Lambda (default: "img2lambda")
14+
--dry-run, -d Conduct a dry-run: Repackage the image, but only write the Lambda layers to local disk (do not publish to Lambda)
15+
--help, -h show help
16+
```
17+
18+
## Build
19+
20+
```
21+
go get -t -v ./...
22+
```
23+
24+
## Example
25+
26+
Build the example Docker image to create a PHP Lambda custom runtime:
27+
```
28+
cd example
29+
630
docker build -t lambda-php .
31+
```
732

33+
The example PHP functions are also built into the example image, so they can be run with Docker:
34+
```
835
docker run lambda-php hello '{"name": "World"}'
936
1037
docker run lambda-php goodbye '{"name": "World"}'
38+
```
39+
40+
Run the tool to create and publish Lambda layers that contain the PHP custom runtime:
41+
```
42+
../../../bin/img2lambda -i lambda-php:latest -r us-east-1
43+
```
44+
45+
Create a PHP function that uses the layers:
46+
```
47+
cd function
1148
12-
../../bin/img2lambda -i docker-daemon:lambda-php:latest
49+
zip hello.zip src/hello.php
50+
51+
aws lambda create-function \
52+
--function-name php-example-hello \
53+
--handler hello \
54+
--zip-file fileb://./hello.zip \
55+
--runtime provided \
56+
--role "arn:aws:iam::XXXXXXXXXXXX:role/service-role/LambdaPhpExample" \
57+
--region us-east-1 \
58+
--layers file://../output/layers.json
59+
```
60+
61+
Finally, invoke the function:
1362
```
63+
aws lambda invoke \
64+
--function-name php-example-hello \
65+
--region us-east-1 \
66+
--log-type Tail \
67+
--query 'LogResult' \
68+
--output text \
69+
--payload '{"name": "World"}' hello-output.txt | base64 --decode
70+
71+
cat hello-output.txt
72+
```
73+
74+
## TODO
1475

15-
TODO:
1676
* Support image types other than local Docker images, where the layer format is tar. For example, layers directly from a Docker registry will be .tar.gz-formatted. OCI images can be either tar or tar.gz, based on the layer's media type.
1777
* De-dupe Lambda layers before publishing them (compare local file's SHA256 to published layer versions with the same name)
1878
* Accept additional parameters for PublishLayerVersion API (license, description, etc)
1979
* Support Lambda compatible runtimes other than 'provided'
20-
* Utility for creating a function deployment package from a Docker image
80+
* Utility for creating a function deployment package from a Docker image (copying from /var/task/src instead of /opt)
2181

2282
## License Summary
2383

publish_layers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"io/ioutil"
6+
"log"
67
"os"
78
"path/filepath"
89
"strings"
@@ -47,6 +48,8 @@ func PublishLambdaLayers(sourceImageName string, layers []LambdaLayer, region st
4748
if err != nil {
4849
return err
4950
}
51+
52+
log.Printf("Published Lambda layer file %s (image layer %s) to Lambda: %s", layer.File, layer.Digest, *resp.LayerVersionArn)
5053
}
5154

5255
jsonArns, err := json.MarshalIndent(layerArns, "", " ")
@@ -66,5 +69,7 @@ func PublishLambdaLayers(sourceImageName string, layers []LambdaLayer, region st
6669
return err
6770
}
6871

72+
log.Printf("Lambda layer ARNs (%d total) are written to %s", len(layerArns), resultsPath)
73+
6974
return nil
7075
}

repack_image.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"os"
78
"path/filepath"
89

@@ -20,6 +21,8 @@ type LambdaLayer struct {
2021

2122
// Converts container image to Lambda layer archive files
2223
func RepackImage(imageName string, layerOutputDir string) (layers []LambdaLayer, retErr error) {
24+
log.Printf("Parsing the docker image %s", imageName)
25+
2326
// Get image's layer data from image name
2427
ref, err := alltransports.ParseImageName(imageName)
2528
if err != nil {
@@ -53,6 +56,8 @@ func RepackImage(imageName string, layerOutputDir string) (layers []LambdaLayer,
5356

5457
layerInfos := src.LayerInfos()
5558

59+
log.Printf("Image %s has %d layers", imageName, len(layerInfos))
60+
5661
// Unpack and inspect each image layer, copy relevant files to new Lambda layer
5762
if err := os.MkdirAll(layerOutputDir, 0777); err != nil {
5863
return nil, err
@@ -75,10 +80,15 @@ func RepackImage(imageName string, layerOutputDir string) (layers []LambdaLayer,
7580
}
7681

7782
if fileCreated {
83+
log.Printf("Created Lambda layer file %s from image layer %s", lambdaLayerFilename, string(layerInfo.Digest))
7884
lambdaLayerNum++
7985
layers = append(layers, LambdaLayer{Digest: string(layerInfo.Digest), File: lambdaLayerFilename})
86+
} else {
87+
log.Printf("Did not create a Lambda layer file from image layer %s (no relevant files found)", string(layerInfo.Digest))
8088
}
8189
}
8290

91+
log.Printf("Created %d Lambda layer files for image %s", len(layers), imageName)
92+
8393
return layers, nil
8494
}

0 commit comments

Comments
 (0)