Go lambda のデフォルト環境変数

# はじめに
`AWS SDK for Go`をLambdaで使用する際に特に設定無しで使用できるので
多分アクセスキーとか環境変数にセットされているんだろうなーと思いつつも
実際に試したわけではないので検証してみました。

# 検証
## 検証コード

```Go
package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-lambda-go/lambda"
	"github.com/aws/aws-lambda-go/lambdacontext"
	"log"
	"os"
)

func init() {
	log.Print("init start")
	showAllEnv()
	log.Print("init end")
}

func Handler(ctx context.Context) (string, error) {
	log.Print("handler start")
	if lambdaCtx, ok := lambdacontext.FromContext(ctx); ok {
		log.Print(fmt.Sprintf("Context AwsRequestID: %v",lambdaCtx.AwsRequestID))
		log.Print(fmt.Sprintf("Context Identity: %v",lambdaCtx.Identity))
		log.Print(fmt.Sprintf("Context InvokedFunctionArn: %v",lambdaCtx.InvokedFunctionArn))
		log.Print(fmt.Sprintf("Context Custom: %v",lambdaCtx.ClientContext.Custom))
		log.Print(fmt.Sprintf("Context Env: %v",lambdaCtx.ClientContext.Env))
		log.Print(fmt.Sprintf("Context Client: %v",lambdaCtx.ClientContext.Client))
	}
	showAllEnv()
	log.Print("handler end")
	return "success", nil
}

func main() {
	log.Print("main start")
	showAllEnv()
	lambda.Start(Handler)
	log.Print("main end")
}

func showAllEnv() {
	log.Print("-showAllEnv start-----------------------------")
	for _, e := range os.Environ() {
		log.Println(e)
	}
	log.Print("-showAllEnv end--------------------------------")
}

```

<details><summary>実行ログ（長いので折りたたみ）</summary><div>

