AWS Lambdaの実行環境で利用されるAMIが更新されると案内がありました。

Upcoming updates to the AWS Lambda and AWS Lambda@Edge execution environment | AWS Compute Blog
https://aws.amazon.com/jp/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/

AWS Lambda and AWS Lambda@Edge run on top of the Amazon Linux operating system distribution and maintain updates to both the core OS and managed language runtimes. We are updating the Lambda execution environment AMI to version 2018.03 of Amazon Linux. This newer AMI brings updates that offer improvements in capabilities, performance, security, and updated packages that your application code might interface with.

AWS LambdaとAWS Lambda @ Edgeは、Amazon Linuxオペレーティングシステムディストリビューションの最上位で実行され、コアOSランタイムとマネージ言語ランタイムの両方に対する更新を維持します。Lambda実行環境AMIをAmazon Linuxのバージョン2018.03に更新しています。この新しいAMIは、機能、パフォーマンス、セキュリティ、およびアプリケーションコードがインターフェイスする可能性のある更新パッケージの向上をもたらすアップデートをもたらします。(Google翻訳)

その後、更新スケジュールが変更になったものの、2019/6/11からは関数を新規作成すると新しい実行環境になるとありますが状況によっては古い環境で実行されるみたいでしたので調べてみました。

Updated timeframe for the upcoming AWS Lambda and AWS Lambda@Edge execution environment update | AWS Compute Blog
https://aws.amazon.com/jp/blogs/compute/updated-timeframe-for-the-upcoming-aws-lambda-and-aws-lambdaedge-execution-environment-update/

June 11, 2019 – New Function Create: All newly created functions will result in those functions running on the new execution environment, unless they have a delayed-update layer configured.

2019年6月11日 – 新規機能作成:遅延更新レイヤーが構成されていない限り、新規作成されたすべての機能により、それらの機能が新しい実行環境で実行されます。(Google翻訳)

前提

  • AWSアカウントがある
  • AWS CLIが利用可能
  • リージョンはバージニア北部( us-east-1 )

Node.js

Node.jsの場合、10.x はAmazon Linux 2が利用されるので、今回更新の対象外となります。(2019/06/20時点)

AWS Lambda Runtimes – AWS Lambda
https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html?icmpid=docs_lambda_rss

Upcoming updates to the AWS Lambda and AWS Lambda@Edge execution environment | AWS Compute Blog https://aws.amazon.com/jp/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/

This does not apply to the recently announced Node.js v10 runtime which today runs on Amazon Linux 2.

これは、最近発表されたAmazon Linux 2上のNode.js v10ランタイムには適用されません。(Google翻訳)

関数を新規作成する

AWS CLIでLambda関数をデプロイします。実装は下記のように関数内で、uname -acat /etc/system-releaseコマンドを実行してAMIのバージョンを確認するようにしました。

index.js

exports.handler = async (event) => {
    const execSync = require('child_process').execSync;

    const unameResult = execSync('uname -a');
    console.log(unameResult.toString());

    const catResult = execSync('cat /etc/system-release');
    console.log(catResult.toString());

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};
> mkdir 任意のディレクトリ
> cd 任意のディレクトリ

# 実行ロールの作成
> cat <<EOF >lambda-role-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF

> aws iam create-role \
  --role-name check-lambda-environment-role \
  --assume-role-policy-document file://lambda-role-policy.json

{
    "Role": {
        "Path": "/",
        "RoleName": "check-lambda-environment-role",
        "RoleId": "XXXXXXXXXXXXXXXXXXXXX",
        "Arn": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
        "CreateDate": "2019-06-20T04:01:40Z",
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Principal": {
                        "Service": "lambda.amazonaws.com"
                    },
                    "Action": "sts:AssumeRole"
                }
            ]
        }
    }
}


> touch index.js
# 内容は上記のコード

> zip index.zip index.js

> aws lambda create-function \
  --function-name check-nodejs-environment \
  --zip-file fileb://index.zip \
  --handler index.handler \
  --runtime nodejs8.10 \
  --role arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role

