きょこみのーと

技術に関係ないほうのブログ

Amazon Lambdaにデフォルトでインストールされているライブラリを最新にして使う方法の1つ

業務でAmazonLambda上でPDFを画像に分割処理を実装したのですが、その時ghostscriptのVersionが古くて、 特定のPDF変換でエラーになるという現象でハマったのでその時調べたことと、 対応策を紹介したいと思います。

前提

  • Apex (Go)で動かしてます
  • ImageMagickがghostscriptを呼び出している
  • ghostscriptが古くてエラー

Apexについては前に書いた記事をご覧ください。

kyokomi.hatenablog.com

Lambdaのamiについて

Amazon Linux AMI 2014.09 Packages

ghostscript-8.70
ghostscript-fonts-5.50

フォーラムでの質問

https://forums.aws.amazon.com/message.jspa?messageID=611159

AWS Lambda doesn't provide a way for functions to update the Amazon Linux AMI. 
As has been suggested, you can include alternative versions of libraries, executables, 
and language runtimes in your function's ZIP file if the default ones available don't address your needs.

We'll also be looking at ways to make that process easier in the future through simplified deployment options. 
Until then, apologies for the inconvenience of having to copy this in with your code.
  • AWS LambdaではAMIをupdateする術を提供してない
  • もしversionを更新したいならzipに含んで頑張ってくれ

と解釈。/(^o^)\

解決策

functionの構成

pdf2images
├── bin
│   └── gs            // 上記のdownloadしたバイナリ
└── main.go

コード抜粋

func main() {
    apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
        os.Setenv("PATH", os.Getenv("LAMBDA_TASK_ROOT")+"/bin"+":"+os.Getenv("PATH"))
 
        // TODO: 〜 色々省略 〜

        outputLog, err := exec.Command("convert", "/tmp/input/hoge.pdf", "/tmp/output/hoge.png").CombinedOutput()
        if err != nil {
            return nil, err
        }
        log.Println(string(outputLog))

        return nil, nil
    })
}