```sh
2020/01/27 06:41:34 init start
2020/01/27 06:41:34 -showAllEnv start-----------------------------
2020/01/27 06:41:34 PATH=/usr/local/bin:/usr/bin/:/bin:/opt/bin
2020/01/27 06:41:34 LD_LIBRARY_PATH=/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib
2020/01/27 06:41:34 LANG=en_US.UTF-8
2020/01/27 06:41:34 TZ=:UTC
2020/01/27 06:41:34 _LAMBDA_CONTROL_SOCKET=18
2020/01/27 06:41:34 _LAMBDA_CONSOLE_SOCKET=20
2020/01/27 06:41:34 LAMBDA_TASK_ROOT=/var/task
2020/01/27 06:41:34 LAMBDA_RUNTIME_DIR=/var/runtime
2020/01/27 06:41:34 _LAMBDA_LOG_FD=27
2020/01/27 06:41:34 _LAMBDA_SB_ID=0
2020/01/27 06:41:34 _LAMBDA_SHARED_MEM_FD=15
2020/01/27 06:41:34 AWS_REGION=ap-northeast-1
2020/01/27 06:41:34 AWS_DEFAULT_REGION=ap-northeast-1
2020/01/27 06:41:34 AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/<functionName>
2020/01/27 06:41:34 AWS_LAMBDA_LOG_STREAM_NAME=2020/01/27/[$LATEST]ebf3ce00674e4576a679be48a4655a2f
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_NAME=<functionName>
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_MEMORY_SIZE=512
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_VERSION=$LATEST
2020/01/27 06:41:34 _AWS_XRAY_DAEMON_ADDRESS=169.254.79.2
2020/01/27 06:41:34 _AWS_XRAY_DAEMON_PORT=2000
2020/01/27 06:41:34 AWS_XRAY_DAEMON_ADDRESS=169.254.79.2:2000
2020/01/27 06:41:34 AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
2020/01/27 06:41:34 _X_AMZN_TRACE_ID=Parent=01314153196111bc
2020/01/27 06:41:34 AWS_EXECUTION_ENV=AWS_Lambda_go1.x
2020/01/27 06:41:34 _HANDLER=main
2020/01/27 06:41:34 _LAMBDA_RUNTIME_LOAD_TIME=1234056831378
2020/01/27 06:41:34 _LAMBDA_SERVER_PORT=33395
2020/01/27 06:41:34 AWS_ACCESS_KEY=<AWS AccessKey>
2020/01/27 06:41:34 AWS_ACCESS_KEY_ID=<AWS AccessKey>
2020/01/27 06:41:34 AWS_SECRET_KEY=<AWS SecretAccessKey>
2020/01/27 06:41:34 AWS_SECRET_ACCESS_KEY=<AWS SecretAccessKey>
2020/01/27 06:41:34 AWS_SESSION_TOKEN=<AWS SessionToken>
2020/01/27 06:41:34 AWS_SECURITY_TOKEN=<AWS SessionToken>
2020/01/27 06:41:34 -showAllEnv end--------------------------------
2020/01/27 06:41:34 init end
2020/01/27 06:41:34 main start
2020/01/27 06:41:34 -showAllEnv start-----------------------------
2020/01/27 06:41:34 PATH=/usr/local/bin:/usr/bin/:/bin:/opt/bin
2020/01/27 06:41:34 LD_LIBRARY_PATH=/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib
2020/01/27 06:41:34 LANG=en_US.UTF-8
2020/01/27 06:41:34 TZ=:UTC
2020/01/27 06:41:34 _LAMBDA_CONTROL_SOCKET=18
2020/01/27 06:41:34 _LAMBDA_CONSOLE_SOCKET=20
2020/01/27 06:41:34 LAMBDA_TASK_ROOT=/var/task
2020/01/27 06:41:34 LAMBDA_RUNTIME_DIR=/var/runtime
2020/01/27 06:41:34 _LAMBDA_LOG_FD=27
2020/01/27 06:41:34 _LAMBDA_SB_ID=0
2020/01/27 06:41:34 _LAMBDA_SHARED_MEM_FD=15
2020/01/27 06:41:34 AWS_REGION=ap-northeast-1
2020/01/27 06:41:34 AWS_DEFAULT_REGION=ap-northeast-1
2020/01/27 06:41:34 AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/<functionName>
2020/01/27 06:41:34 AWS_LAMBDA_LOG_STREAM_NAME=2020/01/27/[$LATEST]ebf3ce00674e4576a679be48a4655a2f
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_NAME=<functionName>
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_MEMORY_SIZE=512
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_VERSION=$LATEST
2020/01/27 06:41:34 _AWS_XRAY_DAEMON_ADDRESS=169.254.79.2
2020/01/27 06:41:34 _AWS_XRAY_DAEMON_PORT=2000
2020/01/27 06:41:34 AWS_XRAY_DAEMON_ADDRESS=169.254.79.2:2000
2020/01/27 06:41:34 AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
2020/01/27 06:41:34 _X_AMZN_TRACE_ID=Parent=01314153196111bc
2020/01/27 06:41:34 AWS_EXECUTION_ENV=AWS_Lambda_go1.x
2020/01/27 06:41:34 _HANDLER=main
2020/01/27 06:41:34 _LAMBDA_RUNTIME_LOAD_TIME=1234056831378
2020/01/27 06:41:34 _LAMBDA_SERVER_PORT=33395
2020/01/27 06:41:34 AWS_ACCESS_KEY=<AWS AccessKey>
2020/01/27 06:41:34 AWS_ACCESS_KEY_ID=<AWS AccessKey>
2020/01/27 06:41:34 AWS_SECRET_KEY=<AWS SecretAccessKey>
2020/01/27 06:41:34 AWS_SECRET_ACCESS_KEY=<AWS SecretAccessKey>
2020/01/27 06:41:34 AWS_SESSION_TOKEN=<AWS SessionToken>
2020/01/27 06:41:34 AWS_SECURITY_TOKEN=<AWS SessionToken>
2020/01/27 06:41:34 -showAllEnv end--------------------------------
START RequestId: 3fec0fa8-4f1b-41a0-a24a-83300a468c5b Version: $LATEST
2020/01/27 06:41:34 handler start
2020/01/27 06:41:34 Context AwsRequestID: 3fec0fa8-4f1b-41a0-a24a-83300a468c5b
2020/01/27 06:41:34 Context Identity: 
{}

2020/01/27 06:41:34 Context InvokedFunctionArn: arn:aws:lambda:ap-northeast-1:<AccountId>:function:<functionName>
2020/01/27 06:41:34 Context Custom: map[]
2020/01/27 06:41:34 Context Env: map[]
2020/01/27 06:41:34 Context Client: 
{}

2020/01/27 06:41:34 -showAllEnv start-----------------------------
2020/01/27 06:41:34 PATH=/usr/local/bin:/usr/bin/:/bin:/opt/bin
2020/01/27 06:41:34 LD_LIBRARY_PATH=/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib
2020/01/27 06:41:34 LANG=en_US.UTF-8
2020/01/27 06:41:34 TZ=:UTC
2020/01/27 06:41:34 _LAMBDA_CONTROL_SOCKET=18
2020/01/27 06:41:34 _LAMBDA_CONSOLE_SOCKET=20
2020/01/27 06:41:34 LAMBDA_TASK_ROOT=/var/task
2020/01/27 06:41:34 LAMBDA_RUNTIME_DIR=/var/runtime
2020/01/27 06:41:34 _LAMBDA_LOG_FD=27
2020/01/27 06:41:34 _LAMBDA_SB_ID=0
2020/01/27 06:41:34 _LAMBDA_SHARED_MEM_FD=15
2020/01/27 06:41:34 AWS_REGION=ap-northeast-1
2020/01/27 06:41:34 AWS_DEFAULT_REGION=ap-northeast-1
2020/01/27 06:41:34 AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/<functionName>
2020/01/27 06:41:34 AWS_LAMBDA_LOG_STREAM_NAME=2020/01/27/[$LATEST]ebf3ce00674e4576a679be48a4655a2f
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_NAME=<functionName>
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_MEMORY_SIZE=512
2020/01/27 06:41:34 AWS_LAMBDA_FUNCTION_VERSION=$LATEST
2020/01/27 06:41:34 _AWS_XRAY_DAEMON_ADDRESS=169.254.79.2
2020/01/27 06:41:34 _AWS_XRAY_DAEMON_PORT=2000
2020/01/27 06:41:34 AWS_XRAY_DAEMON_ADDRESS=169.254.79.2:2000
2020/01/27 06:41:34 AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
2020/01/27 06:41:34 _X_AMZN_TRACE_ID=Parent=01314153196111bc
2020/01/27 06:41:34 AWS_EXECUTION_ENV=AWS_Lambda_go1.x
2020/01/27 06:41:34 _HANDLER=main
2020/01/27 06:41:34 _LAMBDA_RUNTIME_LOAD_TIME=1234056831378
2020/01/27 06:41:34 _LAMBDA_SERVER_PORT=33395
2020/01/27 06:41:34 AWS_ACCESS_KEY=<AWS AccessKey>
2020/01/27 06:41:34 AWS_ACCESS_KEY_ID=<AWS AccessKey>
2020/01/27 06:41:34 AWS_SECRET_KEY=<AWS SecretAccessKey>
2020/01/27 06:41:34 AWS_SECRET_ACCESS_KEY=<AWS SecretAccessKey>
2020/01/27 06:41:34 AWS_SESSION_TOKEN=<AWS SessionToken>
2020/01/27 06:41:34 AWS_SECURITY_TOKEN=<AWS SessionToken>
2020/01/27 06:41:34 -showAllEnv end--------------------------------
2020/01/27 06:41:34 handler end
END RequestId: 3fec0fa8-4f1b-41a0-a24a-83300a468c5b
REPORT RequestId: 3fec0fa8-4f1b-41a0-a24a-83300a468c5b	Duration: 0.91 ms	Billed Duration: 100 ms	Memory Size: 512 MB	Max Memory Used: 32 MB	Init Duration: 5.75 ms
```
</div></details>