{
    "FunctionName": "check-nodejs-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-nodejs-environment",
    "Runtime": "nodejs8.10",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "index.handler",
    "CodeSize": 396,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T02:11:46.450+0000",
    "CodeSha256": "oGABm6ZtX98ncjxUuwiJqhPUul6fdQq3waJcaJqHJiE=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "3b8ee8d9-86ec-4d3a-bbfa-1e4ee0b9482f"
}

デプロイできたら関数を実行してみます。

> aws lambda invoke \
  --function-name check-nodejs-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 7920dfc4-f7ee-4edc-96ab-15f7e259ad10 Version: $LATEST
2019-06-20T02:24:38.757Z        7920dfc4-f7ee-4edc-96ab-15f7e259ad10    Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2019-06-20T02:24:38.778Z        7920dfc4-f7ee-4edc-96ab-15f7e259ad10    Amazon Linux AMI release 2018.03

END RequestId: 7920dfc4-f7ee-4edc-96ab-15f7e259ad10
REPORT RequestId: 7920dfc4-f7ee-4edc-96ab-15f7e259ad10  Duration: 24.06 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 28 MB

Amazon Linux AMI release 2018.03になっていることが確認できました。
次にランタイムをnodejs10.xに変更して実行してみます。

> aws lambda update-function-configuration \
  --function-name check-nodejs-environment \
  --runtime nodejs10.x

{
    "FunctionName": "check-nodejs-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-nodejs-environment",
    "Runtime": "nodejs10.x",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "index.handler",
    "CodeSize": 396,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:11:51.718+0000",
    "CodeSha256": "oGABm6ZtX98ncjxUuwiJqhPUul6fdQq3waJcaJqHJiE=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "7b306be7-a56d-431f-95c4-096d37d060b4"
}


> aws lambda invoke \
  --function-name check-nodejs-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 844951be-8b94-4f69-b678-5fca6b51048b Version: $LATEST
2019-06-20T03:12:27.412Z        844951be-8b94-4f69-b678-5fca6b51048b    INFO    Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
2019-06-20T03:12:27.512Z        844951be-8b94-4f69-b678-5fca6b51048b    INFO    Amazon Linux release 2 (Karoo)
END RequestId: 844951be-8b94-4f69-b678-5fca6b51048b
REPORT RequestId: 844951be-8b94-4f69-b678-5fca6b51048b  Duration: 387.86 ms     Billed Duration: 400 ms         Memory Size: 128 MB   Max Memory Used: 26 MB

想定通りにAmazon Linux release 2 (Karoo) となりました。
次が怪しい挙動となるのですが、ランタイムをnodejs8.10に戻して実行しています。

> aws lambda update-function-configuration \
  --function-name check-nodejs-environment \
  --runtime nodejs8.10

{
    "FunctionName": "check-nodejs-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-nodejs-environment",
    "Runtime": "nodejs8.10",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "index.handler",
    "CodeSize": 396,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:15:14.557+0000",
    "CodeSha256": "oGABm6ZtX98ncjxUuwiJqhPUul6fdQq3waJcaJqHJiE=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "4391a559-ba2b-4e70-97f5-b983988099a3"
}


> aws lambda invoke \
  --function-name check-nodejs-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 8814f1b7-d3c7-46f9-a562-dc55b408e3c2 Version: $LATEST
2019-06-20T03:15:51.173Z        8814f1b7-d3c7-46f9-a562-dc55b408e3c2    Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2019-06-20T03:15:51.311Z        8814f1b7-d3c7-46f9-a562-dc55b408e3c2    Amazon Linux AMI release 2017.03

END RequestId: 8814f1b7-d3c7-46f9-a562-dc55b408e3c2
REPORT RequestId: 8814f1b7-d3c7-46f9-a562-dc55b408e3c2  Duration: 366.43 ms     Billed Duration: 400 ms         Memory Size: 128 MB   Max Memory Used: 22 MB

Amazon Linux AMI release 2017.03になりました。
ブログ記事をみると2019/06/11から新規作成された関数、2019/06/25から新規・更新された関数と条件が付いてるので間違った挙動ではなさそうではあります。まだ新規作成した関数のみが新しい実行環境になるみたいです。(2019/06/20時点)

Upcoming updates to the AWS Lambda and AWS Lambda@Edge execution environment | AWS Compute Bloghttps://aws.amazon.com/jp/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/

June 11, 2019 – New Function Create: All newly created functions will result in those functions running on the new execution environment, unless they have a delayed-update layer configured.

June 25, 2019 – Existing Function Update: All newly created functions or existing functions that you update will result in those functions running on the new execution environment, unless they have a delayed-update layer configured.

2019年6月11日 – 新規機能作成:遅延更新レイヤーが構成されていない限り、新規作成されたすべての機能により、それらの機能が新しい実行環境で実行されます。

2019年6月25日 – 既存の関数の更新:更新されたすべての新しく作成された関数または既存の関数は、遅延更新層が構成されていない限り、新しい実行環境で実行されます。

オプトインしてみる

ブログ記事で今回用のLambdaレイヤーが用意されているとあるので、そちらを利用して新しい実行環境になるか確認してみます。
arn:aws:lambda:::awslayer:AmazonLinux1803のオプトイン用のレイヤーを追加すると新しい実行環境になるとのことです。コードサイズは0となっておりフラグ的に利用されるそうです。この仕組み自分でも使えたら便利そうな、どうなんでしょ?

> aws lambda update-function-configuration \
  --function-name check-nodejs-environment \
  --layers "arn:aws:lambda:::awslayer:AmazonLinux1803"

{
    "FunctionName": "check-nodejs-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-nodejs-environment",
    "Runtime": "nodejs8.10",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "index.handler",
    "CodeSize": 396,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:21:28.575+0000",
    "CodeSha256": "oGABm6ZtX98ncjxUuwiJqhPUul6fdQq3waJcaJqHJiE=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "414d41ec-98db-4588-8d1f-0c77fd50eef0",
    "Layers": [
        {
            "Arn": "arn:aws:lambda:::awslayer:AmazonLinux1803",
            "CodeSize": 0
        }
    ]
}


> aws lambda invoke \
  --function-name check-nodejs-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: dfb209e7-50ef-41b5-b960-60359e4566e4 Version: $LATEST
2019-06-20T03:22:25.570Z        dfb209e7-50ef-41b5-b960-60359e4566e4    Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2019-06-20T03:22:25.592Z        dfb209e7-50ef-41b5-b960-60359e4566e4    Amazon Linux AMI release 2018.03

END RequestId: dfb209e7-50ef-41b5-b960-60359e4566e4
REPORT RequestId: dfb209e7-50ef-41b5-b960-60359e4566e4  Duration: 49.56 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 28 MB

オプトインするとAmazon Linux AMI release 2018.03で実行されることが確認できました。
最後にレイヤーを外して確認してみます。

> aws lambda update-function-configuration \
  --function-name check-nodejs-environment \
  --layers []

{
    "FunctionName": "check-nodejs-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-nodejs-environment",
    "Runtime": "nodejs8.10",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "index.handler",
    "CodeSize": 396,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:31:43.734+0000",
    "CodeSha256": "oGABm6ZtX98ncjxUuwiJqhPUul6fdQq3waJcaJqHJiE=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "d1ca3a16-a01e-4f7e-8a30-00646c3c8d56"
}


> aws lambda invoke \
  --function-name check-nodejs-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 76dd533b-5491-47de-a6f3-4234d1baed7e Version: $LATEST
2019-06-20T03:32:48.012Z        76dd533b-5491-47de-a6f3-4234d1baed7e    Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2019-06-20T03:32:48.090Z        76dd533b-5491-47de-a6f3-4234d1baed7e    Amazon Linux AMI release 2017.03

END RequestId: 76dd533b-5491-47de-a6f3-4234d1baed7e
REPORT RequestId: 76dd533b-5491-47de-a6f3-4234d1baed7e  Duration: 186.55 ms     Billed Duration: 200 ms         Memory Size: 128 MB   Max Memory Used: 20 MB