# 検証結果

`AWS_ACCESS_KEY` `AWS_SECRET_KEY` `AWS_DEFAULT_REGION` 等が環境変数に設定されているのは想定通り。
`init()`, `main()`, `handler()` で取得できる環境変数は（当然ながら）全て同一でした。

[AWS公式リファレンスにあるもの](https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/lambda-environment-variables.html)とほぼ同じですが多少違うものも取れています。

## 環境変数
| Key | Value | Description |
|---|---|---|
|AWS_REGION|ap-northeast-1|Lambda 関数が実行される AWS リージョン|
|AWS_DEFAULT_REGION|ap-northeast-1|Lambda 関数が実行される AWS リージョン|
|AWS_ACCESS_KEY|[AWS AccessKey]|関数の実行ロールから取得されたアクセスキー|
|AWS_ACCESS_KEY_ID|[AWS AccessKey]|関数の実行ロールから取得されたアクセスキー|
|AWS_SECRET_ACCESS_KEY|[AWS SecretAccessKey]|関数の実行ロールから取得されたシークレットアクセスキー|
|AWS_SECRET_KEY|[AWS SecretAccessKey]|関数の実行ロールから取得されたシークレットアクセスキー|
|AWS_SECURITY_TOKEN|[AWS SessionToken]|関数の実行ロールから取得されたセッショントークン|
|AWS_SESSION_TOKEN|[AWS SessionToken]|関数の実行ロールから取得されたセッショントークン|
|AWS_EXECUTION_ENV|AWS_Lambda_go1.x|ランタイム識別子 プレフィックス `AWS_Lambda_` |
|AWS_LAMBDA_FUNCTION_MEMORY_SIZE|512|関数で使用できるメモリの量 (MB 単位)|
|AWS_LAMBDA_FUNCTION_NAME|[functionName]|function名|
|AWS_LAMBDA_FUNCTION_VERSION|$LATEST|実行される関数のバージョン|
|AWS_LAMBDA_LOG_GROUP_NAME|/aws/lambda/[functionName]|CloudWatchのロググループ|
|AWS_LAMBDA_LOG_STREAM_NAME|2020/01/27/[$LATEST]ebf3ce00674e4576a679be48a4655a2f|CloudWatchのログストリーム名|
|LAMBDA_RUNTIME_DIR|/var/runtime|ランタイムライブラリへのパス|
|LAMBDA_TASK_ROOT|/var/task|Lambda 関数コードのパス|
|LANG|en_US.UTF-8|ランタイムのロケール|
|LD_LIBRARY_PATH|/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib||
|PATH|/usr/local/bin:/usr/bin/:/bin:/opt/bin||
|TZ|:UTC|環境のタイムゾーン (UTC)|
|_HANDLER|main|ハンドラー名|
|_AWS_XRAY_DAEMON_ADDRESS|169.254.79.2||
|_AWS_XRAY_DAEMON_PORT|2000||
|AWS_XRAY_CONTEXT_MISSING|LOG_ERROR||
|AWS_XRAY_DAEMON_ADDRESS|169.254.79.2:2000||
|_X_AMZN_TRACE_ID|Parent||
|_LAMBDA_CONSOLE_SOCKET|20||
|_LAMBDA_CONTROL_SOCKET|18||
|_LAMBDA_LOG_FD|27||
|_LAMBDA_RUNTIME_LOAD_TIME|1.23406E+12||
|_LAMBDA_SB_ID|0||
|_LAMBDA_SERVER_PORT|33395||
|_LAMBDA_SHARED_MEM_FD|15||