Amazon Linux AMI release 2017.03に戻りました。

Python

Pythonでも確認してみます。Pythonの場合はすべてAmazon Linux となります。(2019/06/20時点)

AWS Lambda Runtimes – AWS Lambda

https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html?icmpid=docs_lambda_rss

関数を新規作成する

subprocess を利用して、uname -acat /etc/system-releaseコマンドを実行します。

main.py

import json
import subprocess

def lambda_handler(event, context):
    res = subprocess.check_output(['uname', '-a'])
    print(res)

    res2 = subprocess.check_output(['cat', '/etc/system-release'])
    print(res2)

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
> touch main.py
# 内容は上記のコード

> zip main.zip main.py

> aws lambda create-function \
  --function-name check-python-environment \
  --zip-file fileb://main.zip \
  --handler main.lambda_handler \
  --runtime python3.7 \
  --role arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:39:13.434+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "21591033-7aa1-4dc2-b977-cd39b2506b8a"
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: a1567ac2-7973-4495-b036-8e1ff325a6f4 Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2018.03\n'
END RequestId: a1567ac2-7973-4495-b036-8e1ff325a6f4
REPORT RequestId: a1567ac2-7973-4495-b036-8e1ff325a6f4  Duration: 64.31 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 35 MB

Amazon Linux AMI release 2018.03 になっていることが確認できました。

ランタイムを変更してみる

Node.jsと同じようにランタイムを変更すると新しい実行環境とならないのか確認してみます。

> aws lambda update-function-configuration \
  --function-name check-python-environment \
  --runtime python3.6

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.6",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:41:48.447+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "479990ce-9162-4393-b2d9-f7f8bca28605"
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 25765907-7d44-4a5a-b414-1ad942a87e62 Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2018.03\n'
END RequestId: 25765907-7d44-4a5a-b414-1ad942a87e62
REPORT RequestId: 25765907-7d44-4a5a-b414-1ad942a87e62  Duration: 46.01 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 26 MB

Amazon Linux AMI release 2018.03のままです。。。戻してみます。

> aws lambda update-function-configuration \
  --function-name check-python-environment \
  --runtime python3.7

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:43:10.933+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "82de01a8-a011-49f7-a0c6-adcf08f83161"
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 91a53cc6-bd67-418a-b583-b492f3410573 Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2018.03\n'
END RequestId: 91a53cc6-bd67-418a-b583-b492f3410573
REPORT RequestId: 91a53cc6-bd67-418a-b583-b492f3410573  Duration: 95.78 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 21 MB

Node.jsとは挙動が異なるようです。。。

オプトアウトしてみる

試しにオプトアウトしてみます。オプトイン時とはLambdaレイヤーのARNが異なるので注意します。

> aws lambda update-function-configuration \
  --function-name check-python-environment \
  --layers "arn:aws:lambda:::awslayer:AmazonLinux1703"

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:45:46.154+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "7b67d0e1-6c42-4edb-8be8-bbc12ebe46ff",
    "Layers": [
        {
            "Arn": "arn:aws:lambda:::awslayer:AmazonLinux1703",
            "CodeSize": 0
        }
    ]
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: a44a1cda-fb09-4092-a40a-3b1112c66b3b Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2017.03\n'
END RequestId: a44a1cda-fb09-4092-a40a-3b1112c66b3b
REPORT RequestId: a44a1cda-fb09-4092-a40a-3b1112c66b3b  Duration: 138.37 ms     Billed Duration: 200 ms         Memory Size: 128 MB   Max Memory Used: 21 MB

Amazon Linux AMI release 2017.03 となりオプトアウトできることが確認できました。
念の為レイヤーを外すとどうなるかみてみます。

> aws lambda update-function-configuration \
  --function-name check-python-environment \
  --layers []

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:47:42.043+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "dfcf4640-bff1-4018-b0d2-b72b25d64be5"
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: 48048135-8f2c-4fe4-a09c-baffea110561 Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2017.03\n'
END RequestId: 48048135-8f2c-4fe4-a09c-baffea110561
REPORT RequestId: 48048135-8f2c-4fe4-a09c-baffea110561  Duration: 70.53 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 21 MB

むむむ。Amazon Linux AMI release 2017.03のままとなりました。

オプトインしてみる

きになるので、最後にオプトインして戻してみます。

> aws lambda update-function-configuration \
  --function-name check-python-environment \
  --layers "arn:aws:lambda:::awslayer:AmazonLinux1803"

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:49:20.115+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "1e6a3849-3bb4-4be3-85d9-f2161adcfb88",
    "Layers": [
        {
            "Arn": "arn:aws:lambda:::awslayer:AmazonLinux1803",
            "CodeSize": 0
        }
    ]
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: fa55a4eb-3181-485f-9096-e8ccee582e09 Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2018.03\n'
END RequestId: fa55a4eb-3181-485f-9096-e8ccee582e09
REPORT RequestId: fa55a4eb-3181-485f-9096-e8ccee582e09  Duration: 71.20 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 21 MB


> aws lambda update-function-configuration \
  --function-name check-python-environment \
  --layers []

{
    "FunctionName": "check-python-environment",
    "FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:check-python-environment",
    "Runtime": "python3.7",
    "Role": "arn:aws:iam::xxxxxxxxxxxx:role/check-lambda-environment-role",
    "Handler": "main.lambda_handler",
    "CodeSize": 360,
    "Description": "",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2019-06-20T03:50:50.576+0000",
    "CodeSha256": "Ra/oq/0Wx9NILMmLQdYPuD2JjMg6kscBAKJCVm81Fz4=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "fc639f68-98d9-49db-9727-a36978fb3d7e"
}


> aws lambda invoke \
  --function-name check-python-environment \
  --log-type Tail \
  outputfile.txt \
  --query 'LogResult' | tr -d '"' | base64 -D

START RequestId: dc4e13e8-733b-446c-b5a8-d4033baaef7c Version: $LATEST
b'Linux ip-xxx-xxx-xxx-xxx 4.14.123-86.109.amzn1.x86_64 #1 SMP Mon Jun 10 19:44:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux\n'
b'Amazon Linux AMI release 2017.03\n'
END RequestId: dc4e13e8-733b-446c-b5a8-d4033baaef7c
REPORT RequestId: dc4e13e8-733b-446c-b5a8-d4033baaef7c  Duration: 79.33 ms      Billed Duration: 100 ms         Memory Size: 128 MB   Max Memory Used: 21 MB

予想通りというか、なんというかAmazon Linux AMI release 2017.03のままとなりました。

基本的にはブログ記事にあるように2019/06/25までは新規作成された関数は新しい実行環境となり、更新されるといままでの実行環境になるっぽいですが、Node.jsとPythonだと更新の定義が異なるっぽいです。詳細はわかりませんが。。。

まとめ?

2019/06/25からは関数更新のタイミングからも新しい実行環境が適用されるはずなので、今回のような挙動となることはなくなるとは思いますが、今後また新しい実行環境への切り替えが行われる際にはこういった挙動が起こり得るかも。。。というのを覚えておいたほうが良さそうです。

参考

Upcoming updates to the AWS Lambda and AWS Lambda@Edge execution environment | AWS Compute Blog
https://aws.amazon.com/jp/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/

Updated timeframe for the upcoming AWS Lambda and AWS Lambda@Edge execution environment update | AWS Compute Blog
https://aws.amazon.com/jp/blogs/compute/updated-timeframe-for-the-upcoming-aws-lambda-and-aws-lambdaedge-execution-environment-update/

AWS Lambda Runtimes – AWS Lambda
https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html?icmpid=docs_lambda_rss

lambda — AWS CLI 1.16.182 Command Reference
https://docs.aws.amazon.com/cli/latest/reference/lambda/index.html#cli-aws-lambda

AWS Lambda Layers – AWS Lambda
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

元記事はこちら

AWS Lambdaの実行環境(AMI)が新しくなるか確認してみた(Node.js、Python)